From 068dc5ab89e569f2ef2a0b0d3eab9726692739b9 Mon Sep 17 00:00:00 2001 From: helloplhm-qwq Date: Fri, 12 Jan 2024 23:12:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81https=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=20&=20feat:=20=E6=94=AF=E6=8C=81=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E7=9B=91=E5=90=AC=E5=A4=9A=E4=B8=AA=E7=AB=AF=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/config.py | 16 ++++++++++++--- main.py | 51 +++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/common/config.py b/common/config.py index fe9b876..582fda5 100644 --- a/common/config.py +++ b/common/config.py @@ -49,8 +49,18 @@ default = { "common": { "host": "0.0.0.0", "_host-desc": "服务器启动时所使用的HOST地址", - "port": "9763", - "_port-desc": "服务器启动时所使用的端口", + "ports": [ 9763 ], + "_ports-desc": "服务器启动时所使用的端口", + "ssl_info": { + "desc": "服务器https配置", + "enable": False, + "ssl_ports": [ 443 ], + "path": { + "desc": "ssl证书的文件地址", + "cert": "/path/to/your/cer", + "privkey": "/path/to/your/private/key", + }, + }, "debug_mode": False, "_debug_mode-desc": "是否开启调试模式", "log_length_limit": 500, @@ -83,7 +93,7 @@ default = { "wy": ["128k"], "mg": ["128k"], } - } + }, }, "security": { "rate_limit": { diff --git a/main.py b/main.py index 272327c..e7a8fc9 100644 --- a/main.py +++ b/main.py @@ -150,17 +150,51 @@ if (config.read_config('common.allow_download_script')): # 404 app.router.add_route('*', '/{tail:.*}', handle_404) - async def run_app(): while True: try: host = config.read_config('common.host') - port = int(config.read_config('common.port')) - runner = aiohttp.web.AppRunner(app) - await runner.setup() - site = aiohttp.web.TCPSite(runner, host, port) - await site.start() - logger.info(f"监听 -> http://{host}:{port}") + ports = [int(port) for port in config.read_config('common.ports')] + ssl_ports = [int(port) for port in config.read_config('common.ssl_info.ssl_ports')] + + final_ssl_ports = [] + final_ports = [] + for p in ports: + if (p not in ssl_ports): + final_ports.append(p) + else: + final_ssl_ports.append(p) + # 读取证书和私钥路径 + cert_path = config.read_config('common.ssl_info.path.cert') + privkey_path = config.read_config('common.ssl_info.path.privkey') + + # 创建 HTTP AppRunner + http_runner = aiohttp.web.AppRunner(app) + await http_runner.setup() + + # 启动 HTTP 端口监听 + for port in final_ports: + http_site = aiohttp.web.TCPSite(http_runner, host, port) + await http_site.start() + logger.info(f"监听 -> http://{host}:{port}") + + if (config.read_config("common.ssl_info.enable") and final_ssl_ports != []): + if (os.path.exists(cert_path) and os.path.exists(privkey_path)): + import ssl + # 创建 SSL 上下文,加载配置文件中指定的证书和私钥 + ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) + ssl_context.load_cert_chain(cert_path, privkey_path) + + # 创建 HTTPS AppRunner + https_runner = aiohttp.web.AppRunner(app) + await https_runner.setup() + + # 启动 HTTPS 端口监听 + for port in ssl_ports: + https_site = aiohttp.web.TCPSite(https_runner, host, port, ssl_context=ssl_context) + await https_site.start() + logger.info(f"监听 -> https://{host}:{port}") + return except OSError as e: if str(e).startswith("[Errno 98]"): @@ -168,6 +202,9 @@ async def run_app(): logger.info('服务器将在10s后再次尝试启动...') await asyncio.sleep(10) logger.info('重新尝试启动...') + else: + raise + async def initMain(): await scheduler.run()