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

227 lines
6.8 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy unlimited neko path gateway + switch both edges to single /neko/* route."""
from __future__ import annotations
import re
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
FILES = [
("backend/app/neko_edge_gateway.py", "/opt/douyinWEB/backend/app/neko_edge_gateway.py"),
("backend/app/config.py", "/opt/douyinWEB/backend/app/config.py"),
("backend/app/main.py", "/opt/douyinWEB/backend/app/main.py"),
("backend/app/youtube_neko.py", "/opt/douyinWEB/backend/app/youtube_neko.py"),
]
CADDY_NEKO = """
# Unlimited Neko: path gateway on physical :9199 (neko_edge_gateway)
handle /neko {
redir /neko/ 308
}
@neko path /neko /neko/*
handle @neko {
reverse_proxy 10.8.0.2:9199 {
header_up Host {host}
header_up X-Forwarded-Proto {scheme}
header_up X-Forwarded-Host {host}
}
}
"""
NGINX_NEKO = """
# Unlimited Neko via physical path gateway :9199
location = /neko { return 308 /neko/; }
location /neko/ {
proxy_pass http://10.9.0.2:9199;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
proxy_buffering off;
}
"""
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=180):
_, o, e = c.exec_command(cmd, timeout=t)
return (o.read() + e.read()).decode("utf-8", "replace")
def strip_neko_blocks_caddy(text: str) -> str:
# remove handle /neko/N and handle_path /neko/N and @neko gateway blocks
text = re.sub(
r"\n\thandle /neko(?:/\d+)? \{\n\t\tredir /neko(?:/\d+)?/ 308\n\t\}\n",
"\n",
text,
)
text = re.sub(
r"\n\thandle_path /neko/\d+/\* \{[\s\S]*?\n\t\}\n",
"\n",
text,
)
text = re.sub(
r"\n\t# Unlimited Neko[\s\S]*?\n\thandle @neko \{[\s\S]*?\n\t\}\n",
"\n",
text,
)
text = re.sub(r"\n\t@neko path /neko /neko/\*\n", "\n", text)
return text
def strip_neko_blocks_nginx(text: str) -> str:
text = re.sub(
r"\n\s*location = /neko/\d+ \{ return 308 /neko/\d+/; \}\n"
r"\s*location /neko/\d+/ \{[\s\S]*?\n\s*\}\n",
"\n",
text,
)
text = re.sub(
r"\n\s*# Unlimited Neko[\s\S]*?location /neko/ \{[\s\S]*?\n\s*\}\n",
"\n",
text,
)
# also old upstream style location /neko/N/
text = re.sub(
r"\n\s*location = /neko/\d+ \{ return 308 /neko/\d+/; \}\n",
"\n",
text,
)
text = re.sub(
r"\n\s*location /neko/\d+/ \{[\s\S]*?\n\s*\}\n",
"\n",
text,
)
return text
def main():
g = ssh("107.173.30.245", 4014, "eric", "4669805")
sftp = g.open_sftp()
for rel, remote in FILES:
local = ROOT / rel
tmp = f"/tmp/{Path(remote).name}"
with sftp.file(tmp, "w") as f:
f.write(local.read_text(encoding="utf-8"))
print("staged", rel, flush=True)
print(
run(
g,
f"echo '4669805' | sudo -S -p '' bash -lc '"
f"install -m 644 {tmp} {remote}'",
),
flush=True,
)
sftp.close()
# open UFW for gateway from WG peers if ufw exists
print(
run(
g,
"echo '4669805' | sudo -S -p '' bash -lc '"
"ufw allow from 10.8.0.0/24 to any port 9199 proto tcp 2>/dev/null || true; "
"ufw allow from 10.9.0.0/24 to any port 9199 proto tcp 2>/dev/null || true; "
"systemctl restart douyinweb-api; sleep 4; "
"systemctl is-active douyinweb-api; "
"ss -lntp | grep 9199 || echo NO_9199; "
"curl -sS -m 5 -o /dev/null -w \"gw_root=%{http_code}\\n\" http://127.0.0.1:9199/ || true; "
"curl -sS -m 5 -o /dev/null -w \"gw5=%{http_code}\\n\" http://127.0.0.1:9199/neko/5/ || true; "
"curl -sS -m 5 -o /dev/null -w \"gw6=%{http_code}\\n\" http://127.0.0.1:9199/neko/6/ || true"
"'",
120,
),
flush=True,
)
g.close()
# overseas caddy
v = ssh("107.173.30.245", 22, "root", "Xiao4669805")
path = "/etc/caddy/sites/dy.nomadro.com.caddy"
text = run(v, f"cat {path}")
text2 = strip_neko_blocks_caddy(text)
if "@neko path /neko" not in text2:
# insert before handle /_/*
if "handle /_/*" in text2:
text2 = text2.replace("handle /_/*", CADDY_NEKO + "\n\thandle /_/*", 1)
else:
text2 = text2.replace("\thandle {", CADDY_NEKO + "\n\thandle {", 1)
run(v, f"cp -a {path} {path}.bak-gw-$(date +%s)")
sftp = v.open_sftp()
with sftp.file(path, "w") as f:
f.write(text2)
sftp.close()
print(
run(
v,
"caddy validate --config /etc/caddy/Caddyfile 2>&1 | tail -6; "
"systemctl reload caddy; sleep 1; systemctl is-active caddy",
),
flush=True,
)
# tencent nginx
d = ssh("82.157.112.245", 22, "ubuntu", "Xiao4669805")
npath = "/www/server/panel/vhost/nginx/dy.nomadro.cn.conf"
ntext = run(d, f"sudo cat {npath}")
ntext2 = strip_neko_blocks_nginx(ntext)
if "10.9.0.2:9199" not in ntext2:
marker = " location / {\n proxy_pass http://dy_cn_web;"
if marker not in ntext2:
raise SystemExit("nginx marker missing")
ntext2 = ntext2.replace(marker, NGINX_NEKO + "\n" + marker, 1)
run(d, f"sudo cp -a {npath} {npath}.bak-gw-$(date +%s)")
sftp = d.open_sftp()
with sftp.file("/tmp/dy.cn.conf.new", "w") as f:
f.write(ntext2)
sftp.close()
print(
run(
d,
f"sudo mv /tmp/dy.cn.conf.new {npath}; sudo nginx -t 2>&1 | tail -8; "
"sudo nginx -s reload; echo nginx_ok",
),
flush=True,
)
d.close()
time.sleep(2)
print("=== verify ===", flush=True)
print(
run(
v,
r"""
for i in 1 2 4 5 6; do
code=$(curl -sS -m 12 -o /dev/null -w "%{http_code}" "https://dy.nomadro.com/neko/$i/" || echo err)
echo "com/$i=$code"
done
curl -sS -m 12 -o /dev/null -w "dylive=%{http_code}\n" https://dylive.nomadro.com/api/health
grep -n '9199\|@neko\|neko/5' /etc/caddy/sites/dy.nomadro.com.caddy | head
""",
),
flush=True,
)
v.close()
print("DONE", flush=True)
if __name__ == "__main__":
main()