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

165 lines
5.1 KiB
Python

#!/usr/bin/env python3
"""Continue: verify sites, try WG, else FRP-aux for Neko ports too. No Neko start."""
from __future__ import annotations
import sys
import time
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
HOST = "107.173.30.245"
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(
HOST,
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=120):
print(f"$ {cmd[:240]}", 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("## public now", flush=True)
with httpx.Client(follow_redirects=False, timeout=15) as cl:
for u in [
"https://dylive.nomadro.com/api/health",
"https://dy.nomadro.com/api/health",
"https://dy.nomadro.com/",
"https://dy.nomadro.cn/api/health",
"https://dy.nomadro.com/neko/1/",
]:
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("## WG status + try reach 10.9.0.2", flush=True)
v = connect(22)
run(
v,
"systemctl is-active dy-frp-http-aux caddy douyinweb-api 'wg-quick@wg-dy' 'wg-quick@wg-dy-cn'; "
"wg show; "
"ping -c 2 -W 2 10.8.0.2 | tail -3; "
"ping -c 2 -W 2 10.9.0.2 | tail -3; "
"curl -sS -m 5 -o /dev/null -w 'wg8=%{http_code} cn9=%{http_code}\\n' "
"http://10.8.0.2:8014/api/health http://10.9.0.2:8014/api/health 2>/dev/null || "
"curl -sS -m 5 -o /dev/null -w wg8=%{http_code}\\\\n http://10.8.0.2:8014/api/health; "
"curl -sS -m 5 -o /dev/null -w cn9=%{http_code}\\\\n http://10.9.0.2:8014/api/health; "
"cat /etc/wireguard/wg-dy-cn.conf",
t=40,
)
v.close()
print("## extend FRP-aux to Neko 9200-9207 (still do NOT start containers)", flush=True)
v = connect(22)
run(
v,
r'''
set -e
# rebuild unit with api/web + neko 1..8
FORWARDS="-L 127.0.0.1:18014:127.0.0.1:8014 -L 127.0.0.1:14015:127.0.0.1:4015"
for p in $(seq 9200 9207); do
FORWARDS="$FORWARDS -L 127.0.0.1:$p:127.0.0.1:$p"
done
cat >/etc/systemd/system/dy-frp-http-aux.service <<EOF
[Unit]
Description=dy.nomadro.com FRP-aux tunnels (HTTP+Neko ports via :4014)
After=network-online.target frpp.service
Wants=network-online.target
[Service]
Type=simple
Restart=always
RestartSec=3
Environment=SSHPASS=4669805
ExecStart=/usr/bin/sshpass -e ssh -N -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes $FORWARDS -p 4014 eric@127.0.0.1
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl restart dy-frp-http-aux.service
sleep 3
systemctl is-active dy-frp-http-aux
ss -lntp | grep -E ':(18014|14015|9200|9201|9202|9203|9204|9205|9206|9207)\b' | head -20
curl -sS -m 6 http://127.0.0.1:18014/api/health | head -c 100; echo
# rewrite remaining 10.8.0.2:920x -> 127.0.0.1:920x in caddy site
SITE=/etc/caddy/sites/dy.nomadro.com.caddy
cp -a "$SITE" "$SITE.bak.nekofrp.$(date +%s)"
python3 - <<'PY'
from pathlib import Path
p = Path("/etc/caddy/sites/dy.nomadro.com.caddy")
t = p.read_text(encoding="utf-8")
n = 0
for port in range(9200, 9208):
a = f"reverse_proxy 10.8.0.2:{port}"
b = f"reverse_proxy 127.0.0.1:{port}"
if a in t:
t = t.replace(a, b)
n += 1
p.write_text(t, encoding="utf-8")
print("neko_proxy_rewrites", n)
PY
caddy validate --config /etc/caddy/Caddyfile >/tmp/caddyval.txt 2>&1 && echo caddy_ok || (tail -20 /tmp/caddyval.txt; exit 1)
systemctl reload caddy
systemctl is-active caddy
''',
t=90,
)
v.close()
print("## public after", 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.com/neko/1/",
"https://dy.nomadro.com/neko/7/",
"https://dylive.nomadro.com/api/health",
"https://dy.nomadro.cn/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)