douyinWEB/scripts/_finish_dual_edge_neko.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

166 lines
5.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Finish dual-edge Neko WebRTC alignment."""
from __future__ import annotations
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
OVERSEAS = ("107.173.30.245", 22, "root", "Xiao4669805")
DOMESTIC = ("82.157.112.245", 22, "ubuntu", "Xiao4669805")
GUEST = ("107.173.30.245", 4014, "eric", "4669805")
LEGACY = "10.8.0.2"
CN = "10.9.0.2"
NAT1TO1 = "107.173.30.245"
def ssh(host, port, user, pw):
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)
return c
def run(c, cmd, timeout=120):
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
def dnat_bash(target: str, wg_if: str) -> str:
return f"""#!/bin/bash
set -euo pipefail
TARGET="{target}"
WG_IF="{wg_if}"
PUB_IF=$(ip route get 1.1.1.1 | awk '{{for(i=1;i<=NF;i++) if($i=="dev"){{print $(i+1); exit}}}}')
echo "TARGET=$TARGET WG=$WG_IF PUB=$PUB_IF"
sysctl -w net.ipv4.ip_forward=1 >/dev/null
iptables-save -t nat 2>/dev/null | grep -F 'NEKO-WEBRTC' | sed 's/^-A /-D /' | while read -r line; do
iptables -t nat $line 2>/dev/null || true
done
iptables-save 2>/dev/null | grep -F 'NEKO-WEBRTC' | sed 's/^-A /-D /' | while read -r line; do
iptables $line 2>/dev/null || true
done
for p in $(seq 52300 52303); do
iptables -t nat -A PREROUTING -i "$PUB_IF" -p tcp --dport "$p" -m comment --comment NEKO-WEBRTC -j DNAT --to-destination "$TARGET:$p"
iptables -t nat -A PREROUTING -i "$PUB_IF" -p udp --dport "$p" -m comment --comment NEKO-WEBRTC -j DNAT --to-destination "$TARGET:$p"
done
for r in 52000:52098 52101:52199 52201:52299; do
iptables -t nat -A PREROUTING -i "$PUB_IF" -p udp -m multiport --dports "$r" -m comment --comment NEKO-WEBRTC -j DNAT --to-destination "$TARGET"
done
iptables -t nat -A POSTROUTING -o "$WG_IF" -d "$TARGET" -m comment --comment NEKO-WEBRTC -j MASQUERADE
iptables -C FORWARD -i "$PUB_IF" -o "$WG_IF" -d "$TARGET" -m comment --comment NEKO-WEBRTC -j ACCEPT 2>/dev/null || \
iptables -A FORWARD -i "$PUB_IF" -o "$WG_IF" -d "$TARGET" -m comment --comment NEKO-WEBRTC -j ACCEPT
iptables -C FORWARD -i "$WG_IF" -o "$PUB_IF" -s "$TARGET" -m comment --comment NEKO-WEBRTC -j ACCEPT 2>/dev/null || \
iptables -A FORWARD -i "$WG_IF" -o "$PUB_IF" -s "$TARGET" -m comment --comment NEKO-WEBRTC -j ACCEPT
install -m 755 /dev/stdin /etc/iptables-neko-webrtc.sh <<EOF
#!/bin/bash
# dual-edge neko webrtc target=$TARGET wg=$WG_IF
bash /etc/iptables-neko-webrtc-apply.sh
EOF
# keep apply copy
cp -a "$0" /etc/iptables-neko-webrtc-apply.sh 2>/dev/null || true
if command -v netfilter-persistent >/dev/null; then
netfilter-persistent save 2>/dev/null || true
fi
echo '=== NEKO NAT ==='
iptables -t nat -S PREROUTING | grep NEKO | head -12
timeout 2 bash -c "echo >/dev/tcp/$TARGET/52300" && echo TCP52300_OK || echo TCP52300_FAIL
"""
def put_run(c, script: str, sudo: bool = False) -> str:
sftp = c.open_sftp()
with sftp.file("/tmp/neko-webrtc-dnat.sh", "w") as f:
f.write(script)
sftp.close()
if sudo:
return run(c, "sudo bash /tmp/neko-webrtc-dnat.sh", 90)
return run(c, "bash /tmp/neko-webrtc-dnat.sh", 90)
def fix_physical_env(g) -> None:
print("=== fix physical NEKO env ===", flush=True)
fixer = f"""#!/bin/bash
set -euo pipefail
SVC=/etc/systemd/system/douyinweb-api.service
cp -a "$SVC" "$SVC.bak-dual-$(date +%s)"
sed -i '/Environment=Environment=/d' "$SVC"
sed -i '/DY_NEKO_EDGE_UPSTREAM=/d' "$SVC"
sed -i '/DY_NEKO_NAT1TO1=/d' "$SVC"
sed -i '/\\[Service\\]/a Environment=DY_NEKO_EDGE_UPSTREAM={LEGACY}' "$SVC"
sed -i '/\\[Service\\]/a Environment=DY_NEKO_NAT1TO1={NAT1TO1}' "$SVC"
awk 'BEGIN{{c=0}} /Environment=DY_CORS_ORIGINS=/{{c++; if(c>1) next}} {{print}}' "$SVC" > /tmp/douyinweb-api.service.fixed
mv /tmp/douyinweb-api.service.fixed "$SVC"
systemctl daemon-reload
systemctl restart douyinweb-api
sleep 2
systemctl is-active douyinweb-api
grep -E 'DY_NEKO|DY_CORS|DY_PUBLIC' "$SVC" | head -20
"""
sftp = g.open_sftp()
with sftp.file("/tmp/fix_neko_env.sh", "w") as f:
f.write(fixer)
sftp.close()
print(run(g, "echo '4669805' | sudo -S -p '' bash /tmp/fix_neko_env.sh", 90), flush=True)
def main() -> int:
g = ssh(*GUEST)
fix_physical_env(g)
g.close()
print("=== overseas DNAT → 10.8.0.2 ===", flush=True)
v = ssh(*OVERSEAS)
print(put_run(v, dnat_bash(LEGACY, "wg-dy")), flush=True)
print(run(v, "curl -sS -m 15 -o /dev/null -w 'dylive=%{http_code}\\n' https://dylive.nomadro.com/api/health", 25), flush=True)
v.close()
print("=== Tencent UFW + DNAT → 10.9.0.2 ===", flush=True)
d = ssh(*DOMESTIC)
print(
run(
d,
"sudo bash -c '"
"ufw allow 52300:52303/tcp; ufw allow 52300:52303/udp; "
"ufw allow 52000:52098/udp; ufw allow 52101:52199/udp; ufw allow 52201:52299/udp; "
"ufw status | grep -E \"5230|5200|5210|5220\" | head -20'",
40,
),
flush=True,
)
print(put_run(d, dnat_bash(CN, "wg-dy-cn"), sudo=True), flush=True)
d.close()
print("=== verify public ===", flush=True)
import httpx
with httpx.Client(follow_redirects=False, timeout=20) as client:
for url in [
"https://dylive.nomadro.com/api/health",
"https://dy.nomadro.com/api/health",
"https://dy.nomadro.com/neko/1/",
"https://dy.nomadro.cn/api/health",
"https://dy.nomadro.cn/neko/1/",
"https://dy.nomadro.cn/",
]:
try:
r = client.get(url)
print(f"{url} -> {r.status_code} loc={r.headers.get('location','')}", 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())