# -*- coding: utf-8 -*- """Install Zashboard into ShellCrash ui/ and patch Caddy + web_panel deploy.""" from __future__ import annotations import sys import time from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(__file__).resolve().parents[1] VPS = "107.173.30.245" ROOT_PASS = "Xiao4669805" GUEST_USER = "eric" GUEST_PASS = "4669805" GUEST_PORT = 4014 WG = "10.8.0.2" DOMAIN = "dy.nomadro.com" def ssh(port: int, user: str, password: str, tries: int = 6) -> paramiko.SSHClient: last = None for _ in range(tries): try: 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 except Exception as e: last = e time.sleep(2) raise RuntimeError(last) def run(c: paramiko.SSHClient, cmd: str, t: int = 120) -> str: _, o, e = c.exec_command(cmd, timeout=t) out = (o.read() + e.read()).decode("utf-8", "replace") return out INSTALL_UI_SH = r"""#!/usr/bin/env bash set -euo pipefail UI=/etc/ShellCrash/ui TMP=$(mktemp -d) cd "$TMP" # Prefer GitHub release dist; fallback mirror if needed URLS=( "https://github.com/Zephyruso/zashboard/releases/latest/download/dist-cdn-fonts.zip" "https://github.com/Zephyruso/zashboard/releases/latest/download/dist.zip" "https://ghproxy.net/https://github.com/Zephyruso/zashboard/releases/latest/download/dist.zip" ) ok=0 for u in "${URLS[@]}"; do echo "try $u" if curl -fsSL -m 90 -o dist.zip "$u"; then ok=1 break fi done test "$ok" = 1 rm -rf "$UI.new" mkdir -p "$UI.new" unzip -q dist.zip -d "$UI.new" # zip may contain dist/ top folder if [[ -d "$UI.new/dist" ]]; then shopt -s dotglob mv "$UI.new/dist"/* "$UI.new"/ rmdir "$UI.new/dist" fi test -f "$UI.new/index.html" # keep a tiny backup of placeholder mkdir -p /etc/ShellCrash/ui.bak.placeholder cp -a "$UI"/. /etc/ShellCrash/ui.bak.placeholder/ 2>/dev/null || true rm -rf "$UI" mv "$UI.new" "$UI" chown -R root:shellcrash "$UI" || chown -R root:root "$UI" chmod -R a+rX "$UI" rm -rf "$TMP" echo UI_INSTALLED ls -la "$UI" | head -15 curl -sS -m 3 -o /dev/null -w "ui:%{http_code}\n" http://127.0.0.1:9999/ui/ curl -sS -m 3 http://127.0.0.1:9999/ui/ | head -c 200; echo """ def patch_caddy(v: paramiko.SSHClient) -> None: site = f"/etc/caddy/sites/{DOMAIN}.caddy" cur = run(v, f"cat {site}") if "handle_path /shellcrash-ui/" in cur: print("caddy already has shellcrash-ui", flush=True) return block = f""" handle /shellcrash-ui {{ redir /shellcrash-ui/ 308 }} handle_path /shellcrash-ui/* {{ reverse_proxy {WG}:9999 {{ header_up Host {{host}} header_up X-Forwarded-Proto {{scheme}} }} }} """ # Insert before catch-all handle { marker = "\thandle {\n\t\treverse_proxy 10.8.0.2:4015" if marker not in cur: raise SystemExit("caddy catch-all marker not found") new = cur.replace(marker, block + "\n" + marker, 1) sftp = v.open_sftp() bak = site + f".bak.{int(time.time())}" run(v, f"cp -a {site} {bak}") with sftp.file(site, "w") as f: f.write(new) sftp.close() print(run(v, "caddy validate --config /etc/caddy/Caddyfile && systemctl reload caddy && echo CADDY_OK"), flush=True) print(run(v, f"grep -n shellcrash /etc/caddy/sites/{DOMAIN}.caddy"), flush=True) def main() -> None: g = ssh(GUEST_PORT, GUEST_USER, GUEST_PASS) print("=== install zashboard ui ===", flush=True) # write script via sudo tee b64 = __import__("base64").b64encode(INSTALL_UI_SH.encode()).decode() out = run( g, f"echo {b64} | base64 -d > /tmp/install_sc_ui.sh && " f"echo '{GUEST_PASS}' | sudo -S -p '' bash /tmp/install_sc_ui.sh", t=180, ) print(out, flush=True) if "UI_INSTALLED" not in out: raise SystemExit("ui install failed") g.close() v = ssh(22, "root", ROOT_PASS) print("=== patch caddy ===", flush=True) patch_caddy(v) print("=== probe via wg and public ===", flush=True) print( run( v, "curl -sS -m 5 -o /dev/null -w 'wg_ui:%{http_code}\\n' http://10.8.0.2:9999/ui/; " "curl -sS -m 5 -o /dev/null -w 'wg_ver:%{http_code}\\n' http://10.8.0.2:9999/version; " f"curl -sS -m 8 -o /dev/null -w 'pub_ui:%{{http_code}}\\n' https://{DOMAIN}/shellcrash-ui/ui/; " f"curl -sS -m 8 -o /dev/null -w 'pub_ver:%{{http_code}}\\n' https://{DOMAIN}/shellcrash-ui/version; " f"curl -sS -m 8 https://{DOMAIN}/shellcrash-ui/ui/ | head -c 180; echo", ), flush=True, ) v.close() print("DONE_PHASE1", flush=True) if __name__ == "__main__": main()