douyinWEB/scripts/_fix_neko_webrtc_forward.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

107 lines
3.4 KiB
Python

# -*- coding: utf-8 -*-
"""Install iptables DNAT on VPS: public WebRTC ports -> PVE via wg-dy-cn (primary)."""
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
VPS = ("107.173.30.245", 22, "root", "Xiao4669805")
PVE_WG = "10.8.0.2" # legacy wg-dy; switch to 10.9.0.2 when wg-dy-cn is healthy
REMOTE = rf"""#!/bin/bash
set -euo pipefail
WG_IF=$(ip -o link show type wireguard | awk -F': ' '{{print $2}}' | head -1)
PUB_IF=$(ip route get 1.1.1.1 | awk '{{for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}}')
echo "WG=$WG_IF PUB=$PUB_IF"
sysctl -w net.ipv4.ip_forward=1
MARK=/etc/iptables-neko-webrtc.sh
cat > "$MARK" <<'EOF'
#!/bin/bash
# Neko WebRTC: VPS public -> physical via WireGuard (dy.nomadro.com physical deploy)
WG_IF="${{NEKO_WG_IF:-}}"
if [ -z "$WG_IF" ]; then
if ip link show wg-dy-cn >/dev/null 2>&1 && [[ "{PVE_WG}" == 10.9.* ]]; then
WG_IF=wg-dy-cn
elif ip link show wg-dy >/dev/null 2>&1; then
WG_IF=wg-dy
else
WG_IF=$(ip -o link show type wireguard | awk -F': ' '{{print $2}}' | head -1)
fi
fi
PUB_IF=$(ip route get 1.1.1.1 | awk '{{for(i=1;i<=NF;i++) if($i=="dev") print $(i+1)}}')
TARGET="{PVE_WG}"
# Remove stale neko rules (idempotent)
iptables-save -t nat | grep -F 'NEKO-WEBRTC' | sed 's/^-A/-D/' | while read -r line; do
iptables -t nat $line 2>/dev/null || true
done
iptables-save | grep -F 'NEKO-WEBRTC' | sed 's/^-A/-D/' | while read -r line; do
iptables $line 2>/dev/null || true
done
add_dnat() {{
local proto=$1 port=$2
iptables -t nat -A PREROUTING -i "$PUB_IF" -p "$proto" --dport "$port" -m comment --comment NEKO-WEBRTC \
-j DNAT --to-destination "$TARGET:$port"
}}
add_dnat_range() {{
local proto=$1 range=$2
iptables -t nat -A PREROUTING -i "$PUB_IF" -p "$proto" -m multiport --dports "$range" -m comment --comment NEKO-WEBRTC \
-j DNAT --to-destination "$TARGET"
}}
# mux TCP/UDP per instance (up to 4)
for p in $(seq 52300 52303); do
add_dnat tcp "$p"
add_dnat udp "$p"
done
# UDP EPR ranges (instances 1-3)
for r in 52000:52098 52101:52199 52201:52299; do
add_dnat_range udp "$r"
done
# SNAT return traffic so clients see VPS public IP
iptables -t nat -A POSTROUTING -o "$WG_IF" -d "$TARGET" -m comment --comment NEKO-WEBRTC -j MASQUERADE
# FORWARD
iptables -A FORWARD -i "$PUB_IF" -o "$WG_IF" -d "$TARGET" -m comment --comment NEKO-WEBRTC -j ACCEPT
iptables -A FORWARD -i "$WG_IF" -o "$PUB_IF" -s "$TARGET" -m comment --comment NEKO-WEBRTC -j ACCEPT
EOF
chmod +x "$MARK"
bash "$MARK"
echo '=== nat rules ==='
iptables -t nat -L PREROUTING -n -v | grep NEKO || true
iptables -t nat -L POSTROUTING -n -v | grep NEKO || true
iptables -L FORWARD -n -v | grep NEKO || true
# persist if netfilter-persistent exists
if command -v netfilter-persistent >/dev/null; then
netfilter-persistent save || true
fi
# quick probe via wg
echo '=== probe {PVE_WG}:52300 from VPS ==='
timeout 2 bash -c 'echo >/dev/tcp/{PVE_WG}/52300' && echo WG_TCP52300_OK || echo WG_TCP52300_FAIL
"""
def main():
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(*VPS[:3], VPS[3], timeout=25, allow_agent=False, look_for_keys=False)
_, o, e = c.exec_command(REMOTE, timeout=60)
print(o.read().decode("utf-8", "replace"))
err = e.read().decode("utf-8", "replace")
if err.strip():
print("STDERR:", err)
c.close()
if __name__ == "__main__":
main()