# -*- coding: utf-8 -*- """Open WebRTC DNAT on Tencent (+ overseas backup) for neko 1..16 incl. 5/6. NAT1TO1 is 82.157.112.245 → clients send ICE to Tencent; must DNAT to 10.9.0.2. """ from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") # Durable apply script installed on both edges APPLY = r'''#!/bin/bash set -u COMMENT=NEKO-WEBRTC TARGET="${NEKO_DNAT_TARGET:-10.9.0.2}" WG_IF="${NEKO_DNAT_WG:-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 "[neko-dnat] TARGET=$TARGET WG=$WG_IF PUB=$PUB_IF" sysctl -w net.ipv4.ip_forward=1 >/dev/null # wipe previous NEKO-WEBRTC rules while iptables-save -t nat 2>/dev/null | grep -q "$COMMENT"; do line=$(iptables-save -t nat | grep "$COMMENT" | head -1 | sed 's/^-A /-D /') iptables -t nat $line || break done while iptables-save 2>/dev/null | grep -q "$COMMENT"; do line=$(iptables-save | grep "$COMMENT" | head -1 | sed 's/^-A /-D /') iptables $line || break done add() { iptables "$@" || echo "FAIL: $*"; } # mux TCP+UDP continuous range for instances 1..64 (assign fleet) add -t nat -A PREROUTING -i "$PUB_IF" -p tcp --dport 52300:52363 -m comment --comment "$COMMENT" -j DNAT --to-destination "$TARGET" add -t nat -A PREROUTING -i "$PUB_IF" -p udp --dport 52300:52363 -m comment --comment "$COMMENT" -j DNAT --to-destination "$TARGET" # ops-neko tab fleet mux + EPR (62300+/62500+) — same NAT1TO1 Tencent IP add -t nat -A PREROUTING -i "$PUB_IF" -p tcp --dport 62300:62307 -m comment --comment "$COMMENT" -j DNAT --to-destination "$TARGET" add -t nat -A PREROUTING -i "$PUB_IF" -p udp --dport 62300:62307 -m comment --comment "$COMMENT" -j DNAT --to-destination "$TARGET" add -t nat -A PREROUTING -i "$PUB_IF" -p udp --dport 62500:62579 -m comment --comment "$COMMENT" -j DNAT --to-destination "$TARGET" # EPR UDP ranges (youtube_neko._udp_range) for r in 52000:52098 52101:52199 52201:52299 52400:53999; do add -t nat -A PREROUTING -i "$PUB_IF" -p udp --dport "$r" -m comment --comment "$COMMENT" -j DNAT --to-destination "$TARGET" done add -t nat -A POSTROUTING -o "$WG_IF" -d "$TARGET" -m comment --comment "$COMMENT" -j MASQUERADE add -A FORWARD -i "$PUB_IF" -o "$WG_IF" -d "$TARGET" -m comment --comment "$COMMENT" -j ACCEPT add -A FORWARD -i "$WG_IF" -o "$PUB_IF" -s "$TARGET" -m comment --comment "$COMMENT" -j ACCEPT # host firewall if command -v ufw >/dev/null 2>&1; then ufw allow 52300:52363/tcp >/dev/null 2>&1 || true ufw allow 52300:52363/udp >/dev/null 2>&1 || true ufw allow 62300:62307/tcp >/dev/null 2>&1 || true ufw allow 62300:62307/udp >/dev/null 2>&1 || true ufw allow 62500:62579/udp >/dev/null 2>&1 || true ufw allow 52000:52098/udp >/dev/null 2>&1 || true ufw allow 52101:52199/udp >/dev/null 2>&1 || true ufw allow 52201:52299/udp >/dev/null 2>&1 || true ufw allow 52400:53999/udp >/dev/null 2>&1 || true fi echo "[neko-dnat] rules:" iptables -t nat -S | grep "$COMMENT" | wc -l iptables -t nat -S | grep "$COMMENT" | head -20 ''' def ssh(h, p, u, pw): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(h, p, u, pw, timeout=60, allow_agent=False, look_for_keys=False) return c def run(c, cmd, t=120): _, o, e = c.exec_command(cmd, timeout=t) return (o.read() + e.read()).decode("utf-8", "replace") def deploy_edge(c, *, target: str, wg: str, label: str, use_sudo: bool) -> None: print(f"=== {label} → {target} via {wg} ===", flush=True) sftp = c.open_sftp() with sftp.file("/tmp/neko-webrtc-dnat.sh", "w") as f: f.write(APPLY) sftp.close() env = f"NEKO_DNAT_TARGET={target} NEKO_DNAT_WG={wg}" if use_sudo: cmd = ( f"sudo bash -lc 'cp /tmp/neko-webrtc-dnat.sh /usr/local/sbin/neko-webrtc-dnat.sh; " f"chmod +x /usr/local/sbin/neko-webrtc-dnat.sh; " f"{env} /usr/local/sbin/neko-webrtc-dnat.sh; " "cat >/etc/systemd/system/neko-webrtc-dnat.service <<\"UNIT\"\n" "[Unit]\nDescription=Neko WebRTC DNAT\nAfter=network-online.target\nWants=network-online.target\n\n" "[Service]\nType=oneshot\n" f"Environment=NEKO_DNAT_TARGET={target}\nEnvironment=NEKO_DNAT_WG={wg}\n" "ExecStart=/usr/local/sbin/neko-webrtc-dnat.sh\nRemainAfterExit=yes\n\n" "[Install]\nWantedBy=multi-user.target\nUNIT\n" "systemctl daemon-reload; systemctl enable neko-webrtc-dnat.service'" ) else: cmd = ( f"cp /tmp/neko-webrtc-dnat.sh /usr/local/sbin/neko-webrtc-dnat.sh; chmod +x /usr/local/sbin/neko-webrtc-dnat.sh; " f"{env} /usr/local/sbin/neko-webrtc-dnat.sh; " "cat >/etc/systemd/system/neko-webrtc-dnat.service <<'UNIT'\n" "[Unit]\nDescription=Neko WebRTC DNAT\nAfter=network-online.target\nWants=network-online.target\n\n" "[Service]\nType=oneshot\n" f"Environment=NEKO_DNAT_TARGET={target}\nEnvironment=NEKO_DNAT_WG={wg}\n" "ExecStart=/usr/local/sbin/neko-webrtc-dnat.sh\nRemainAfterExit=yes\n\n" "[Install]\nWantedBy=multi-user.target\nUNIT\n" "systemctl daemon-reload; systemctl enable --now neko-webrtc-dnat.service || systemctl enable neko-webrtc-dnat.service" ) print(run(c, cmd, 120), flush=True) def main(): # Tencent primary (NAT1TO1) d = ssh("82.157.112.245", 22, "ubuntu", "Xiao4669805") deploy_edge(d, target="10.9.0.2", wg="wg-dy-cn", label="TENCENT", use_sudo=True) d.close() # Overseas backup (if ICE ever uses .com IP) v = ssh("107.173.30.245", 22, "root", "Xiao4669805") deploy_edge(v, target="10.8.0.2", wg="wg-dy", label="OVERSEAS", use_sudo=False) # physical ufw allow webrtc g = ssh("107.173.30.245", 4014, "eric", "4669805") print( run( g, "echo '4669805' | sudo -S -p '' bash -lc '" "ufw allow 52300:52363/tcp; ufw allow 52300:52363/udp; " "ufw allow 52000:52098/udp; ufw allow 52101:52199/udp; " "ufw allow 52201:52299/udp; ufw allow 52400:53999/udp; " "ufw status | grep -E \"5230|5240|5250|5260|5200|5210|5220\" | head'" "'", 60, ), flush=True, ) g.close() print( run( v, "echo --- tencent nat sample ---; " # can't see tencent from here; show overseas "iptables -t nat -S | grep NEKO | head -25; " "echo --- mux tcp to guest ---; " "for p in 52304 52305; do timeout 2 bash -c \"echo >/dev/tcp/10.8.0.2/$p\" && echo $p ok || echo $p fail; done", ), flush=True, ) v.close() print( "DONE — open Tencent SG UDP 52300-52363 + 52000-53999 if console blocks. " "Hard refresh https://dy.nomadro.com/neko/5/ login douyinweb", flush=True, ) if __name__ == "__main__": main()