Skip to content

Commit

Permalink
feat: 支持https服务器 & feat: 支持同时监听多个端口
Browse files Browse the repository at this point in the history
  • Loading branch information
helloplhm-qwq committed Jan 12, 2024
1 parent e2ae17f commit 068dc5a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
16 changes: 13 additions & 3 deletions common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,18 @@ class ConfigReadException(Exception):
"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,
Expand Down Expand Up @@ -83,7 +93,7 @@ class ConfigReadException(Exception):
"wy": ["128k"],
"mg": ["128k"],
}
}
},
},
"security": {
"rate_limit": {
Expand Down
51 changes: 44 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,61 @@ async def handle_404(request):
# 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]"):
logger.error("端口已被占用,请检查\n" + str(e))
logger.info('服务器将在10s后再次尝试启动...')
await asyncio.sleep(10)
logger.info('重新尝试启动...')
else:
raise


async def initMain():
await scheduler.run()
Expand Down

0 comments on commit 068dc5a

Please sign in to comment.