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

137 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""Continue dy.nomadro.com recovery: try alt WG port, else Caddy via 10.9.0.2 if reachable.
No Neko. Live flush.
"""
from __future__ import annotations
import sys
import time
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
def connect(port=22, user="root", pw="Xiao4669805"):
for i in range(10):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
port,
user,
pw,
timeout=25,
banner_timeout=40,
allow_agent=False,
look_for_keys=False,
)
print(f"connected :{port}", flush=True)
return c
except Exception as ex:
print(f"retry :{port} {i} {ex}", flush=True)
time.sleep(2)
raise RuntimeError("ssh fail")
def run(c, cmd, t=90):
print(f"$ {cmd[:220]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
while not o.channel.exit_status_ready():
if o.channel.recv_ready():
sys.stdout.write(o.channel.recv(4096).decode("utf-8", "replace"))
sys.stdout.flush()
if o.channel.recv_stderr_ready():
sys.stdout.write(o.channel.recv_stderr(4096).decode("utf-8", "replace"))
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write((o.read() + e.read()).decode("utf-8", "replace"))
sys.stdout.flush()
print("->", o.channel.recv_exit_status(), flush=True)
print("## 1 inventory WG + caddy site", flush=True)
v = connect(22)
run(
v,
"ls -la /etc/wireguard/; "
"systemctl is-active 'wg-quick@wg-dy' 'wg-quick@wg-dy-cn' 2>/dev/null; "
"wg show; "
"ls /etc/caddy/sites/ | head -40; "
"grep -n 'reverse_proxy\\|dy.nomadro' /etc/caddy/sites/dy.nomadro.com.caddy 2>/dev/null | head -30 || "
"ls /etc/caddy/sites/dy* 2>/dev/null",
)
v.close()
print("## 2 try ListenPort 443/udp for wg-dy (bypass common 51820 block)", flush=True)
v = connect(22)
run(
v,
"cp -a /etc/wireguard/wg-dy.conf /etc/wireguard/wg-dy.conf.bak.pre443; "
"sed -i 's/^ListenPort = .*/ListenPort = 443/' /etc/wireguard/wg-dy.conf; "
"grep -E 'ListenPort|Address|PublicKey|AllowedIPs' /etc/wireguard/wg-dy.conf; "
"ufw allow 443/udp comment 'wg-dy-alt' 2>/dev/null || true; "
"iptables -C INPUT -p udp --dport 443 -j ACCEPT 2>/dev/null || iptables -I INPUT -p udp --dport 443 -j ACCEPT; "
"wg-quick down wg-dy; wg-quick up wg-dy; "
"ss -ulnp | grep -E '443|51820' || true; "
"wg show wg-dy",
)
v.close()
print("## 3 guest point Endpoint to :443", flush=True)
g = connect(4014, "eric", "4669805")
run(
g,
"echo 4669805 | sudo -S -p '' bash -lc '"
"cp -a /etc/wireguard/wg-dy.conf /etc/wireguard/wg-dy.conf.bak.pre443; "
"sed -i \"s/^Endpoint = .*/Endpoint = 107.173.30.245:443/\" /etc/wireguard/wg-dy.conf; "
"grep -E \"Endpoint|Address|PublicKey|AllowedIPs|Keepalive\" /etc/wireguard/wg-dy.conf; "
"wg-quick down wg-dy; wg-quick up wg-dy; sleep 4; "
"wg show wg-dy; ping -c 4 -W 2 10.8.0.1 | tail -6"
"'",
t=50,
)
g.close()
print("## 4 VPS check handshake", flush=True)
time.sleep(2)
v = connect(22)
run(
v,
"wg show wg-dy; ping -c 3 -W 2 10.8.0.2 | tail -5; "
"curl -sS -m 8 -o /dev/null -w via_wg=%{http_code}\\\\n http://10.8.0.2:8014/api/health",
t=30,
)
v.close()
# If still dead, try Caddy via 10.9.0.2 through existing path
print("## 5 if still down, check reachability of 10.9.0.2 from VPS", flush=True)
v = connect(22)
run(
v,
"ping -c 2 -W 2 10.9.0.2 | tail -4; "
"curl -sS -m 5 -o /dev/null -w 'cn_api=%{http_code}\\\\n' http://10.9.0.2:8014/api/health; "
"ip route | grep -E '10\\.8\\.|10\\.9\\.' || true; "
"test -f /etc/wireguard/wg-dy-cn.conf && cat /etc/wireguard/wg-dy-cn.conf | sed -n '1,20p' || echo no_wg_dy_cn_conf",
t=25,
)
v.close()
print("## public", flush=True)
with httpx.Client(follow_redirects=False, timeout=15) as cl:
for u in [
"https://dy.nomadro.com/api/health",
"https://dy.nomadro.com/",
"https://dy.nomadro.cn/api/health",
"https://dylive.nomadro.com/api/health",
]:
try:
r = cl.get(u)
print(f"{u} -> {r.status_code}", flush=True)
except Exception as ex:
print(f"{u} ERR {ex}", flush=True)
print("DONE", flush=True)