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

161 lines
5.4 KiB
Python

#!/usr/bin/env python3
"""Fix dy.nomadro.com 502 + dylive.nomadro.com invalid response."""
from __future__ import annotations
import sys
import time
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
VPS_HOST = "107.173.30.245"
VPS_PASS = "Xiao4669805"
GUEST_PASS = "4669805"
def ssh(host, port, user, pw, tries=12):
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,
)
print(f"ssh {host}:{port} ok try={i}", flush=True)
return c
except Exception as ex:
last = ex
print(f"ssh {host}:{port} retry {i} {type(ex).__name__}: {ex}", flush=True)
time.sleep(4)
raise RuntimeError(f"ssh fail {host}:{port}: {last}")
def run(c, cmd, t=120, label=""):
print(f"\n[{label}]$ {cmd[:250]}", 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[-7000:] if len(out) > 7000 else out, flush=True)
print(f"[{label}]→{code}", flush=True)
return code, out
def probe():
print("\n=== PUBLIC PROBE ===", flush=True)
with httpx.Client(follow_redirects=False, timeout=25) as cl:
for url in [
"https://dy.nomadro.com/api/health",
"https://dy.nomadro.com/",
"https://dylive.nomadro.com/api/health",
"https://dylive.nomadro.com/youtube",
"https://dylive.nomadro.com/",
]:
try:
r = cl.get(url)
print(
f"{url} -> {r.status_code} ctype={r.headers.get('content-type','')[:40]} "
f"loc={r.headers.get('location','')[:50]} body={r.text[:80]!r}",
flush=True,
)
except Exception as ex:
print(f"{url} ERR {ex}", flush=True)
def main() -> int:
probe()
v = ssh(VPS_HOST, 22, "root", VPS_PASS)
try:
# --- dylive local stack ---
run(
v,
"uptime; free -h | head -2; "
"systemctl restart caddy; "
"systemctl restart douyinweb-pb douyinweb-yt douyinweb-api douyinweb-web; "
"sleep 4; "
"systemctl is-active caddy douyinweb-api douyinweb-web douyinweb-yt douyinweb-pb nginx; "
"curl -sS -m 12 http://127.0.0.1:8014/api/health | head -c 250; echo; "
"curl -sS -m 12 -o /dev/null -w 'local_web=%{http_code}\\n' http://127.0.0.1:4015/; "
"curl -sS -m 12 -o /dev/null -w 'local_yt=%{http_code}\\n' http://127.0.0.1:4015/youtube",
t=90,
label="dylive-restart",
)
# --- WG for dy.nomadro.com ---
run(
v,
"systemctl start frpp nginx 'wg-quick@wg-dy' 2>/dev/null; "
"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 2 10.8.0.2 | tail -5 || true",
t=60,
label="wg-bounce-vps",
)
finally:
v.close()
# Physical guest via FRP
try:
g = ssh(VPS_HOST, 4014, "eric", GUEST_PASS)
try:
run(
g,
f"echo '{GUEST_PASS}' | sudo -S -p '' bash -lc '"
"wg-quick down wg-dy 2>/dev/null || true; "
"wg-quick up wg-dy; sleep 2; "
"wg show wg-dy | head -15; "
"ping -c 3 -W 2 10.8.0.1 | tail -4; "
"systemctl restart douyinweb-api douyinweb-web 2>/dev/null || true; "
"sleep 3; "
"systemctl is-active douyinweb-api douyinweb-web; "
"curl -sS -m 10 http://127.0.0.1:8014/api/health | head -c 200; echo; "
"curl -sS -m 10 -o /dev/null -w local_web=%{http_code}\\\\n http://127.0.0.1:4015/"
"'",
t=120,
label="guest-fix",
)
finally:
g.close()
except Exception as ex:
print(f"guest FRP failed: {ex}", flush=True)
# Re-check tunnel from VPS
v = ssh(VPS_HOST, 22, "root", VPS_PASS)
try:
run(
v,
"wg show wg-dy | head -18; "
"ping -c 4 -W 2 10.8.0.2 | tail -5; "
"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/; "
"systemctl reload caddy 2>/dev/null || systemctl restart caddy; "
"sleep 1; "
"curl -sS -m 15 -o /dev/null -w 'caddy_dy=%{http_code}\\n' -H 'Host: dy.nomadro.com' https://127.0.0.1/api/health -k; "
"curl -sS -m 15 -o /dev/null -w 'caddy_dylive=%{http_code}\\n' -H 'Host: dylive.nomadro.com' https://127.0.0.1/api/health -k",
t=90,
label="final-tunnel",
)
finally:
v.close()
time.sleep(2)
probe()
print("\nDONE fix", flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())