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>
265 lines
9.4 KiB
Python
265 lines
9.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Install WG-primary / FRP-aux: watch on guest, idempotent wg unit on VPS+guest, verify SSH over WG."""
|
|
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"
|
|
|
|
WATCH_UNIT = """[Unit]
|
|
Description=DouyinWEB WireGuard watch (wg-dy + wg-dy-cn keepalive)
|
|
After=network-online.target
|
|
Wants=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
Environment="WIREGUARD_WATCH_IFACES=wg-dy wg-dy-cn"
|
|
Environment=WIREGUARD_WATCH_INTERVAL=15
|
|
Environment=WIREGUARD_STALE_SECONDS=90
|
|
Environment=WIREGUARD_RESTART_COOLDOWN=60
|
|
ExecStart=/opt/douyinWEB/scripts/linux/wireguard_watch.sh
|
|
Restart=always
|
|
RestartSec=5
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
"""
|
|
|
|
WG_OVERRIDE = """[Service]
|
|
ExecStart=
|
|
ExecStart=/usr/bin/bash -c 'if ip link show wg-dy >/dev/null 2>&1; then exit 0; else exec /usr/bin/wg-quick up wg-dy; fi'
|
|
ExecStop=
|
|
ExecStop=/usr/bin/bash -c 'if ip link show wg-dy >/dev/null 2>&1; then exec /usr/bin/wg-quick down wg-dy; else exit 0; fi'
|
|
"""
|
|
|
|
|
|
def ssh(port, user, password, tries=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=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 must_dylive(v, label):
|
|
code = run(v, "curl -sS -m 8 -o /dev/null -w '%{http_code}' https://dylive.nomadro.com/api/health", 15).strip()
|
|
print(f"[dylive {label}] {code}", flush=True)
|
|
if code != "200":
|
|
raise SystemExit(f"ABORT dylive {code}")
|
|
|
|
|
|
def main() -> int:
|
|
v = ssh(22, "root", VPS_PW)
|
|
must_dylive(v, "start")
|
|
|
|
# VPS: idempotent wg-quick drop-in (iface may already be up)
|
|
print("=== VPS wg-dy unit polish ===", flush=True)
|
|
sftp = v.open_sftp()
|
|
with sftp.file("/tmp/_wg_override.conf", "w") as f:
|
|
f.write(WG_OVERRIDE)
|
|
sftp.close()
|
|
print(
|
|
run(
|
|
v,
|
|
"mkdir -p /etc/systemd/system/wg-quick@wg-dy.service.d; "
|
|
"install -m 644 /tmp/_wg_override.conf /etc/systemd/system/wg-quick@wg-dy.service.d/override.conf; "
|
|
"systemctl daemon-reload; "
|
|
"systemctl reset-failed wg-quick@wg-dy || true; "
|
|
"systemctl enable --now wg-quick@wg-dy; "
|
|
"systemctl is-active wg-quick@wg-dy; "
|
|
"wg show wg-dy | head -12",
|
|
40,
|
|
),
|
|
flush=True,
|
|
)
|
|
must_dylive(v, "after_vps_wg")
|
|
|
|
# Guest via FRP first (aux) to install watch — then verify SSH via WG (primary)
|
|
g = ssh(4014, "eric", GUEST_PW)
|
|
sftp = g.open_sftp()
|
|
for rel in (
|
|
"scripts/linux/wireguard_watch.sh",
|
|
"backend/app/network_hub.py",
|
|
"frontend/src/app/network/page.tsx",
|
|
):
|
|
local = ROOT / rel
|
|
remote = f"/tmp/_up_{Path(rel).name}"
|
|
if rel.endswith(".sh"):
|
|
# Windows checkouts may be CRLF; guest shebang must be LF-only.
|
|
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)
|
|
else:
|
|
sftp.put(str(local), remote)
|
|
print("put", rel, flush=True)
|
|
with sftp.file("/tmp/_wg_override.conf", "w") as f:
|
|
f.write(WG_OVERRIDE)
|
|
with sftp.file("/tmp/_douyinweb-wireguard-watch.service", "w") as f:
|
|
f.write(WATCH_UNIT)
|
|
sftp.close()
|
|
|
|
print("=== guest WG watch + hub ===", flush=True)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
|
|
"set -e; "
|
|
"mkdir -p /opt/douyinWEB/scripts/linux; "
|
|
"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; "
|
|
"mkdir -p /etc/systemd/system/wg-quick@wg-dy.service.d; "
|
|
"install -m 644 /tmp/_wg_override.conf /etc/systemd/system/wg-quick@wg-dy.service.d/override.conf; "
|
|
"install -m 644 /tmp/_douyinweb-wireguard-watch.service /etc/systemd/system/douyinweb-wireguard-watch.service; "
|
|
"systemctl daemon-reload; "
|
|
"systemctl reset-failed wg-quick@wg-dy || true; "
|
|
"systemctl enable --now wg-quick@wg-dy; "
|
|
"systemctl enable --now douyinweb-wireguard-watch; "
|
|
"systemctl restart douyinweb-api; "
|
|
"sleep 3; "
|
|
"systemctl is-active wg-quick@wg-dy douyinweb-wireguard-watch douyinweb-api; "
|
|
"/opt/douyinWEB/scripts/linux/wireguard_watch.sh once || true; "
|
|
"sshd -T 2>/dev/null | grep -E \"^(port|listenaddress)\" | head -5 || true"
|
|
"'",
|
|
90,
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
|
|
# Preferred ops path: SSH over WireGuard (from VPS → 10.8.0.2), not public :22
|
|
print("=== SSH over WG (primary ops) ===", 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=8 "
|
|
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 build for network page
|
|
g = ssh(4014, "eric", GUEST_PW)
|
|
with g.open_sftp().file("/tmp/_fe_build.sh", "w") as f:
|
|
f.write(
|
|
"#!/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"
|
|
)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c 'chmod +x /tmp/_fe_build.sh; systemctl stop douyinweb-web || true; "
|
|
"nohup /tmp/_fe_build.sh >/dev/null 2>&1 & echo BUILD'",
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
|
|
for i in range(40):
|
|
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:
|
|
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} t=%{time_total}\\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; "
|
|
"curl -sS -m 5 -o /dev/null -w 'frp_ssh_port:%{http_code}\\n' http://127.0.0.1:4014/ || 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 role",
|
|
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 role", frp.get("role"), "purpose", 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())
|