8000 chore: 优化部分代码 · TTB-Network/python-openbmclapi@8f18c32 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f18c32

Browse files
committed
chore: 优化部分代码
1 parent 283bc2b commit 8f18c32

File tree

10 files changed

+42
-36
lines changed

10 files changed

+42
-36
lines changed

core/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ async def load_config():
7070
if clusters.count == 0 or clusters.storages.count == 0:
7171
logger.terror("core.initialize.missing", clusters=clusters.count, storages=clusters.storages.count)
7272

73+
async def load_cluster_certificates():
74+
certificates = await clusters.load_certificates()
75+
web.update_certificates(certificates)
76+
7377
async def main():
7478
global clusters
7579

@@ -93,6 +97,8 @@ async def main():
9397

9498
await setup_dashboard(web.app, task_group)
9599

100+
await load_cluster_certificates()
101+
96102
await clusters.sync()
97103

98104
# serve
@@ -229,7 +235,7 @@ def access_log(request: fastapi.Request, response: fastapi.Response, total_time:
229235
status=response.status_code,
230236
total_time=units.format_count_time(total_time, 4).rjust(14),
231237
user_agent=request.headers.get("User-Agent") or "",
232-
address=address,
238+
address=address.ljust(16),
233239
)
234240

235241
@web.app.middleware("http")

core/cluster.py

< 8000 div class="d-flex flex-justify-end flex-items-center">
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
from . import abc
12
import contextlib
23
import datetime
34
import hmac
45
import io
56
import json
6-
import os
77
from pathlib import Path
88
import sys
9-
import tempfile
109
import time
1110
from typing import Any, Optional
1211
import aiohttp
@@ -754,6 +753,20 @@ async def stop(self):
754753
for cluster in self.clusters:
755754
await cluster.stop_serve()
756755

756+
async def load_certificates(self):
757+
cert_type = utils.get_certificate_type()
758+
if cert_type != abc.CertificateType.CLUSTER:
759+
return []
760+
certificates = []
761+
for cert in await concurrency.gather(*(
762+
cluster.request_cert() for cluster in self.clusters
763+
)):
764+
if cert is None:
765+
continue
766+
certificates.append(cert)
767+
768+
return certificates
769+
757770
async def fetch_cluster_name(self):
758771
assert self._task_group is not None
759772
async with aiohttp.ClientSession(

core/dashboard.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import psutil
1010
from fastapi.staticfiles import StaticFiles
1111

12-
from .config import ROOT_PATH, DEBUG
12+
from .config import ROOT_PATH
1313
from .web import query_per_second_statistics
1414
from .utils import scheduler
1515
from tianxiu2b2t.utils import runtime

core/logger.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import inspect
21
import logging
32
import sys
43
import traceback

core/storage/abc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import abc
2-
import tempfile
32

43
import anyio.abc
54
import io

core/storage/alist.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import io
2-
from tempfile import _TemporaryFileWrapper
32
import time
43
from typing import Any
54
import urllib.parse as urlparse
65
import aiohttp
76
import anyio.abc
8-
from tianxiu2b2t import units
97

108
from . import abc
119
from ..config import USER_AGENT

core/storage/minio.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
from datetime import timedelta
22
import datetime
33
import io
4-
import tempfile
54
import time
65
from typing import Optional
76
import urllib.parse as urlparse
87
import aiohttp
98
import anyio.abc
109

11-
from ..abc import ResponseFile, ResponseFileMemory, ResponseFileRemote, ResponseFileNotFound
10+
from ..abc import ResponseFile, ResponseFileMemory, ResponseFileRemote
1211
from ..utils import UnboundTTLCache
1312
from ..logger import logger
1413

1514
from .abc import CPath, FileInfo, Storage
1615
from miniopy_async import Minio
1716
from miniopy_async.api import BaseURL, presign_v4
1817
from miniopy_async.datatypes import Object
19-
from tianxiu2b2t import units
2018

2119

2220
class MinioStorage(Storage):

core/storage/s3.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from io import BytesIO
22
import io
3-
import tempfile
43
import time
54
import aioboto3.session
65
import anyio.abc
76
import anyio.to_thread
87
import aioboto3
98
import urllib.parse as urlparse
10-
from tianxiu2b2t import units
119

1210
from ..logger import logger
1311
from ..utils import UnboundTTLCache

core/web.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import tianxiu2b2t.anyio.streams.proxy as streams_proxy
1212
from tianxiu2b2t.anyio import concurrency
1313
from tianxiu2b2t.utils import runtime
14-
from tianxiu2b2t.http.asgi import ASGIApplicationBridge, ASGIConfig
14+
from tianxiu2b2t.http.asgi import ASGIApplicationBridge, ASGIConfig, ASGIListener
1515

1616
from . import utils, abc
1717
from .logger import logger
@@ -130,16 +130,18 @@ async def pub_listener(
130130
async def serve(
131131
listener: anyio.abc.Listener,
132132
):
133-
async with listener:
134-
logger.tinfo("web.forward.pub_port", port=pub_port)
135-
if not cfg.bridge_web_application:
136-
await listener.serve(pub_handler)
137-
async with ASGIApplicationBridge(
133+
logger.tinfo("web.forward.pub_port", port=pub_port)
134+
if cfg.bridge_web_application:
135+
asgi_listener = ASGIListener(
138136
ASGIConfig(
139137
app,
140-
)
141-
) as bridge:
142-
await bridge.serve(listener)
138+
),
139+
listener
140+
)
141+
await asgi_listener.serve()
142+
return
143+
async with listener:
144+
await listener.serve(pub_handler)
143145

144146
async def pub_handler(
145147
sock: streams.BufferedByteStream,
@@ -241,29 +243,22 @@ async def setup(
241243
cfg.get("cert.cert"),
242244
cfg.get("cert.key")
243245
))
244-
elif cert_type == abc.CertificateType.CLUSTER:
245-
for cert in await concurrency.gather(*(
246-
cluster.request_cert() for cluster in clusters.clusters
247-
)):
248-
if cert is None:
249-
continue
250-
certificates.append(cert)
251246

252-
if len(certificates) == 0:
253-
raise RuntimeError(t("error.web.certificates"))
247+
update_certificates(certificates)
254248

255-
if tls_listener is None:
256-
raise RuntimeError(t("error.web.tls_listener"))
257-
258-
for cert in certificates:
249+
def update_certificates(
250+
certicates: list[abc.Certificate]
251+
):
252+
assert tls_listener is not None
253+
for cert in certicates:
259254
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
260255
context.load_cert_chain(cert.cert, cert.key)
261256
context.check_hostname = False
262257
context.hostname_checks_common_name = False
263258
context.verify_mode = ssl.CERT_NONE
264259
if cfg.bridge_web_application:
265260
context.set_alpn_protocols(["h2", "http/1.1"])
266-
261+
267262
for domain in cert.domains:
268263
tls_listener.add_context(
269264
domain,

requirements.txt

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
0