douyinWEB/scripts/_repair_dy_edge_keep.py
2026-09-13 22:10:49 -05:00

122 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""Repair dy.nomadro.com path: bounce wg-dy, ping physical, keep edge services up.
Does NOT start nomadro/robook/tiktok-promo/xauto/ebook/hackrobot product stacks.
"""
from __future__ import annotations
import sys
import time
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
VPS = ("107.173.30.245", 22, "root", "Xiao4669805")
GUEST = ("107.173.30.245", 4014, "eric", "4669805")
GUEST_SUDO = "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=60, banner_timeout=180, allow_agent=False, look_for_keys=False)
print(f"ssh ok {host}:{port} try={i}", flush=True)
return c
except Exception as ex:
last = ex
print(f"ssh retry {host}:{port} {i} {type(ex).__name__}: {ex}", flush=True)
time.sleep(5)
raise SystemExit(f"ssh failed {host}:{port}: {last}")
def run(c, cmd, t=120, label=""):
print(f"\n[{label}]$ {cmd[:220]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
if out.strip():
print(out[-5000:] if len(out) > 5000 else out, flush=True)
print(f"[{label}]→ {code}", flush=True)
return code, out
def main() -> int:
v = ssh(*VPS)
# Ensure edge stays up
run(
v,
"systemctl start caddy nginx frpp docker 'wg-quick@wg-dy'; "
"systemctl is-active caddy nginx frpp docker 'wg-quick@wg-dy'",
label="vps-edge",
)
# Bounce WG on VPS
run(
v,
"wg-quick down wg-dy 2>/dev/null || true; wg-quick up wg-dy; sleep 2; "
"wg show wg-dy | head -25; ping -c 3 -W 3 10.8.0.2 | tail -5",
t=60,
label="vps-wg-bounce",
)
# Try FRP guest: bounce peer side too
try:
g = ssh(*GUEST)
run(
g,
f"echo '{GUEST_SUDO}' | sudo -S -p '' bash -lc '"
"wg show wg-dy | head -20; "
"wg-quick down wg-dy 2>/dev/null || true; "
"wg-quick up wg-dy; sleep 2; "
"wg show wg-dy | head -20; "
"ping -c 3 -W 3 10.8.0.1 | tail -5; "
"systemctl is-active douyinweb-api douyinweb-web || true; "
"systemctl start douyinweb-api douyinweb-web 2>/dev/null || true; "
"curl -sS -m 8 http://127.0.0.1:8014/api/health | head -c 200; echo"
"'",
t=90,
label="guest-wg",
)
g.close()
except Exception as ex:
print(f"guest FRP path issue: {ex}", flush=True)
# Re-check from VPS through tunnel
time.sleep(3)
run(
v,
"wg show wg-dy | head -20; "
"ping -c 4 -W 3 10.8.0.2 | tail -6; "
"curl -sS -m 12 -o /dev/null -w 'via_wg_api=%{http_code}\\n' http://10.8.0.2:8014/api/health; "
"curl -sS -m 12 -o /dev/null -w 'via_wg_web=%{http_code}\\n' http://10.8.0.2:4015/",
t=50,
label="vps-verify-tunnel",
)
v.close()
print("\n=== public ===", flush=True)
with httpx.Client(follow_redirects=False, timeout=30) as cl:
for url in [
"https://dy.nomadro.com/api/health",
"https://dy.nomadro.com/",
"https://dy.nomadro.com/login",
]:
try:
r = cl.get(url)
print(f"{url} -> {r.status_code} loc={r.headers.get('location','')[:60]}", flush=True)
except Exception as ex:
print(f"{url} ERR {ex}", flush=True)
print("DONE", flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())