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>
132 lines
4.3 KiB
Python
132 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Repair stale wg-dy (overseas ↔ physical 10.8.0.2). Do not touch dylive / Chrome / Neko."""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
PASS = "4669805"
|
|
|
|
|
|
def ssh(host, port, user, pw, tries=10):
|
|
last = None
|
|
for i in range(tries):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
host,
|
|
port,
|
|
user,
|
|
pw,
|
|
timeout=45,
|
|
banner_timeout=120,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
)
|
|
return c
|
|
except Exception as ex:
|
|
last = ex
|
|
print(f"ssh {host}:{port} {i} {type(ex).__name__}", flush=True)
|
|
time.sleep(2)
|
|
raise SystemExit(f"ssh fail {last}")
|
|
|
|
|
|
def run(c, cmd, timeout=90):
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
def public(url: str) -> str:
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=20) as r:
|
|
return f"{r.status} {(r.read()[:80]).decode('utf-8','replace')}"
|
|
except Exception as ex:
|
|
return f"ERR {getattr(ex, 'code', type(ex).__name__)} {str(ex)[:120]}"
|
|
|
|
|
|
def main() -> int:
|
|
print("BEFORE", public("https://dy.nomadro.com/api/health"), flush=True)
|
|
print("dylive", public("https://dylive.nomadro.com/api/health"), flush=True)
|
|
print("cn", public("https://dy.nomadro.cn/api/health"), flush=True)
|
|
|
|
g = ssh("107.173.30.245", 4014, "eric", PASS)
|
|
v = ssh("107.173.30.245", 22, "root", "Xiao4669805")
|
|
|
|
print("\n=== guest wg before ===", flush=True)
|
|
print(run(g, f"echo '{PASS}' | sudo -S -p '' wg show wg-dy"), flush=True)
|
|
print("=== ensure PersistentKeepalive + bounce wg-dy on guest ===", flush=True)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{PASS}' | sudo -S -p '' bash -lc '"
|
|
"set -e; "
|
|
"test -f /etc/wireguard/wg-dy.conf; "
|
|
"grep -q PersistentKeepalive /etc/wireguard/wg-dy.conf || "
|
|
" sed -i \"/AllowedIPs/a PersistentKeepalive = 25\" /etc/wireguard/wg-dy.conf; "
|
|
"systemctl restart wg-quick@wg-dy || (wg-quick down wg-dy 2>/dev/null || true; wg-quick up wg-dy); "
|
|
"sleep 2; wg show wg-dy; "
|
|
"ping -c 3 -W 2 10.8.0.1 || true"
|
|
"'",
|
|
timeout=60,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
print("\n=== VPS wg after guest bounce ===", flush=True)
|
|
time.sleep(3)
|
|
print(run(v, "wg show wg-dy; ping -c 3 -W 2 10.8.0.2 || true"), flush=True)
|
|
print(
|
|
run(
|
|
v,
|
|
"curl -sS -m 8 -o /dev/null -w 'api:%{http_code}\\n' http://10.8.0.2:8014/api/health; "
|
|
"curl -sS -m 8 -o /dev/null -w 'web:%{http_code}\\n' http://10.8.0.2:4015/",
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
# If still dead, also restart VPS side interface carefully (not dylive)
|
|
ping = run(v, "ping -c 1 -W 2 10.8.0.2 >/dev/null 2>&1 && echo OK || echo FAIL").strip()
|
|
if ping != "OK":
|
|
print("\n=== bounce VPS wg-dy only ===", flush=True)
|
|
print(
|
|
run(
|
|
v,
|
|
"systemctl restart wg-quick@wg-dy || (wg-quick down wg-dy 2>/dev/null || true; wg-quick up wg-dy); "
|
|
"sleep 2; wg show wg-dy",
|
|
timeout=40,
|
|
),
|
|
flush=True,
|
|
)
|
|
time.sleep(4)
|
|
# guest keepalive should re-handshake
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{PASS}' | sudo -S -p '' bash -lc '"
|
|
"wg show wg-dy; ping -c 3 -W 2 10.8.0.1 || true"
|
|
"'",
|
|
),
|
|
flush=True,
|
|
)
|
|
print(run(v, "ping -c 3 -W 2 10.8.0.2 || true; curl -sS -m 8 -o /dev/null -w 'api:%{http_code}\\n' http://10.8.0.2:8014/api/health"), flush=True)
|
|
|
|
print("\n=== public after ===", flush=True)
|
|
time.sleep(1)
|
|
print("dy.com", public("https://dy.nomadro.com/api/health"), flush=True)
|
|
print("dylive", public("https://dylive.nomadro.com/api/health"), flush=True)
|
|
print("cn", public("https://dy.nomadro.cn/api/health"), flush=True)
|
|
|
|
g.close()
|
|
v.close()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|