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

133 lines
4.6 KiB
Python

# -*- coding: utf-8 -*-
"""Fix phys Neko: restore wg-dy-cn, verify ports/API, soft-refresh NAT if needed. No switch/data changes."""
from __future__ import annotations
import json
import sys
import time
import http.cookiejar as cookiejar
import urllib.request
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
def ssh(host, port, user, pw, sudo=False):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=45, allow_agent=False, look_for_keys=False)
def run(cmd, t=120):
if sudo:
cmd = f"echo '{pw}' | sudo -S -p '' bash -lc {repr(cmd)}"
_, o, e = c.exec_command(cmd, timeout=t)
return (o.read() + e.read()).decode("utf-8", "replace")
return c, run
print("===== PHYS =====", flush=True)
p, pr = ssh("107.173.30.245", 4014, "eric", "4669805", sudo=True)
print("--- before wg ---", flush=True)
print(pr(
"wg show wg-dy-cn | egrep 'endpoint|handshake|transfer'; "
"wg show wg-dy | egrep 'endpoint|handshake|transfer'; "
"systemctl is-active 'wg-quick@wg-dy-cn' 'wg-quick@wg-dy' douyinweb-api douyinweb-wireguard-watch 2>/dev/null"
))
# Refresh CN WG only (Neko ICE depends on it). Do NOT touch sing-box/frpp.
print("--- bounce wg-dy-cn ---", flush=True)
print(pr(
"systemctl restart 'wg-quick@wg-dy-cn'; "
"sleep 3; "
"wg show wg-dy-cn | egrep 'endpoint|latest handshake|transfer'; "
"ping -c 2 -W 2 10.9.0.1 2>&1 | tail -5"
))
# Also try overseas wg-dy (FRP fallback path health); if down, restart once
print("--- bounce wg-dy if dead ---", flush=True)
print(pr(
"rx=$(wg show wg-dy transfer 2>/dev/null | awk '{print $2}'); "
"echo wg_dy_rx=$rx; "
"if [ -z \"$rx\" ] || [ \"$rx\" = \"0\" ]; then "
" systemctl restart 'wg-quick@wg-dy'; sleep 3; "
" wg show wg-dy | egrep 'handshake|transfer'; "
" ping -c 1 -W 2 10.8.0.1 2>&1 | tail -2; "
"fi"
))
print("--- local neko ports ---", flush=True)
print(pr(
"for p in 9200 9201 9202 9203 9204 9205 9206 9207 18200 18201; do "
" code=$(curl -sS -m 2 -o /dev/null -w '%{http_code}' http://127.0.0.1:$p/ || echo x); "
" echo $p:$code; "
"done; "
"ss -ltn | grep -E ':920|:1820|:5230|:623' | head -30"
))
print("--- api health ---", flush=True)
print(pr(
"curl -sS -m 5 http://127.0.0.1:8014/api/health | head -c 220; echo; "
"ss -ltn sport = :8014; "
"systemctl is-active douyinweb-api"
))
# Soft: if CN still dead after bounce, force VPS NAT on running containers WITHOUT recreate
# by rewriting neko server env file if present and restarting only neko process — prefer CN restore first.
print("--- cn_edge after ---", flush=True)
print(pr(
"python3 - <<'PY'\n"
"import os,time,subprocess\n"
"def hs(iface):\n"
" out=subprocess.getoutput(f'wg show {iface} latest-handshakes')\n"
" print(iface, out)\n"
" try:\n"
" ts=int(out.split()[-1])\n"
" print(' age', int(time.time())-ts if ts else 'none')\n"
" except Exception as e:\n"
" print(' parse', e)\n"
"hs('wg-dy-cn'); hs('wg-dy')\n"
"PY"
))
# Soft restart neko server inside containers to pick network — NO chrome profile wipe
# Only restart `neko` supervisord program if exists
print("--- soft neko server bounce (no profile wipe) ---", flush=True)
print(pr(
"for N in $(docker ps --format '{{.Names}}' | grep -E 'douyinweb-(neko|ops-neko)-'); do "
" echo == $N ==; "
" docker exec $N supervisorctl restart neko 2>/dev/null || "
" docker exec $N supervisorctl restart neko-server 2>/dev/null || "
" echo no_neko_svc; "
"done | head -80"
))
p.close()
print("===== VPS edge =====", flush=True)
v, vr = ssh("107.173.30.245", 22, "root", "Xiao4669805")
print(vr(
"systemctl is-active dy-frp-http-aux frpp caddy; "
"curl -sS -m 4 -o /dev/null -w 'frp9200:%{http_code}\\n' http://127.0.0.1:9200/; "
"curl -sS -m 4 -o /dev/null -w 'frp19200:%{http_code}\\n' http://127.0.0.1:19200/; "
"curl -sS -m 4 -o /dev/null -w 'frp18200:%{http_code}\\n' http://127.0.0.1:18200/; "
"wg show wg-dy 2>/dev/null | egrep 'handshake|transfer' | head -5; "
"ping -c1 -W2 10.8.0.2 2>&1 | tail -2"
))
v.close()
print("===== public ====", flush=True)
for u in [
"https://dy.nomadro.com/neko/1/",
"https://dy.nomadro.com/neko/3/",
"https://dy.nomadro.com/ops-neko/1/",
"https://dy.nomadro.cn/neko/1/",
"https://dy.nomadro.cn/ops-neko/1/",
]:
try:
with urllib.request.urlopen(u, timeout=15) as r:
print(u, r.status, r.read(60)[:60])
except Exception as ex:
print(u, "ERR", ex)