232 lines
9.3 KiB
Python
232 lines
9.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Fix ops-neko stuck loading: Tencent DNAT + nginx /ops-neko → physical.
|
|
|
|
Root cause:
|
|
NAT1TO1=82.157.112.245 but Tencent only DNATs assign mux 52300+.
|
|
Ops fleet uses 62300+ / EPR 62500+ — ICE never reaches guest → forever loading.
|
|
.cn also had no /ops-neko locations (fell through to Next → login/308).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import sys
|
|
import time
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
TENCENT = ("82.157.112.245", 22, "ubuntu", "Xiao4669805")
|
|
PHYS = ("107.173.30.245", 4014, "eric", "4669805")
|
|
NGINX_CONF = "/www/server/panel/vhost/nginx/dy.nomadro.cn.conf"
|
|
UP = "10.9.0.2"
|
|
OPS_SLOTS = 8
|
|
|
|
|
|
def ssh(spec):
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(*spec[:3], password=spec[3], timeout=45, allow_agent=False, look_for_keys=False)
|
|
return c
|
|
|
|
|
|
def run(c, cmd, t=120):
|
|
_i, o, e = c.exec_command(cmd, timeout=t)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
OPS_DNAT = r'''#!/bin/bash
|
|
set -u
|
|
COMMENT=OPS-NEKO-WEBRTC
|
|
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 "[ops-neko-dnat] TARGET=$TARGET PUB=$PUB_IF"
|
|
|
|
# wipe previous OPS-NEKO-WEBRTC only (keep assign NEKO-WEBRTC)
|
|
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 for ops slots 1..8 → 62300..62307
|
|
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"
|
|
# EPR UDP 62500-62579 (8 * 10)
|
|
add -t nat -A PREROUTING -i "$PUB_IF" -p udp --dport 62500:62579 -m comment --comment "$COMMENT" -j DNAT --to-destination "$TARGET"
|
|
|
|
# reuse FORWARD/MASQ if NEKO-WEBRTC already has them; still add tagged ones for clarity
|
|
add -t nat -A POSTROUTING -o "$WG_IF" -d "$TARGET" -p tcp --dport 62300:62307 -m comment --comment "$COMMENT" -j MASQUERADE 2>/dev/null || true
|
|
add -t nat -A POSTROUTING -o "$WG_IF" -d "$TARGET" -p udp --dport 62300:62307 -m comment --comment "$COMMENT" -j MASQUERADE 2>/dev/null || true
|
|
add -t nat -A POSTROUTING -o "$WG_IF" -d "$TARGET" -p udp --dport 62500:62579 -m comment --comment "$COMMENT" -j MASQUERADE 2>/dev/null || true
|
|
|
|
if command -v ufw >/dev/null 2>&1; then
|
|
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
|
|
fi
|
|
|
|
# persist if netfilter-persistent / iptables-save available
|
|
if command -v netfilter-persistent >/dev/null 2>&1; then
|
|
netfilter-persistent save 2>/dev/null || true
|
|
elif [ -d /etc/iptables ]; then
|
|
iptables-save > /etc/iptables/rules.v4 2>/dev/null || true
|
|
fi
|
|
|
|
echo "[ops-neko-dnat] rules:"
|
|
iptables -t nat -S | grep "$COMMENT"
|
|
'''
|
|
|
|
|
|
def nginx_ops_blocks() -> str:
|
|
parts = [
|
|
"\n # Ops-Neko tab fleet (douyinweb-ops-neko-*) — same WS headers as /neko/\n",
|
|
" location ~ ^/ops-neko/([0-9]+)$ {\n",
|
|
" return 308 /ops-neko/$1/;\n",
|
|
" }\n",
|
|
]
|
|
for n in range(1, OPS_SLOTS + 1):
|
|
port = 18200 + n - 1
|
|
parts.append(
|
|
f" location /ops-neko/{n}/ {{\n"
|
|
f" proxy_pass http://{UP}:{port}/;\n"
|
|
f" proxy_http_version 1.1;\n"
|
|
f" proxy_set_header Upgrade $http_upgrade;\n"
|
|
f" proxy_set_header Connection $connection_upgrade;\n"
|
|
f" proxy_set_header Host $host;\n"
|
|
f" proxy_set_header X-Forwarded-Host $host;\n"
|
|
f" proxy_set_header X-Real-IP $remote_addr;\n"
|
|
f" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n"
|
|
f" proxy_set_header X-Forwarded-Proto $scheme;\n"
|
|
f" proxy_read_timeout 3600s;\n"
|
|
f" proxy_send_timeout 3600s;\n"
|
|
f" proxy_buffering off;\n"
|
|
f" }}\n"
|
|
)
|
|
return "".join(parts)
|
|
|
|
|
|
def main():
|
|
t = ssh(TENCENT)
|
|
print("=== 1) DNAT ops-neko WebRTC on Tencent ===", flush=True)
|
|
sftp = t.open_sftp()
|
|
with sftp.file("/tmp/ops-neko-webrtc-dnat.sh", "w") as f:
|
|
f.write(OPS_DNAT)
|
|
sftp.close()
|
|
print(
|
|
run(
|
|
t,
|
|
"sudo chmod +x /tmp/ops-neko-webrtc-dnat.sh; "
|
|
"sudo bash /tmp/ops-neko-webrtc-dnat.sh; "
|
|
"sudo install -m 755 /tmp/ops-neko-webrtc-dnat.sh /usr/local/sbin/ops-neko-webrtc-dnat.sh",
|
|
60,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
print("=== 2) nginx /ops-neko locations ===", flush=True)
|
|
conf = run(t, f"sudo cat {NGINX_CONF}")
|
|
if "location /ops-neko/1/" in conf:
|
|
print("ops-neko locations already present — skip insert", flush=True)
|
|
else:
|
|
blocks = nginx_ops_blocks()
|
|
# insert before first /neko/ location block
|
|
m = re.search(r"\n\s*# Neko 1\.\.|^\s*location ~ \^/neko/", conf, re.M)
|
|
if not m:
|
|
m = re.search(r"\n\s*location /neko/1/ \{", conf)
|
|
if not m:
|
|
raise SystemExit("cannot find insertion point for ops-neko in nginx conf")
|
|
new_conf = conf[: m.start()] + "\n" + blocks + conf[m.start() :]
|
|
bak = f"{NGINX_CONF}.bak.opsneko.{int(time.time())}"
|
|
run(t, f"sudo cp -a {NGINX_CONF} {bak}")
|
|
sftp = t.open_sftp()
|
|
with sftp.file("/tmp/dy.nomadro.cn.conf.new", "w") as f:
|
|
f.write(new_conf)
|
|
sftp.close()
|
|
out = run(
|
|
t,
|
|
f"sudo mv /tmp/dy.nomadro.cn.conf.new {NGINX_CONF}; "
|
|
f"sudo nginx -t 2>&1; "
|
|
f"sudo nginx -s reload 2>&1; "
|
|
f"echo bak={bak}",
|
|
60,
|
|
)
|
|
print(out, flush=True)
|
|
if "syntax is ok" not in out and "test is successful" not in out:
|
|
print("nginx -t failed — restoring", flush=True)
|
|
print(run(t, f"sudo cp -a {bak} {NGINX_CONF}; sudo nginx -s reload"), flush=True)
|
|
raise SystemExit("nginx restore")
|
|
|
|
print("=== 3) physical firewall for ops mux via wg-dy-cn ===", flush=True)
|
|
p = ssh(PHYS)
|
|
print(
|
|
run(
|
|
p,
|
|
"echo '4669805' | sudo -S -p '' bash -lc '"
|
|
"ufw allow in on wg-dy-cn to any port 62300:62307 proto tcp 2>/dev/null || true; "
|
|
"ufw allow in on wg-dy-cn to any port 62300:62307 proto udp 2>/dev/null || true; "
|
|
"ufw allow in on wg-dy-cn to any port 62500:62579 proto udp 2>/dev/null || true; "
|
|
"ufw allow in on wg-dy-cn to any port 18200:18207 proto tcp 2>/dev/null || true; "
|
|
"iptables -C INPUT -i wg-dy-cn -p tcp --dport 62300:62307 -j ACCEPT 2>/dev/null || "
|
|
" iptables -I INPUT -i wg-dy-cn -p tcp --dport 62300:62307 -j ACCEPT; "
|
|
"iptables -C INPUT -i wg-dy-cn -p udp --dport 62300:62307 -j ACCEPT 2>/dev/null || "
|
|
" iptables -I INPUT -i wg-dy-cn -p udp --dport 62300:62307 -j ACCEPT; "
|
|
"iptables -C INPUT -i wg-dy-cn -p udp --dport 62500:62579 -j ACCEPT 2>/dev/null || "
|
|
" iptables -I INPUT -i wg-dy-cn -p udp --dport 62500:62579 -j ACCEPT; "
|
|
"iptables -C INPUT -i wg-dy-cn -p tcp --dport 18200:18207 -j ACCEPT 2>/dev/null || "
|
|
" iptables -I INPUT -i wg-dy-cn -p tcp --dport 18200:18207 -j ACCEPT; "
|
|
"echo phys_fw_ok; "
|
|
"ss -lntup | grep 62300 | head -3; "
|
|
"curl -sS -m 5 -o /dev/null -w \"ops1=%{http_code}\\n\" http://127.0.0.1:18200/; "
|
|
"'",
|
|
60,
|
|
),
|
|
flush=True,
|
|
)
|
|
p.close()
|
|
|
|
print("=== 4) verify ===", flush=True)
|
|
print(
|
|
run(
|
|
t,
|
|
r"""
|
|
curl -sS -m 12 -o /dev/null -w 'cn_ops1=%{http_code}\n' https://dy.nomadro.cn/ops-neko/1/
|
|
curl -sS -m 12 -o /tmp/ops.html -w 'cn_ops1_body=%{http_code} size=%{size_download}\n' https://dy.nomadro.cn/ops-neko/1/
|
|
head -c 120 /tmp/ops.html; echo
|
|
# TCP to guest mux via DNAT path from tencent itself (local connect won't hit PREROUTING)
|
|
timeout 3 bash -c 'echo >/dev/tcp/10.9.0.2/62300' && echo guest_mux_open || echo guest_mux_closed
|
|
timeout 3 bash -c 'echo >/dev/tcp/10.9.0.2/18200' && echo guest_http_open || echo guest_http_closed
|
|
sudo iptables -t nat -S | grep OPS-NEKO
|
|
""",
|
|
40,
|
|
),
|
|
flush=True,
|
|
)
|
|
t.close()
|
|
|
|
# public .com still HTTP via FRP; ICE uses Tencent IP
|
|
v = ssh(("107.173.30.245", 22, "root", "Xiao4669805"))
|
|
print(
|
|
run(
|
|
v,
|
|
"curl -sS -m 12 -o /dev/null -w 'com_ops1=%{http_code}\\n' https://dy.nomadro.com/ops-neko/1/; "
|
|
"curl -sS -m 8 -o /dev/null -w 'com_api=%{http_code}\\n' https://dy.nomadro.com/api/health; "
|
|
"curl -sS -m 8 -o /dev/null -w 'dylive=%{http_code}\\n' https://dylive.nomadro.com/api/health",
|
|
40,
|
|
),
|
|
flush=True,
|
|
)
|
|
v.close()
|
|
print("DONE — hard refresh https://dy.nomadro.com/ops-neko/1/ (ICE via 82.157.112.245:62300)", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|