Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP. Co-authored-by: Cursor <cursoragent@cursor.com>
102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Activate wg-dy-cn as primary after Tencent SG allows UDP 51821.
|
|
|
|
Preflight: overseas VPS must ping 10.9.0.2 and curl API before switching Caddy.
|
|
Rollback: scripts/_rollback_caddy_wg_legacy.py
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
if hasattr(sys.stdout, "reconfigure"):
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
OVERSEAS = "107.173.30.245"
|
|
OVERSEAS_PASS = "Xiao4669805"
|
|
GUEST_PASS = "4669805"
|
|
PHYSICAL_CN = "10.9.0.2"
|
|
PHYSICAL_LEGACY = "10.8.0.2"
|
|
|
|
|
|
def ssh(host, port, user, pw):
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(host, port, user, pw, timeout=45, allow_agent=False, look_for_keys=False)
|
|
return c
|
|
|
|
|
|
def run(c, cmd, timeout=60):
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
def preflight(v) -> bool:
|
|
out = run(
|
|
v,
|
|
f"wg show wg-dy-cn 2>/dev/null | grep -q 'latest handshake' || exit 1; "
|
|
f"ping -c 2 -W 3 {PHYSICAL_CN} | grep -q 'received' || exit 1; "
|
|
f"curl -sS -m 10 -o /dev/null -w '%{{http_code}}' http://{PHYSICAL_CN}:8014/api/health",
|
|
35,
|
|
).strip()
|
|
print(f"preflight: {out}", flush=True)
|
|
return out.endswith("200")
|
|
|
|
|
|
def main() -> int:
|
|
v = ssh(OVERSEAS, 22, "root", OVERSEAS_PASS)
|
|
if not preflight(v):
|
|
print(
|
|
"ABORT: wg-dy-cn not healthy. Open Tencent SG inbound UDP 51821 for "
|
|
"107.173.30.245 and physical public IP, then retry.",
|
|
flush=True,
|
|
)
|
|
v.close()
|
|
return 1
|
|
|
|
site = "/etc/caddy/sites/dy.nomadro.com.caddy"
|
|
print(run(v, f"sed -i 's/{PHYSICAL_LEGACY}/{PHYSICAL_CN}/g' {site} && caddy validate --config /etc/caddy/Caddyfile && systemctl reload caddy", 45))
|
|
print(run(v, "curl -sS -m 10 -o /dev/null -w 'dy=%{http_code} t=%{time_total}s\\n' https://dy.nomadro.com/api/health", 20))
|
|
v.close()
|
|
|
|
# neko DNAT via wg-dy-cn
|
|
fix = Path(__file__).resolve().parent / "_fix_neko_webrtc_forward_cn.py"
|
|
src = Path(__file__).resolve().parent / "_fix_neko_webrtc_forward.py"
|
|
fix.write_text(
|
|
src.read_text(encoding="utf-8").replace(
|
|
f'PVE_WG = "{PHYSICAL_LEGACY}"', f'PVE_WG = "{PHYSICAL_CN}"'
|
|
),
|
|
encoding="utf-8",
|
|
)
|
|
r = subprocess.run([sys.executable, str(fix)], capture_output=True, text=True)
|
|
print(r.stdout, flush=True)
|
|
fix.unlink(missing_ok=True)
|
|
|
|
g = ssh(OVERSEAS, 4014, "eric", GUEST_PASS)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{GUEST_PASS}' | sudo -S -p '' bash -c '"
|
|
f"grep -q DY_NEKO_EDGE_UPSTREAM={PHYSICAL_CN} /etc/systemd/system/douyinweb-api.service || "
|
|
f" sed -i \"s/DY_NEKO_EDGE_UPSTREAM=.*/Environment=DY_NEKO_EDGE_UPSTREAM={PHYSICAL_CN}/\" /etc/systemd/system/douyinweb-api.service; "
|
|
f"systemctl daemon-reload; systemctl restart douyinweb-api; sleep 2; systemctl is-active douyinweb-api'",
|
|
60,
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
|
|
v = ssh(OVERSEAS, 22, "root", OVERSEAS_PASS)
|
|
print(run(v, f"echo 'cn path:'; ping -c 3 -W 2 {PHYSICAL_CN} | tail -2; echo 'legacy:'; ping -c 3 -W 2 {PHYSICAL_LEGACY} | tail -2", 30))
|
|
v.close()
|
|
print("ACTIVATED wg-dy-cn primary", flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|