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

138 lines
3.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Sudo recreate Neko with Tencent NAT1TO1 (PYTHONPATH fixed)."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
TENCENT = "82.157.112.245"
PASS = "4669805"
REMOTE_PY = r'''
import os, sys
sys.path.insert(0, "/opt/douyinWEB/backend")
os.chdir("/opt/douyinWEB/backend")
os.environ["DY_NEKO_NAT1TO1"] = "82.157.112.245"
os.environ["DY_NEKO_HTTP_BIND"] = "0.0.0.0"
os.environ["DY_NEKO_EDGE_UPSTREAM"] = "10.8.0.2"
os.environ.setdefault("DY_PUBLIC_BASE_URL", "https://dy.nomadro.com")
os.environ.setdefault("DY_INTERNAL_API_URL", "http://127.0.0.1:8014")
from app import youtube_neko as yn
cache = yn._neko_root() / "nat1to1.txt"
cache.unlink(missing_ok=True)
yn._nat_ip_cache = ""
yn._nat_ip_cached_at = 0.0
print("nat1to1", yn._detect_public_ip())
print("http_bind", yn._http_bind())
desired = max(1, yn._desired_from_db())
for i in range(1, yn.MAX_INSTANCES + 1):
st = yn._container_state(i)
if st.get("exists"):
desired = max(desired, i)
print("desired", desired)
text = yn._render_compose(desired)
path = yn._compose_path()
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8")
print("wrote", path)
for ln in text.splitlines():
if "NAT1TO1" in ln or ":920" in ln or ":8080" in ln:
print(ln)
code, out = yn._run(yn._compose_cmd("up", "-d", "--force-recreate"), timeout=900)
print("up_code", code)
print(out[-1200:])
for i in range(1, desired + 1):
print("neko", i, yn._container_state(i))
'''
def ssh_guest():
last = None
for i in range(6):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"107.173.30.245",
4014,
"eric",
PASS,
timeout=60,
banner_timeout=120,
allow_agent=False,
look_for_keys=False,
)
return c
except Exception as ex:
last = ex
print("ssh", i, type(ex).__name__, flush=True)
time.sleep(2 + i)
raise last # type: ignore[misc]
def run(c, cmd, timeout=900):
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
def main():
g = ssh_guest()
sftp = g.open_sftp()
with sftp.file("/tmp/_recreate_neko_nat.py", "w") as f:
f.write(REMOTE_PY)
sftp.close()
print(
run(
g,
f"echo '{PASS}' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_recreate_neko_nat.py",
900,
),
flush=True,
)
print(
run(
g,
f"echo '{PASS}' | sudo -S -p '' bash -c \""
"docker inspect douyinweb-neko-1 --format '{{range .Config.Env}}{{println .}}{{end}}' "
"| grep -E 'NAT1TO1|UDPMUX|EPR|TCPMUX'; "
"docker ps --format '{{.Names}} {{.Status}} {{.Ports}}' | grep neko || true; "
"curl -sS -m 5 -o /dev/null -w 'local=%{http_code}\\n' http://127.0.0.1:9200/; "
"curl -sS -m 5 -o /dev/null -w 'cn_wg=%{http_code}\\n' http://10.9.0.2:9200/; "
"curl -sS -m 5 -o /dev/null -w 'leg_wg=%{http_code}\\n' http://10.8.0.2:9200/\"",
40,
),
flush=True,
)
g.close()
import httpx
time.sleep(3)
with httpx.Client(follow_redirects=False, timeout=20) as cl:
for u in [
"https://dy.nomadro.cn/neko/1/",
"https://dy.nomadro.com/neko/1/",
"https://dy.nomadro.cn/api/health",
"https://dylive.nomadro.com/api/health",
]:
try:
r = cl.get(u)
print(u, r.status_code, flush=True)
except Exception as ex:
print(u, ex, flush=True)
print("DONE recreate", flush=True)
if __name__ == "__main__":
main()