Files
boss_dp/worker/config.py
ddrwode 4a520306b1 ha'ha
2026-02-12 16:46:01 +08:00

44 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
Worker 配置。通过命令行参数或环境变量设置。
"""
import os
# ─── 中央服务器 ───
SERVER_WS_URL: str = os.getenv("SERVER_WS_URL", "ws://127.0.0.1:8000/ws")
# ─── Worker 标识 ───
WORKER_ID: str = os.getenv("WORKER_ID", "worker-1")
WORKER_NAME: str = os.getenv("WORKER_NAME", "本机")
# ─── 比特浏览器 ───
BIT_API_BASE: str = os.getenv("BIT_API_BASE", "http://127.0.0.1:54345")
# ─── WebSocket ───
HEARTBEAT_INTERVAL: int = 25 # 心跳发送间隔(秒)
RECONNECT_DELAY: int = 5 # 断线重连等待(秒)
RECONNECT_MAX_DELAY: int = 60 # 重连最大等待(秒,指数退避上限)
# ─── 隧道(内网穿透,与 Worker 集成) ───
TUNNEL_ENABLED: bool = os.getenv("TUNNEL_ENABLED", "1").strip().lower() in ("1", "true", "yes")
TUNNEL_SERVER: str = os.getenv("TUNNEL_SERVER", "") # 留空则从 SERVER_WS_URL 解析 host
TUNNEL_CONTROL_PORT: int = int(os.getenv("TUNNEL_CONTROL_PORT", "8001"))
TUNNEL_STREAM_PORT: int = int(os.getenv("TUNNEL_STREAM_PORT", "8003"))
TUNNEL_LOCAL_PORT: int = int(os.getenv("TUNNEL_LOCAL_PORT", "54345")) # 暴露的本地端口(如比特浏览器 API
def get_tunnel_server_host() -> str:
"""隧道服务端地址。未设置 TUNNEL_SERVER 时从 SERVER_WS_URL 解析。"""
if TUNNEL_SERVER:
return TUNNEL_SERVER.split(":")[0] if ":" in TUNNEL_SERVER else TUNNEL_SERVER
url = SERVER_WS_URL.strip()
for prefix in ("wss://", "ws://"):
if url.startswith(prefix):
rest = url[len(prefix):]
if "/" in rest:
rest = rest.split("/")[0]
if ":" in rest:
return rest.split(":")[0]
return rest
return "127.0.0.1"