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>
144 lines
4.8 KiB
Python
144 lines
4.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Resume: overseas DNAT→10.8.0.2 + Tencent DNAT→10.9.0.2 (SSH retries)."""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
LEGACY = "10.8.0.2"
|
|
CN = "10.9.0.2"
|
|
|
|
|
|
def ssh(host, port, user, pw, tries=8):
|
|
last = None
|
|
for i in range(tries):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
host,
|
|
port,
|
|
user,
|
|
pw,
|
|
timeout=60,
|
|
banner_timeout=120,
|
|
auth_timeout=60,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
)
|
|
return c
|
|
except Exception as ex:
|
|
last = ex
|
|
print(f"ssh {host}:{port} try{i} {type(ex).__name__}: {ex}", flush=True)
|
|
time.sleep(3 + i * 2)
|
|
raise last # type: ignore[misc]
|
|
|
|
|
|
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_script(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 -C POSTROUTING -o "$WG_IF" -d "$TARGET" -m comment --comment NEKO-WEBRTC -j MASQUERADE 2>/dev/null || \
|
|
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
|
|
|
|
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 main() -> int:
|
|
print("=== overseas DNAT → 10.8.0.2 ===", flush=True)
|
|
v = ssh("107.173.30.245", 22, "root", "Xiao4669805")
|
|
print(put_run(v, dnat_script(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; "
|
|
"curl -sS -m 12 -o /dev/null -w 'com_neko=%{http_code}\\n' https://dy.nomadro.com/neko/1/",
|
|
40,
|
|
),
|
|
flush=True,
|
|
)
|
|
v.close()
|
|
|
|
print("=== Tencent UFW + DNAT → 10.9.0.2 ===", flush=True)
|
|
d = ssh("82.157.112.245", 22, "ubuntu", "Xiao4669805")
|
|
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; "
|
|
"echo UFW_OK'",
|
|
40,
|
|
),
|
|
flush=True,
|
|
)
|
|
print(put_run(d, dnat_script(CN, "wg-dy-cn"), sudo=True), flush=True)
|
|
print(
|
|
run(
|
|
d,
|
|
"sudo iptables -t nat -S PREROUTING | grep NEKO | head -8; "
|
|
"curl -sS -m 8 -o /dev/null -w 'cn_neko=%{http_code}\\n' https://dy.nomadro.cn/neko/1/",
|
|
30,
|
|
),
|
|
flush=True,
|
|
)
|
|
d.close()
|
|
|
|
print("DONE", flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|