douyinWEB/scripts/_fix_wg_cn_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

81 lines
2.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Enable TCP forwarding through domestic wg-dy-cn hub."""
from __future__ import annotations
import sys
import paramiko
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
def ssh(h, p, u, pw):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(h, p, u, pw, timeout=45, allow_agent=False, look_for_keys=False)
return c
def run(c, cmd, timeout=60):
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
FORWARD_RULES = r"""sudo bash -c '
sysctl -w net.ipv4.ip_forward=1
# persist forward
grep -q "^net.ipv4.ip_forward=1" /etc/sysctl.conf 2>/dev/null || echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf
# Allow WG mesh forwarding (idempotent)
iptables -C FORWARD -i wg-dy-cn -o wg-dy-cn -j ACCEPT 2>/dev/null || iptables -A FORWARD -i wg-dy-cn -o wg-dy-cn -j ACCEPT
iptables -C FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT 2>/dev/null || iptables -I FORWARD 1 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -L FORWARD -n -v | head -10
'"""
def main():
d = ssh("82.157.112.245", 22, "ubuntu", "Xiao4669805")
print("=== domestic hub forward ===")
print(run(d, FORWARD_RULES, 30))
g = ssh("107.173.30.245", 4014, "eric", "4669805")
print("=== physical: allow wg-dy-cn to app ports ===")
print(
run(
g,
"echo '4669805' | sudo -S -p '' bash -c '"
"ufw allow in on wg-dy-cn to any port 8014 proto tcp 2>/dev/null || true; "
"ufw allow in on wg-dy-cn to any port 4015 proto tcp 2>/dev/null || true; "
"ufw allow in on wg-dy-cn to any port 9200:9210 proto tcp 2>/dev/null || true; "
"iptables -C INPUT -i wg-dy-cn -p tcp --dport 8014 -j ACCEPT 2>/dev/null || "
" iptables -I INPUT -i wg-dy-cn -p tcp --dport 8014 -j ACCEPT; "
"iptables -C INPUT -i wg-dy-cn -p tcp --dport 4015 -j ACCEPT 2>/dev/null || "
" iptables -I INPUT -i wg-dy-cn -p tcp --dport 4015 -j ACCEPT; "
"iptables -C INPUT -i wg-dy-cn -p tcp --dport 9200:9210 -j ACCEPT 2>/dev/null || "
" iptables -I INPUT -i wg-dy-cn -p tcp --dport 9200:9210 -j ACCEPT; "
"ss -lntp | grep -E \":8014|:4015|:9200\" | head -5'",
45,
)
)
g.close()
v = ssh("107.173.30.245", 22, "root", "Xiao4669805")
print("=== VPS test api via wg-dy-cn ===")
print(
run(
v,
"curl -sS -m 10 -o /dev/null -w 'api_cn=%{http_code} t=%{time_total}s\\n' http://10.9.0.2:8014/api/health; "
"curl -sS -m 10 -o /dev/null -w 'api_legacy=%{http_code} t=%{time_total}s\\n' http://10.8.0.2:8014/api/health; "
"ping -c 3 -W 2 10.9.0.2 | tail -2",
35,
)
)
v.close()
d.close()
if __name__ == "__main__":
main()