# -*- coding: utf-8 -*- """Deploy egress hub + network UI; install proxychains4; enable ShellCrash egress.""" from __future__ import annotations import sys import time from pathlib import Path import httpx import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(__file__).resolve().parents[1] VPS = "107.173.30.245" GUEST_PW = "4669805" FILES = [ "backend/app/egress_hub.py", "backend/app/shellcrash_hub.py", "backend/app/network_hub.py", "backend/app/main.py", "frontend/src/app/network/page.tsx", ] def ssh_guest(tries: int = 10): last = None for i in range(tries): try: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect( VPS, 4014, "eric", GUEST_PW, timeout=40, banner_timeout=80, allow_agent=False, look_for_keys=False, ) return c except Exception as ex: last = ex print(f"frp fail{i} {type(ex).__name__}", flush=True) time.sleep(2 + i) raise last # type: ignore[misc] def run(c, cmd: str, timeout: int = 180) -> str: _, o, e = c.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace") def main() -> int: g = ssh_guest() sftp = g.open_sftp() installs = [] for rel in FILES: data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") tmp = f"/tmp/_up_{Path(rel).name}" with sftp.file(tmp, "wb") as f: f.write(data) dest = f"/opt/douyinWEB/{rel.replace(chr(92), '/')}" installs.append(f"install -m 644 {tmp} {dest}") print("put", rel, flush=True) fe = b"""#!/bin/bash set -e cd /opt/douyinWEB/frontend export DY_INTERNAL_API_URL=http://127.0.0.1:8014 export DY_PUBLIC_BASE_URL=https://dy.nomadro.com npm run build >/tmp/fe-egress.log 2>&1 echo EXIT:$? >>/tmp/fe-egress.log """ with sftp.file("/tmp/_fe_egress.sh", "wb") as f: f.write(fe.replace(b"\r\n", b"\n")) sftp.close() print( run( g, f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '" + "; ".join(installs) + "; " "DEBIAN_FRONTEND=noninteractive apt-get install -y proxychains4 >/tmp/apt-pc.log 2>&1 || true; " "command -v proxychains4; " "systemctl restart douyinweb-api; sleep 2; systemctl is-active douyinweb-api; " "systemctl stop douyinweb-web || true; " "chmod 755 /tmp/_fe_egress.sh; rm -f /tmp/fe-egress.log; " "nohup /bin/bash /tmp/_fe_egress.sh >/dev/null 2>&1 & echo BUILD'" ), flush=True, ) g.close() for i in range(55): time.sleep(12) g = ssh_guest() st = run(g, "grep '^EXIT:' /tmp/fe-egress.log 2>/dev/null | tail -1") g.close() print(f"fe[{i}] {st.strip()}", flush=True) if "EXIT:0" in st: break if "EXIT:" in st and "EXIT:0" not in st: g = ssh_guest() print(run(g, "tail -60 /tmp/fe-egress.log"), flush=True) g.close() raise SystemExit("fe fail") else: raise SystemExit("fe timeout") g = ssh_guest() print( run( g, f"echo '{GUEST_PW}' | sudo -S -p '' systemctl start douyinweb-web; " "sleep 3; systemctl is-active douyinweb-web douyinweb-api", ), flush=True, ) g.close() with httpx.Client(base_url="https://dy.nomadro.com", timeout=180.0, follow_redirects=True) as client: client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"}) net = client.get("/api/network") print("network", net.status_code, flush=True) if net.status_code != 200: print(net.text[:500], flush=True) raise SystemExit("network api fail") body = net.json() eg = body.get("egress") or {} print("egress_mode", eg.get("mode"), "pc", (eg.get("proxychains") or {}).get("installed"), flush=True) applied = client.patch( "/api/network/egress", json={ "mode": "shellcrash", "chain_residential": False, "cover": {"neko": True, "relay": True, "uploads": True}, "refresh_neko": True, }, ) print("apply", applied.status_code, flush=True) if applied.status_code != 200: print(applied.text[:800], flush=True) raise SystemExit("egress apply fail") aj = applied.json() eg2 = aj.get("egress") or {} print( "applied mode", eg2.get("mode"), "chain", eg2.get("chain_residential"), "exit", eg2.get("exit_node"), flush=True, ) probe = client.post("/api/network/egress/probe") print("probe", probe.status_code, flush=True) pj = probe.json() for row in pj.get("probes") or []: print( " ", row.get("target"), "ok" if row.get("ok") else "FAIL", row.get("http_code"), row.get("via"), flush=True, ) page = client.get("/network") print("page", page.status_code, "出海" in page.text or True, flush=True) # check js chunk import re chunks = re.findall(r"/_next/static/chunks/app/network/[^\"']+", page.text) found = False for u in sorted(set(chunks)): t = client.get(u).text if "业务出海" in t or "\u4e1a\u52a1\u51fa\u6d77" in t: found = True print("ui_chunk_ok", u, flush=True) break print("ui_found", found, flush=True) print("dylive", httpx.get("https://dylive.nomadro.com/api/health", timeout=15).status_code, flush=True) # Confirm neko compose proxy line g = ssh_guest() print( run( g, "grep -n NEKO_BROWSER_PROXY /opt/douyinWEB/backend/data/neko/docker-compose.yml 2>/dev/null | head -5; " "test -f /opt/douyinWEB/config/proxychains.conf && head -20 /opt/douyinWEB/config/proxychains.conf || echo no-pc-conf", ), flush=True, ) g.close() print("DONE", flush=True) return 0 if __name__ == "__main__": raise SystemExit(main())