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>
237 lines
9.0 KiB
Python
237 lines
9.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Deploy ShellCrash hub/yaml/UI; rebuild FE; final verify."""
|
|
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"
|
|
|
|
|
|
def ssh_guest(tries: int = 8):
|
|
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 = 120) -> 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()
|
|
for rel, remote in (
|
|
("backend/app/shellcrash_hub.py", "/tmp/_up_shellcrash_hub.py"),
|
|
("backend/app/network_hub.py", "/tmp/_up_network_hub.py"),
|
|
("backend/app/clash_assets/clash.yaml", "/tmp/_up_clash.yaml"),
|
|
("frontend/src/app/network/page.tsx", "/tmp/_up_page.tsx"),
|
|
("scripts/linux/install_shellcrash.sh", "/tmp/_up_install_sc.sh"),
|
|
):
|
|
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
|
|
with sftp.file(remote, "wb") as f:
|
|
f.write(data)
|
|
print("put", rel, flush=True)
|
|
patch = b"""from pathlib import Path
|
|
for p in [Path("/etc/ShellCrash/yamls/config.yaml"), Path("/tmp/ShellCrash/config.yaml")]:
|
|
if not p.is_file():
|
|
continue
|
|
lines = []
|
|
ec = mp = False
|
|
for line in p.read_text(encoding="utf-8", errors="replace").splitlines():
|
|
if line.startswith("global-client-fingerprint:"):
|
|
continue
|
|
if line.startswith("external-controller:"):
|
|
lines.append("external-controller: 0.0.0.0:9999")
|
|
ec = True
|
|
continue
|
|
if line.startswith("mixed-port:"):
|
|
lines.append("mixed-port: 7890")
|
|
mp = True
|
|
continue
|
|
lines.append(line)
|
|
if not ec:
|
|
lines.append("external-controller: 0.0.0.0:9999")
|
|
if not mp:
|
|
lines.append("mixed-port: 7890")
|
|
p.write_text("\\n".join(lines).rstrip() + "\\n", encoding="utf-8")
|
|
print("patched")
|
|
"""
|
|
with sftp.file("/tmp/_patch_sc_yaml.py", "wb") as f:
|
|
f.write(patch.replace(b"\\n", b"\n") if False else patch) # noqa: keep
|
|
# write correctly
|
|
with sftp.file("/tmp/_patch_sc_yaml.py", "wb") as f:
|
|
f.write(
|
|
b"from pathlib import Path\n"
|
|
b"for p in [Path('/etc/ShellCrash/yamls/config.yaml'), Path('/tmp/ShellCrash/config.yaml')]:\n"
|
|
b" if not p.is_file():\n"
|
|
b" continue\n"
|
|
b" lines=[]; ec=mp=False\n"
|
|
b" for line in p.read_text(encoding='utf-8', errors='replace').splitlines():\n"
|
|
b" if line.startswith('global-client-fingerprint:'): continue\n"
|
|
b" if line.startswith('external-controller:'):\n"
|
|
b" lines.append('external-controller: 0.0.0.0:9999'); ec=True; continue\n"
|
|
b" if line.startswith('mixed-port:'):\n"
|
|
b" lines.append('mixed-port: 7890'); mp=True; continue\n"
|
|
b" lines.append(line)\n"
|
|
b" if not ec: lines.append('external-controller: 0.0.0.0:9999')\n"
|
|
b" if not mp: lines.append('mixed-port: 7890')\n"
|
|
b" p.write_text('\\n'.join(lines).rstrip()+'\\n', encoding='utf-8')\n"
|
|
b"print('patched')\n"
|
|
)
|
|
sftp.close()
|
|
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
|
|
"set -e; "
|
|
"install -m 644 /tmp/_up_shellcrash_hub.py /opt/douyinWEB/backend/app/shellcrash_hub.py; "
|
|
"install -m 644 /tmp/_up_network_hub.py /opt/douyinWEB/backend/app/network_hub.py; "
|
|
"install -m 644 /tmp/_up_page.tsx /opt/douyinWEB/frontend/src/app/network/page.tsx; "
|
|
"install -m 755 /tmp/_up_install_sc.sh /opt/douyinWEB/scripts/linux/install_shellcrash.sh; "
|
|
"mkdir -p /opt/douyinWEB/backend/app/clash_assets /etc/ShellCrash/yamls /tmp/ShellCrash; "
|
|
"install -m 644 /tmp/_up_clash.yaml /opt/douyinWEB/backend/app/clash_assets/clash.yaml; "
|
|
"install -m 644 /tmp/_up_clash.yaml /etc/ShellCrash/yamls/config.yaml; "
|
|
"install -m 644 /tmp/_up_clash.yaml /tmp/ShellCrash/config.yaml; "
|
|
"python3 /tmp/_patch_sc_yaml.py; "
|
|
"systemctl restart shellcrash; "
|
|
"systemctl restart douyinweb-api; "
|
|
"sleep 4; "
|
|
"systemctl is-active shellcrash douyinweb-api"
|
|
"'",
|
|
90,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
# Prefer node via uploaded helper
|
|
print("=== pick via local python ===", flush=True)
|
|
sftp = g.open_sftp()
|
|
with sftp.file("/tmp/_sc_pick.py", "wb") as f:
|
|
f.write(
|
|
b"import sys\n"
|
|
b"sys.path.insert(0, '/opt/douyinWEB/backend')\n"
|
|
b"from app import shellcrash_hub\n"
|
|
b"print(shellcrash_hub.pick_best_node())\n"
|
|
b"st = shellcrash_hub.status()\n"
|
|
b"print('status', {k: st.get(k) for k in "
|
|
b"('installed','running','unit','selected_node','firewall_area','mixed_port')})\n"
|
|
)
|
|
sftp.close()
|
|
print(run(g, "/opt/douyinWEB/backend/.venv/bin/python /tmp/_sc_pick.py", 120), flush=True)
|
|
|
|
# FE build
|
|
fe = (
|
|
"#!/bin/bash\n"
|
|
"cd /opt/douyinWEB/frontend || exit 1\n"
|
|
"export DY_INTERNAL_API_URL=http://127.0.0.1:8014\n"
|
|
"export DY_PUBLIC_BASE_URL=https://dy.nomadro.com\n"
|
|
"npm run build >/tmp/fe-sc.log 2>&1\n"
|
|
"echo EXIT:$? >>/tmp/fe-sc.log\n"
|
|
)
|
|
sftp = g.open_sftp()
|
|
with sftp.file("/tmp/_fe_sc.sh", "wb") as f:
|
|
f.write(fe.encode().replace(b"\r\n", b"\n"))
|
|
sftp.close()
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
|
|
"chmod 755 /tmp/_fe_sc.sh; rm -f /tmp/fe-sc.log; "
|
|
"systemctl stop douyinweb-web || true; "
|
|
"nohup /bin/bash /tmp/_fe_sc.sh >/dev/null 2>&1 & echo BUILD'"
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
|
|
for i in range(50):
|
|
time.sleep(12)
|
|
g = ssh_guest()
|
|
st = run(g, "grep '^EXIT:' /tmp/fe-sc.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 -40 /tmp/fe-sc.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 shellcrash wg-quick@wg-dy douyinweb-api; "
|
|
"env -u http_proxy -u https_proxy -u HTTP_PROXY -u HTTPS_PROXY "
|
|
"curl -sS -m 10 -o /dev/null -w mixed:%{http_code}\\\\n -x http://127.0.0.1:7890 "
|
|
"https://www.google.com/generate_204 || echo mixed:fail",
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
|
|
with httpx.Client(base_url="https://dy.nomadro.com", timeout=30.0, follow_redirects=False) as client:
|
|
client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
|
|
j = client.get("/api/network").json()
|
|
sc = j.get("shellcrash") or {}
|
|
print(
|
|
"hub sc",
|
|
sc.get("installed"),
|
|
sc.get("running"),
|
|
sc.get("selected_node"),
|
|
sc.get("firewall_area"),
|
|
flush=True,
|
|
)
|
|
pick = client.post("/api/network/shellcrash/pick", json={})
|
|
print("pick api", pick.status_code, flush=True)
|
|
try:
|
|
pj = pick.json()
|
|
print("pick", (pj.get("pick") or {}).get("picked"), (pj.get("pick") or {}).get("ok"), flush=True)
|
|
except Exception:
|
|
print(pick.text[:300], flush=True)
|
|
cl = (client.get("/api/network").json() or {}).get("physical_checklist") or {}
|
|
print("checklist ok=", cl.get("ok"), "failed=", cl.get("failed"), flush=True)
|
|
for c in cl.get("checks") or []:
|
|
print(f" [{'OK' if c.get('ok') else '!!'}] {c.get('name')}: {c.get('detail')}", flush=True)
|
|
print("page", client.get("/network").status_code, flush=True)
|
|
print("dylive", httpx.get("https://dylive.nomadro.com/api/health", timeout=15).status_code, flush=True)
|
|
|
|
print("DONE", flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|