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

131 lines
4.9 KiB
Python

# -*- coding: utf-8 -*-
"""Proxy ShellCrash UI+API on dy.nomadro.com /ui and clash paths → WG:9999."""
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]
HUB = ROOT / "backend" / "app" / "shellcrash_hub.py"
VPS = "107.173.30.245"
ROOT_PASS = "Xiao4669805"
GUEST_USER, GUEST_PASS, GUEST_PORT = "eric", "4669805", 4014
DOMAIN = "dy.nomadro.com"
WG = "10.8.0.2"
# Mihomo external-controller routes used by Zashboard (same-origin under /ui).
CLASH_HANDLES = f"""
# ShellCrash / Zashboard on this host (SPA base /ui + Clash API at /)
handle /ui {{
redir /ui/ 308
}}
@clash path /ui/* /version /proxies /proxies/* /configs /configs/* /rules /rules/* /connections /connections/* /providers /providers/* /cache /cache/* /dns /dns/* /debug /debug/* /traffic /logs /memory /restart /upgrade /upgrade/* /group /group/* /script /script/* /fakeip /fakeip/* /endpoint /endpoint/* /runtime /runtime/* /meta /meta/* /schema /schema/* /flushfakeip /externaloverride /externaloverride/* /storage /storage/*
handle @clash {{
reverse_proxy {WG}:9999 {{
header_up Host {{host}}
}}
}}
"""
def ssh(port, user, password):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(VPS, port, user, password, timeout=40, banner_timeout=80, allow_agent=False, look_for_keys=False)
return c
def run(c, cmd, t=120):
_, o, e = c.exec_command(cmd, timeout=t)
return (o.read() + e.read()).decode("utf-8", "replace")
def patch_caddy(v):
site = f"/etc/caddy/sites/{DOMAIN}.caddy"
cur = run(v, f"cat {site}")
if "@clash path /ui/*" in cur or "handle /ui {" in cur and "@clash" in cur:
print("caddy clash handles already present", flush=True)
# still ensure block is correct — rewrite if old shellcrash-only
# Remove outdated incomplete /ui-only if any, then insert before catch-all
if "@clash path /ui/*" not in cur:
# drop old shellcrash-ui-only if we want both; keep shellcrash-ui for secondaryPath compat
marker = "\thandle {\n\t\treverse_proxy 10.8.0.2:4015"
if marker not in cur:
raise SystemExit("catch-all marker missing")
if "handle /ui {" in cur and "@clash" not in cur:
# unlikely
pass
new = cur.replace(marker, CLASH_HANDLES + "\n" + marker, 1)
bak = site + f".bak.{int(time.time())}"
run(v, f"cp -a {site} {bak}")
sftp = v.open_sftp()
with sftp.file(site, "w") as f:
f.write(new)
sftp.close()
out = run(v, "caddy validate --config /etc/caddy/Caddyfile && systemctl reload caddy && echo CADDY_OK")
print(out[-800:], flush=True)
print(run(v, f"grep -n 'ui\\|clash\\|shellcrash' {site}"), flush=True)
def deploy_hub(g):
sftp = g.open_sftp()
sftp.put(str(HUB), "/tmp/_up_shellcrash_hub.py")
sftp.close()
print(
run(
g,
f"echo '{GUEST_PASS}' | sudo -S -p '' bash -c '"
"install -m 644 /tmp/_up_shellcrash_hub.py /opt/douyinWEB/backend/app/shellcrash_hub.py; "
"cd /opt/douyinWEB/backend && .venv/bin/python -m py_compile app/shellcrash_hub.py && "
"systemctl restart douyinweb-api'"
),
flush=True,
)
for _ in range(12):
time.sleep(2)
code = run(g, "curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health").strip()
print("health", code, flush=True)
if code == "200":
break
def main():
# update hub source first
text = HUB.read_text(encoding="utf-8")
if 'f"{public}/ui/#/proxies"' not in text:
# will be written by StrReplace before this script; just deploy whatever is on disk
pass
v = ssh(22, "root", ROOT_PASS)
print("=== caddy ===", flush=True)
patch_caddy(v)
v.close()
g = ssh(GUEST_PORT, GUEST_USER, GUEST_PASS)
print("=== api hub ===", flush=True)
deploy_hub(g)
g.close()
with httpx.Client(base_url=f"https://{DOMAIN}", timeout=25, follow_redirects=True) as client:
for path in ("/ui/", "/ui/#/proxies", "/version", "/proxies"):
# fragment not sent to server for /ui/#/proxies — probe /ui/
if "#" in path:
continue
r = client.get(path)
print(path, r.status_code, (r.headers.get("content-type") or "")[:48], (r.text[:60] if path != "/proxies" else "json_ok"), flush=True)
r = client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
print("login", r.status_code, flush=True)
wp = client.post("/api/network/shellcrash/web_panel").json()
print("web_panel.url", wp.get("url"), flush=True)
print("web_panel.direct", wp.get("direct_url"), flush=True)
if __name__ == "__main__":
main()