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>
197 lines
6.8 KiB
Python
197 lines
6.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Finish WG-primary / FRP-aux: LF watch, restart unit, SSH-over-WG, FE, checklist."""
|
|
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, user, password, tries=10):
|
|
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=45,
|
|
banner_timeout=90,
|
|
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, timeout=120):
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
def put_lf_sh(sftp, local: Path, remote: str) -> None:
|
|
data = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
|
|
with sftp.file(remote, "wb") as f:
|
|
f.write(data)
|
|
|
|
|
|
def main() -> int:
|
|
v = ssh(22, "root", VPS_PW)
|
|
print(
|
|
run(v, "curl -sS -m 8 -o /dev/null -w 'dylive:%{http_code}\\n' https://dylive.nomadro.com/api/health"),
|
|
flush=True,
|
|
)
|
|
|
|
g = ssh(4014, "eric", GUEST_PW)
|
|
sftp = g.open_sftp()
|
|
put_lf_sh(sftp, ROOT / "scripts/linux/wireguard_watch.sh", "/tmp/_up_wireguard_watch.sh")
|
|
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; "
|
|
"head -1 /opt/douyinWEB/scripts/linux/wireguard_watch.sh | od -An -tx1 | head -1; "
|
|
"systemctl restart douyinweb-wireguard-watch douyinweb-api; "
|
|
"sleep 2; "
|
|
"systemctl is-active douyinweb-wireguard-watch douyinweb-api wg-quick@wg-dy; "
|
|
"/opt/douyinWEB/scripts/linux/wireguard_watch.sh once; "
|
|
"systemctl status douyinweb-wireguard-watch --no-pager -l | head -20"
|
|
"'",
|
|
90,
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
|
|
print("=== SSH over WG ===", flush=True)
|
|
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; ip -br a show wg-dy; "
|
|
"curl -sS -m 3 -o /dev/null -w local_api:%{http_code}\\\\n http://127.0.0.1:8014/api/health'",
|
|
60,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
# FE rebuild
|
|
g = ssh(4014, "eric", GUEST_PW)
|
|
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 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"
|
|
"'",
|
|
)
|
|
g.close()
|
|
print("BUILD started", flush=True)
|
|
|
|
for i in range(45):
|
|
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 -40 /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()
|
|
|
|
print("=== matrix ===", 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 'via_caddy:%{http_code}\\n' https://dy.nomadro.com/api/health; "
|
|
"curl -sS -m 8 -o /dev/null -w 'dylive:%{http_code}\\n' https://dylive.nomadro.com/api/health; "
|
|
"ss -lntp | grep -E ':4014|:7000' | head -4 || true",
|
|
40,
|
|
),
|
|
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") or {}).get("primary"), flush=True)
|
|
print("ssh", roles.get("ops_ssh") or {}, 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", cl.get("ok"), "failed", cl.get("failed"), "policy", cl.get("policy"), 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)
|
|
|
|
v.close()
|
|
print("DONE", flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|