douyinWEB/scripts/_finish_wg_primary_frp_aux.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
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>
2026-09-04 22:42:40 -05:00

206 lines
7.1 KiB
Python

# -*- coding: utf-8 -*-
"""Finish WG-primary / FRP-aux via FRP guest + optional VPS SSH."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
VPS = "107.173.30.245"
PVE_WG = "10.8.0.2"
GUEST_PW = "4669805"
VPS_PW = "Xiao4669805"
def ssh(port: int, user: str, password: str, tries: int = 8):
last = None
for i in range(tries):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
VPS,
port,
user,
password,
timeout=40,
banner_timeout=80,
allow_agent=False,
look_for_keys=False,
)
return c
except Exception as ex:
last = ex
print(f"ssh:{port} fail{i} {type(ex).__name__}", flush=True)
time.sleep(2 + i)
raise last # type: ignore[misc]
def run(c, cmd: str, timeout: int = 120) -> str:
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
def put_bytes(sftp, remote: str, data: bytes) -> None:
with sftp.file(remote, "wb") as f:
f.write(data)
def main() -> int:
# --- guest via FRP (aux ops path) ---
g = ssh(4014, "eric", GUEST_PW)
sftp = g.open_sftp()
watch = (ROOT / "scripts/linux/wireguard_watch.sh").read_bytes()
watch = watch.replace(b"\r\n", b"\n").replace(b"\r", b"\n")
put_bytes(sftp, "/tmp/_up_wireguard_watch.sh", watch)
sftp.put(str(ROOT / "backend/app/network_hub.py"), "/tmp/_up_network_hub.py")
sftp.put(str(ROOT / "frontend/src/app/network/page.tsx"), "/tmp/_up_page.tsx")
sftp.close()
print(
run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
"set -e; "
"install -m 755 /tmp/_up_wireguard_watch.sh /opt/douyinWEB/scripts/linux/wireguard_watch.sh; "
"install -m 644 /tmp/_up_network_hub.py /opt/douyinWEB/backend/app/network_hub.py; "
"install -m 644 /tmp/_up_page.tsx /opt/douyinWEB/frontend/src/app/network/page.tsx; "
"file /opt/douyinWEB/scripts/linux/wireguard_watch.sh; "
"systemctl reset-failed douyinweb-wireguard-watch || true; "
"systemctl restart douyinweb-wireguard-watch; "
"systemctl restart douyinweb-api; "
"sleep 3; "
"systemctl is-active wg-quick@wg-dy douyinweb-wireguard-watch douyinweb-api frpp; "
"/opt/douyinWEB/scripts/linux/wireguard_watch.sh once; "
"wg show wg-dy | head -10; "
"systemctl status douyinweb-wireguard-watch --no-pager -l | head -18"
"'",
90,
),
flush=True,
)
g.close()
# --- SSH over WG from VPS (primary ops path) ---
print("=== SSH over WG ===", flush=True)
try:
v = ssh(22, "root", VPS_PW, tries=5)
print(
run(
v,
"command -v sshpass >/dev/null || apt-get install -y -qq sshpass >/dev/null; "
f"sshpass -p '{GUEST_PW}' ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 "
f"eric@{PVE_WG} 'hostname; curl -sS -m 3 -o /dev/null -w local_api:%{{http_code}}\\\\n "
"http://127.0.0.1:8014/api/health'",
50,
),
flush=True,
)
print(
run(
v,
"curl -sS -m 8 -o /dev/null -w 'via_wg:%{http_code}\\n' http://10.8.0.2:8014/api/health; "
"curl -sS -m 8 -o /dev/null -w 'dylive:%{http_code}\\n' https://dylive.nomadro.com/api/health",
30,
),
flush=True,
)
v.close()
except Exception as ex:
print(f"VPS ssh skipped: {type(ex).__name__} {ex}", flush=True)
# --- FE build ---
g = ssh(4014, "eric", GUEST_PW)
print(
run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
"cat >/tmp/_fe_build.sh <<\"EOF\"\n"
"#!/bin/bash\n"
"cd /opt/douyinWEB/frontend || exit 1\n"
"export DY_INTERNAL_API_URL=http://127.0.0.1:8014\n"
"export DY_PUBLIC_BASE_URL=https://dy.nomadro.com\n"
"npm run build >/tmp/fe-wg.log 2>&1\n"
"echo EXIT:$? >>/tmp/fe-wg.log\n"
"EOF\n"
"chmod +x /tmp/_fe_build.sh; rm -f /tmp/fe-wg.log; "
"systemctl stop douyinweb-web || true; "
"nohup /tmp/_fe_build.sh >/dev/null 2>&1 & echo BUILD'"
),
flush=True,
)
g.close()
for i in range(50):
time.sleep(12)
g = ssh(4014, "eric", GUEST_PW)
st = run(
g,
"grep '^EXIT:' /tmp/fe-wg.log 2>/dev/null | tail -1; "
"test -f /opt/douyinWEB/frontend/.next/BUILD_ID && echo HAS || echo NO",
)
g.close()
print(f"fe[{i}] {st.strip()}", flush=True)
if "EXIT:0" in st and "HAS" in st:
break
if "EXIT:" in st and "EXIT:0" not in st:
g = ssh(4014, "eric", GUEST_PW)
print(run(g, "tail -50 /tmp/fe-wg.log"), flush=True)
g.close()
raise SystemExit("fe fail")
else:
raise SystemExit("fe timeout")
g = ssh(4014, "eric", GUEST_PW)
print(
run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' systemctl start douyinweb-web; "
"sleep 3; systemctl is-active douyinweb-web",
),
flush=True,
)
g.close()
# --- public checklist ---
print("=== checklist ===", flush=True)
with httpx.Client(base_url="https://dy.nomadro.com", timeout=25.0, follow_redirects=False) as client:
client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
j = client.get("/api/network").json()
roles = j.get("path_roles") or {}
print("policy", roles.get("policy"), flush=True)
print("app", roles.get("app_https"), flush=True)
print("ssh", roles.get("ops_ssh"), flush=True)
wg = j.get("wireguard") or {}
frp = j.get("frp") or {}
print(
"wg",
wg.get("role"),
"fresh",
wg.get("handshake_fresh"),
"age",
wg.get("handshake_age_sec"),
"watch",
(wg.get("watch") or {}).get("unit"),
flush=True,
)
print("frp", frp.get("role"), frp.get("purpose"), flush=True)
cl = j.get("physical_checklist") or {}
print("checklist ok=", cl.get("ok"), "failed=", cl.get("failed"), flush=True)
for c in cl.get("checks") or []:
print(f" [{'OK' if c.get('ok') else '!!'}] {c.get('name')}: {c.get('detail')}", flush=True)
print("page", client.get("/network").status_code, flush=True)
print("health", client.get("/api/health").status_code, flush=True)
print("DONE", flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())