# -*- coding: utf-8 -*- """Install Docker + Neko assets on physical Ubuntu; refresh Caddy /neko routes on VPS. Does not restart sing-box / dylive. Reloads Caddy only after validate. """ from __future__ import annotations import sys import tarfile import tempfile import time from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(__file__).resolve().parents[1] SEED = ROOT / "backend" / "neko" / "tampermonkey-seed" VPS = "107.173.30.245" PVE_WG = "10.8.0.2" def ssh(host, port, user, password): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host, port, user, password, timeout=40, allow_agent=False, look_for_keys=False) return c def run(c, cmd, timeout=300): _, o, e = c.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace") def must_dylive(v, label): code = run(v, "curl -sS -m 8 -o /dev/null -w '%{http_code}' https://dylive.nomadro.com/api/health", 15).strip() print(f"[dylive {label}] {code}", flush=True) if code != "200": raise SystemExit(f"ABORT dylive {code}") def pack_seed() -> Path: tmp = tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False) tmp.close() path = Path(tmp.name) with tarfile.open(path, "w:gz") as tar: tar.add(SEED, arcname="tampermonkey-seed") print(f"[pack seed] {path.stat().st_size / 1024 / 1024:.1f} MB", flush=True) return path def main() -> int: v = ssh(VPS, 22, "root", "Xiao4669805") must_dylive(v, "start") p = ssh(VPS, 4014, "eric", "4669805") print("=== install docker on guest ===", flush=True) print( run( p, "echo '4669805' | sudo -S -p '' bash -c '" "export DEBIAN_FRONTEND=noninteractive; " "if ! command -v docker >/dev/null; then " " apt-get update -y; " " apt-get install -y --no-install-recommends ca-certificates curl; " " curl -fsSL https://get.docker.com | sh; " "fi; " "systemctl enable --now docker; " "docker --version; docker compose version" "'", 600, ), flush=True, ) seed_tar = pack_seed() sftp = p.open_sftp() sftp.put(str(seed_tar), "/tmp/tampermonkey-seed.tar.gz") # Upload changed backend files for rel in ( "backend/app/youtube_neko.py", "backend/app/network_hub.py", "backend/app/global_socks.py", "backend/app/main.py", "frontend/src/lib/shellNav.ts", "frontend/src/app/network/page.tsx", "frontend/next.config.mjs", ): local = ROOT / rel if local.is_file(): remote = f"/tmp/_up_{local.name}" sftp.put(str(local), remote) sftp.close() seed_tar.unlink(missing_ok=True) print("=== install assets + env ===", flush=True) print( run( p, "echo '4669805' | sudo -S -p '' bash -c '" "set -e; " "mkdir -p /opt/douyinWEB/backend/neko /opt/douyinWEB/backend/data/neko; " "tar -xzf /tmp/tampermonkey-seed.tar.gz -C /opt/douyinWEB/backend/neko; " "install -m 644 /tmp/_up_youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py; " "install -m 644 /tmp/_up_network_hub.py /opt/douyinWEB/backend/app/network_hub.py; " "install -m 644 /tmp/_up_global_socks.py /opt/douyinWEB/backend/app/global_socks.py; " "install -m 644 /tmp/_up_main.py /opt/douyinWEB/backend/app/main.py; " "mkdir -p /opt/douyinWEB/frontend/src/app/network; " "install -m 644 /tmp/_up_shellNav.ts /opt/douyinWEB/frontend/src/lib/shellNav.ts; " "install -m 644 /tmp/_up_page.tsx /opt/douyinWEB/frontend/src/app/network/page.tsx; " # Neko env: edge upstream for Caddy snippet = WG self; NAT1TO1 leave auto / VPS IP for docs "grep -q DY_NEKO_EDGE_UPSTREAM /etc/systemd/system/douyinweb-api.service || " " sed -i \"/DY_SESSION_SECRET=/a Environment=DY_NEKO_EDGE_UPSTREAM=10.8.0.2\" /etc/systemd/system/douyinweb-api.service; " "grep -q DY_NEKO_NAT1TO1 /etc/systemd/system/douyinweb-api.service || " " sed -i \"/DY_NEKO_EDGE_UPSTREAM=/a Environment=DY_NEKO_NAT1TO1=107.173.30.245\" /etc/systemd/system/douyinweb-api.service; " "systemctl daemon-reload; " "systemctl enable --now wg-quick@wg-dy || true; " "docker pull ghcr.io/m1k1o/neko/google-chrome:3.0.0; " "ls /opt/douyinWEB/backend/neko/tampermonkey-seed | head -3; " "systemctl restart douyinweb-api; sleep 4; systemctl is-active douyinweb-api" "'", 900, ), flush=True, ) print("=== frontend build (network tab) ===", flush=True) print( run( p, "echo '4669805' | sudo -S -p '' bash -c '" "cd /opt/douyinWEB/frontend; " "export DY_INTERNAL_API_URL=http://127.0.0.1:8014 DY_PUBLIC_BASE_URL=https://dy.nomadro.com; " "npm run build; systemctl restart douyinweb-web; sleep 2; systemctl is-active douyinweb-web" "'", 300, ), flush=True, ) print("=== generate caddy snippet + probe neko status ===", flush=True) print( run( p, "echo '4669805' | sudo -S -p '' bash -c '" "cd /opt/douyinWEB/backend; " ".venv/bin/python - <<\"PY\"\n" "from app import youtube_neko, yt_assign_db\n" "groups = yt_assign_db.list_groups()\n" "print(\"groups\", len(groups))\n" "r = youtube_neko.sync_for_groups(groups)\n" "print(\"sync\", r)\n" "print(\"caddy\", youtube_neko.sync_caddy_neko_snippet(max(1, len(groups) or 1)))\n" "print(\"status\", youtube_neko.status_summary())\n" "PY" "'", 180, ), flush=True, ) # Pull snippet via cat and merge into VPS caddy for first 4 slots at least snippet = run( p, "echo '4669805' | sudo -S -p '' cat /opt/douyinWEB/backend/data/neko/caddy-neko.snippet 2>/dev/null | tail -n +1", 20, ) # Build Caddy site with Host headers + neko handles for instances 1..4 minimum neko_blocks = [] for i in range(1, 5): port = 9200 + i - 1 neko_blocks.append( f""" handle /neko/{i} {{ redir /neko/{i}/ 308 }} handle_path /neko/{i}/* {{ reverse_proxy {PVE_WG}:{port} {{ header_up Host {{host}} header_up X-Forwarded-Proto {{scheme}} }} }}""" ) caddy = f"""# Physical app — dy.nomadro.com → WG {PVE_WG} # Do not edit dylive.nomadro.com.caddy dy.nomadro.com {{ encode gzip request_body {{ max_size 200MB }} handle /api/* {{ reverse_proxy {PVE_WG}:8014 {{ header_up Host {{host}} }} }} handle /covers/* {{ reverse_proxy {PVE_WG}:8014 {{ header_up Host {{host}} }} }} {''.join(neko_blocks)} handle /_/* {{ reverse_proxy {PVE_WG}:8092 {{ header_up Host {{host}} }} }} handle /douyin-covers/* {{ reverse_proxy {PVE_WG}:8333 {{ header_up Host {{host}} }} }} handle {{ reverse_proxy {PVE_WG}:4015 {{ header_up Host {{host}} }} }} }} """ must_dylive(v, "before caddy") sftp = v.open_sftp() with sftp.file("/etc/caddy/sites/dy.nomadro.com.caddy", "w") as f: f.write(caddy) sftp.close() val = run(v, "caddy validate --config /etc/caddy/Caddyfile 2>&1", 30) print(val[-800:], flush=True) if "Valid configuration" not in val and "valid" not in val.lower(): raise SystemExit("caddy validate failed") print(run(v, "systemctl reload caddy; sleep 2; systemctl is-active caddy", 20), flush=True) must_dylive(v, "after caddy") print( run( v, "curl -sS -m 10 -o /dev/null -w 'dy_api:%{http_code}\\n' https://dy.nomadro.com/api/health; " "curl -sS -m 10 -o /dev/null -w 'dy_network:%{http_code}\\n' https://dy.nomadro.com/network; " "curl -sS -m 10 http://10.8.0.2:8014/api/network | head -c 400; echo", 30, ), flush=True, ) print("snippet_head", (snippet or "")[:200], flush=True) v.close() p.close() print("DONE", flush=True) return 0 if __name__ == "__main__": raise SystemExit(main())