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

87 lines
3.1 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Force-apply Tencent Neko DNAT without set -e early exit."""
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
SCRIPT = r"""#!/bin/bash
TARGET=10.9.0.2
WG_IF=wg-dy-cn
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
# clear old
while iptables-save -t nat | grep -q 'NEKO-WEBRTC'; do
line=$(iptables-save -t nat | grep 'NEKO-WEBRTC' | head -1 | sed 's/^-A /-D /')
echo "DEL nat: $line"
iptables -t nat $line || break
done
while iptables-save | grep -q 'NEKO-WEBRTC'; do
line=$(iptables-save | grep 'NEKO-WEBRTC' | head -1 | sed 's/^-A /-D /')
echo "DEL filter: $line"
iptables $line || break
done
add() { echo "+ $*"; iptables "$@" || echo "FAIL: $*"; }
for p in 52300 52301 52302 52303; do
add -t nat -A PREROUTING -i "$PUB_IF" -p tcp --dport "$p" -m comment --comment NEKO-WEBRTC -j DNAT --to-destination "$TARGET:$p"
add -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
add -t nat -A PREROUTING -i "$PUB_IF" -p udp -m multiport --dports "$r" -m comment --comment NEKO-WEBRTC -j DNAT --to-destination "$TARGET"
done
add -t nat -A POSTROUTING -o "$WG_IF" -d "$TARGET" -m comment --comment NEKO-WEBRTC -j MASQUERADE
add -A FORWARD -i "$PUB_IF" -o "$WG_IF" -d "$TARGET" -m comment --comment NEKO-WEBRTC -j ACCEPT
add -A FORWARD -i "$WG_IF" -o "$PUB_IF" -s "$TARGET" -m comment --comment NEKO-WEBRTC -j ACCEPT
# persist via systemd oneshot if possible
cat >/etc/iptables-neko-webrtc-apply.sh <<EOF
#!/bin/bash
# re-apply dual-edge CN neko DNAT
bash $0
EOF
# actually save a standalone copy of this applied state via iptables-save snippet
iptables-save | grep NEKO-WEBRTC > /etc/iptables-neko-webrtc.rules || true
chmod +x /etc/iptables-neko-webrtc-apply.sh 2>/dev/null || true
# systemd reapply on boot
cat >/etc/systemd/system/neko-webrtc-dnat.service <<'UNIT'
[Unit]
Description=Neko WebRTC DNAT via wg-dy-cn
After=network-online.target wg-quick@wg-dy-cn.service
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/bin/bash /tmp/neko-webrtc-dnat.sh
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
UNIT
# keep script durable
cp -a /tmp/neko-webrtc-dnat.sh /usr/local/sbin/neko-webrtc-dnat.sh
sed -i 's|ExecStart=.*|ExecStart=/bin/bash /usr/local/sbin/neko-webrtc-dnat.sh|' /etc/systemd/system/neko-webrtc-dnat.service
systemctl daemon-reload
systemctl enable neko-webrtc-dnat.service
echo '=== RESULT ==='
iptables -t nat -S | grep NEKO | head -20
iptables -S | grep NEKO | head -10
"""
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("82.157.112.245", 22, "ubuntu", "Xiao4669805", timeout=45, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
with sftp.file("/tmp/neko-webrtc-dnat.sh", "w") as f:
f.write(SCRIPT)
sftp.close()
_, o, e = c.exec_command("sudo bash /tmp/neko-webrtc-dnat.sh", timeout=90)
print((o.read() + e.read()).decode())
c.close()