|
|
@@ -241,6 +241,25 @@ def check_status(config_path):
|
|
|
print("⚪ 代理服务当前未运行。")
|
|
|
|
|
|
|
|
|
+def check_health(config_path):
|
|
|
+ """用于 Docker HEALTHCHECK / 容器就绪探测的健康检查 (就绪返回码 0,未就绪返回 1)"""
|
|
|
+ mixed_port = 10000
|
|
|
+ if os.path.isfile(config_path):
|
|
|
+ try:
|
|
|
+ with open(config_path, "r", encoding="utf-8") as f:
|
|
|
+ data = yaml.safe_load(f) or {}
|
|
|
+ mixed_port = data.get("mixed-port", 10000)
|
|
|
+ except Exception:
|
|
|
+ pass
|
|
|
+
|
|
|
+ if is_port_listening("127.0.0.1", mixed_port, timeout=1.5):
|
|
|
+ print(f"OK - Proxy is healthy (Mixed-Port {mixed_port} listening)", flush=True)
|
|
|
+ sys.exit(0)
|
|
|
+ else:
|
|
|
+ print(f"FAIL - Proxy is not ready (Mixed-Port {mixed_port} not listening)", file=sys.stderr, flush=True)
|
|
|
+ sys.exit(1)
|
|
|
+
|
|
|
+
|
|
|
def start_service(args):
|
|
|
"""启动代理服务"""
|
|
|
bin_path = find_mihomo_bin(args.bin)
|
|
|
@@ -255,8 +274,23 @@ def start_service(args):
|
|
|
print("👉 请先运行: python tools/generate_clash_multi_port.py 生成配置。")
|
|
|
sys.exit(1)
|
|
|
|
|
|
- print(f"[*] Mihomo 程序: {bin_path}")
|
|
|
- print(f"[*] 配置文件路径: {config_path}")
|
|
|
+ print(f"[*] Mihomo 程序: {bin_path}", flush=True)
|
|
|
+ print(f"[*] 配置文件路径: {config_path}", flush=True)
|
|
|
+
|
|
|
+ # 自动适配 Docker / 外部连接:确保 allow-lan 开启
|
|
|
+ allow_lan_env = os.getenv("ALLOW_LAN", "").lower() in ("1", "true", "yes")
|
|
|
+ in_docker = os.path.exists("/.dockerenv") or os.getenv("DOCKER_CONTAINER") == "true"
|
|
|
+ if getattr(args, "allow_lan", False) or allow_lan_env or in_docker:
|
|
|
+ try:
|
|
|
+ with open(config_path, "r", encoding="utf-8") as f:
|
|
|
+ content = f.read()
|
|
|
+ if "allow-lan: false" in content:
|
|
|
+ print("[*] 正在为容器/局域网环境自动修正 allow-lan: true ...", flush=True)
|
|
|
+ content = content.replace("allow-lan: false", "allow-lan: true")
|
|
|
+ with open(config_path, "w", encoding="utf-8") as f:
|
|
|
+ f.write(content)
|
|
|
+ except Exception as e:
|
|
|
+ print(f"⚠️ 自动配置 allow-lan 提示: {e}", flush=True)
|
|
|
|
|
|
# 读取配置概要
|
|
|
try:
|
|
|
@@ -360,9 +394,16 @@ def main():
|
|
|
parser.add_argument("--log-file", type=str, default=None, help="后台运行日志输出文件 (默认: logs/mihomo.log)")
|
|
|
parser.add_argument("--status", action="store_true", help="检查代理服务运行状态")
|
|
|
parser.add_argument("--stop", action="store_true", help="停止后台代理服务")
|
|
|
+ parser.add_argument("--check-health", action="store_true", help="检查服务端口健康状态 (正常监听返回 0,未就绪返回 1)")
|
|
|
+ parser.add_argument("--allow-lan", action="store_true", help="强制确保 allow-lan 开启 (允许局域网/跨主机访问)")
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
+ if args.check_health:
|
|
|
+ config_path = find_config_file(args.config)
|
|
|
+ check_health(config_path)
|
|
|
+ return
|
|
|
+
|
|
|
if args.stop:
|
|
|
stop_service()
|
|
|
return
|