2026-02-12 16:27:43 +08:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
"""
|
2026-02-14 16:49:44 +08:00
|
|
|
|
中央服务器入口(Django + Channels + uvicorn)。
|
2026-02-12 16:27:43 +08:00
|
|
|
|
启动方式: python -m server.main
|
|
|
|
|
|
"""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
import logging
|
2026-02-14 16:49:44 +08:00
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "server.settings")
|
|
|
|
|
|
|
|
|
|
|
|
import django # noqa: E402
|
|
|
|
|
|
django.setup() # noqa: E402
|
|
|
|
|
|
|
|
|
|
|
|
import uvicorn # noqa: E402
|
|
|
|
|
|
|
|
|
|
|
|
from server import config # noqa: E402
|
2026-03-03 13:32:20 +08:00
|
|
|
|
from server.core.task_health_monitor import task_health_monitor # noqa: E402
|
2026-02-14 16:49:44 +08:00
|
|
|
|
from server.core.worker_manager import worker_manager # noqa: E402
|
|
|
|
|
|
from tunnel.server import TunnelServer # noqa: E402
|
2026-02-12 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
|
level=logging.INFO,
|
|
|
|
|
|
format="%(asctime)s %(name)-28s %(levelname)-5s %(message)s",
|
|
|
|
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
|
|
|
|
)
|
|
|
|
|
|
logger = logging.getLogger("server.main")
|
|
|
|
|
|
|
2026-02-12 16:49:50 +08:00
|
|
|
|
|
2026-02-14 16:49:44 +08:00
|
|
|
|
async def run_server():
|
|
|
|
|
|
"""启动服务器:Django ASGI + 心跳巡检 + 隧道。"""
|
|
|
|
|
|
# 启动心跳巡检
|
2026-02-12 16:49:50 +08:00
|
|
|
|
asyncio.create_task(worker_manager.check_heartbeats_loop())
|
2026-03-03 13:32:20 +08:00
|
|
|
|
# 启动超时任务巡检
|
|
|
|
|
|
asyncio.create_task(task_health_monitor.run_loop())
|
2026-02-14 16:49:44 +08:00
|
|
|
|
|
|
|
|
|
|
# 启动隧道服务
|
2026-02-12 16:49:50 +08:00
|
|
|
|
tunnel_server = TunnelServer(
|
|
|
|
|
|
control_port=config.TUNNEL_CONTROL_PORT,
|
|
|
|
|
|
stream_port=config.TUNNEL_STREAM_PORT,
|
|
|
|
|
|
proxy_base_port=config.TUNNEL_PROXY_BASE_PORT,
|
|
|
|
|
|
host=config.HOST,
|
|
|
|
|
|
)
|
|
|
|
|
|
await tunnel_server.start()
|
2026-02-14 16:49:44 +08:00
|
|
|
|
|
2026-02-12 16:49:50 +08:00
|
|
|
|
logger.info(
|
|
|
|
|
|
"服务器启动: http://%s:%s | 隧道: 控制 %s, 流 %s, 代理起始 %s",
|
|
|
|
|
|
config.HOST, config.PORT,
|
|
|
|
|
|
config.TUNNEL_CONTROL_PORT, config.TUNNEL_STREAM_PORT, config.TUNNEL_PROXY_BASE_PORT,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-02-14 16:49:44 +08:00
|
|
|
|
# 启动 uvicorn(使用 Django Channels ASGI 应用)
|
|
|
|
|
|
uvi_config = uvicorn.Config(
|
|
|
|
|
|
"server.asgi:application",
|
|
|
|
|
|
host=config.HOST,
|
|
|
|
|
|
port=config.PORT,
|
|
|
|
|
|
log_level="info",
|
|
|
|
|
|
)
|
|
|
|
|
|
server = uvicorn.Server(uvi_config)
|
2026-02-12 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
|
try:
|
2026-02-14 16:49:44 +08:00
|
|
|
|
await server.serve()
|
2026-02-12 16:27:43 +08:00
|
|
|
|
finally:
|
2026-02-14 16:49:44 +08:00
|
|
|
|
await tunnel_server.stop()
|
|
|
|
|
|
logger.info("服务器已关闭")
|
2026-02-12 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2026-02-14 16:49:44 +08:00
|
|
|
|
asyncio.run(run_server())
|
2026-02-12 16:27:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
main()
|