Skip to content

Commit 3745095

Browse files
committed
feat: 新增多证书共用端口
1 parent 4911e44 commit 3745095

File tree

5 files changed

+540
-165
lines changed

5 files changed

+540
-165
lines changed

core/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import asyncio
22
import os
33
import time
4+
5+
from core import web
46
from .logger import logger
57
from . import utils
68
import atexit
79
from . import cluster
810
from . import scheduler
9-
from . import web
11+
from . import web_old
1012
from . import dashboard
1113
from . import database
1214

@@ -28,10 +30,10 @@ async def main():
2830
await asyncio.gather(*[
2931
call(m, "init") for m in (
3032
scheduler,
31-
web,
3233
database,
3334
dashboard,
3435
cluster,
36+
web
3537
)
3638
])
3739
_WAITLOCK.acquire()
@@ -45,10 +47,10 @@ async def main():
4547
await asyncio.gather(*[
4648
call(m, "unload") for m in (
4749
scheduler,
48-
web,
4950
cluster,
5051
database,
51-
dashboard
52+
dashboard,
53+
web
5254
)
5355
])
5456

core/cluster.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ async def _check_available(self):
7878
self.available_storages.remove(storage)
7979
except:
8080
logger.ttraceback("storage.error.check_available", type=storage.type, path=storage.path, url=getattr(storage, "url", None))
81-
self.available_storages.remove(storage)
81+
if storage in self.available_storages:
82+
self.available_storages.remove(storage)
8283
if res:
8384
if storage not in self.available_storages:
8485
self.available_storages.append(storage)
@@ -573,6 +574,7 @@ def __init__(self):
573574
self.cluster_id_tables: dict[str, 'Cluster'] = {}
574575
self.file_manager = FileListManager(self)
575576
self.storage_manager = StorageManager(self)
577+
self.clusters_certificate: dict[str, ClusterCertificate] = {}
576578

577579
def add_cluster(self, cluster: 'Cluster'):
578580
self.cluster_id_tables[cluster.id] = cluster
@@ -585,10 +587,11 @@ async def start(self):
585587
self.storage_manager.init()
586588
logger.tdebug("cluster.debug.base_url", base_url=config.const.base_url)
587589

588-
cert = await self.get_certificate()
589-
await web.start_ssl_server(
590-
cert.cert, cert.key
591-
)
590+
certificates = await self.get_certificates()
591+
for cert in certificates:
592+
await web.start_private_server(
593+
cert.cert, cert.key
594+
)
592595
# start web ssl
593596
public_port = config.const.public_port
594597
public_host = cert.host
@@ -617,17 +620,26 @@ def byoc(self):
617620
return True
618621
return False
619622

620-
async def get_certificate(self):
623+
async def get_certificates(self, cluster_id: Optional[str] = None):
621624
if config.const.ssl_cert and config.const.ssl_key:
622625
main = ClusterCertificate(
623626
config.const.host,
624627
Path(config.const.ssl_cert),
625628
Path(config.const.ssl_key)
626629
)
627630
if main.is_valid:
628-
return main
629-
await asyncio.gather(*[cluster.request_cert() for cluster in self.clusters])
630-
return [cluster.certificate for cluster in self.clusters][0]
631+
return [
632+
main
633+
]
634+
if not self.clusters_certificate:
635+
await asyncio.gather(*[cluster.request_cert() for cluster in self.clusters])
636+
for cluster in self.clusters:
637+
self.clusters_certificate[cluster.id] = cluster.certificate
638+
if cluster_id is None:
639+
return list(self.clusters_certificate.values())
640+
if cluster_id in self.clusters_certificate:
641+
return [self.clusters_certificate[cluster_id]]
642+
return []
631643

632644
async def stop(self):
633645
await asyncio.gather(*[cluster.disable(True) for cluster in self.clusters])
@@ -736,7 +748,7 @@ async def request_cert(self):
736748
key.write(result.ack["key"])
737749

738750
async def enable(self):
739-
cert = await clusters.get_certificate()
751+
cert = await clusters.get_certificates(self.id)
740752
scheduler.cancel(self.delay_enable_task)
741753
if self.want_enable:
742754
return
@@ -747,7 +759,7 @@ async def enable(self):
747759
result = await self.socket_io.emit(
748760
"enable", {
749761
"host": config.const.host,
750-
"port": config.const.public_port,
762+
"port": config.const.public_port or config.const.port,
751763
"byoc": clusters.byoc(),
752764
"version": API_VERSION,
753765
"noFastEnable": True,

core/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ def threads(self):
114114

115115
@property
116116
def public_port(self):
117-
return os.environ.get("web.public_port", Config.get("web.public_port", 6543))
117+
return os.environ.get("web.public_port", Config.get("web.public_port", 6543)) or 6543
118+
119+
@property
120+
def port(self):
121+
return os.environ.get("web.port", Config.get("web.port", 0)) or 0
118122

119123
@property
120124
def ssl_dir(self):
@@ -166,7 +170,7 @@ def rank_clusters_url(self):
166170

167171
const = Const()
168172

169-
VERSION = "3.2.7"
173+
VERSION = "3.3.0"
170174
API_VERSION = "1.13.1"
171175
USER_AGENT = f"openbmclapi/{API_VERSION} python-openbmclapi/{VERSION}"
172176
PYTHON_VERSION = ".".join(map(str, (sys.version_info.major, sys.version_info.minor, sys.version_info.micro)))

0 commit comments

Comments
 (0)