# -*- coding: utf-8 -*-
"""Fix /ui blank page: force /ui → /ui/ redirect + ."""
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, ROOT_PASS = "107.173.30.245", "Xiao4669805"
GUEST_USER, GUEST_PASS, GUEST_PORT = "eric", "4669805", 4014
DOMAIN = "dy.nomadro.com"
SITE = f"/etc/caddy/sites/{DOMAIN}.caddy"
CLASH_BLOCK = """
# ShellCrash Zashboard: MUST redirect /ui → /ui/ or relative ./assets 404 (blank page)
@ui_exact path /ui
handle @ui_exact {
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 10.8.0.2:9999 {
header_up Host {host}
}
}
"""
PATCH_UI_SH = r"""#!/usr/bin/env bash
set -euo pipefail
IDX=/etc/ShellCrash/ui/index.html
test -f "$IDX"
# idempotent base href so ./assets resolve under /ui even if path is wrong briefly
if ! grep -q 'href="/ui/"' "$IDX"; then
sed -i 's|
|\n |' "$IDX"
fi
grep -n base "$IDX" | head -5
curl -sS -m 3 -o /dev/null -w "local_ui:%{http_code}\n" http://127.0.0.1:9999/ui/
"""
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=90):
_, o, e = c.exec_command(cmd, timeout=t)
return (o.read() + e.read()).decode("utf-8", "replace")
def main():
v = ssh(22, "root", ROOT_PASS)
cur = run(v, f"cat {SITE}")
# replace old clash/ui block (from comment through handle @clash closing brace)
import re
new = cur
# remove previous variants
new = re.sub(
r"\n\t# ShellCrash[^\n]*\n(?:\t.*\n)*?\thandle @clash \{\n\t\treverse_proxy 10\.8\.0\.2:9999 \{\n\t\t\theader_up Host \{host\}\n\t\t\}\n\t\}\n",
"\n",
new,
count=1,
)
new = re.sub(
r"\n\t# ShellCrash / Zashboard[^\n]*\n(?:\t.*\n)*?\thandle @clash \{\n\t\treverse_proxy 10\.8\.0\.2:9999 \{\n\t\t\theader_up Host \{host\}\n\t\t\}\n\t\}\n",
"\n",
new,
count=1,
)
# also remove orphaned handle /ui redir if left
new = re.sub(r"\n\thandle /ui \{\n\t\tredir /ui/ 308\n\t\}\n", "\n", new)
new = re.sub(r"\n\t@ui_exact path /ui\n\thandle @ui_exact \{\n\t\tredir /ui/ 308\n\t\}\n", "\n", new)
marker = "\thandle {\n\t\treverse_proxy 10.8.0.2:4015"
if marker not in new:
raise SystemExit("marker missing after cleanup")
if "@ui_exact path /ui" not in new:
new = new.replace(marker, CLASH_BLOCK + "\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()
print(run(v, "caddy validate --config /etc/caddy/Caddyfile && systemctl reload caddy && echo CADDY_OK"), flush=True)
print(run(v, f"grep -n 'ui_exact\\|@clash\\|handle /ui' {SITE}"), flush=True)
# probe via caddy locally
print(
run(
v,
"curl -sSI https://127.0.0.1/ui -H 'Host: dy.nomadro.com' --resolve dy.nomadro.com:443:127.0.0.1 | head -20; "
"echo ---; "
"curl -sSI https://dy.nomadro.com/ui | head -20",
),
flush=True,
)
v.close()
g = ssh(GUEST_PORT, GUEST_USER, GUEST_PASS)
import base64
b64 = base64.b64encode(PATCH_UI_SH.encode()).decode()
print(
run(g, f"echo {b64} | base64 -d > /tmp/patch_ui_base.sh && echo '{GUEST_PASS}' | sudo -S -p '' bash /tmp/patch_ui_base.sh"),
flush=True,
)
sftp = g.open_sftp()
sftp.put(str(HUB), "/tmp/_up_shellcrash_hub.py")
sftp.close()
# ensure URL uses /ui/ before hash
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; "
"systemctl restart douyinweb-api'"
),
flush=True,
)
for _ in range(10):
time.sleep(2)
if run(g, "curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health").strip() == "200":
break
g.close()
with httpx.Client(timeout=25, follow_redirects=False) as c:
r = c.get(f"https://{DOMAIN}/ui")
print("GET /ui", r.status_code, "loc", r.headers.get("location"), "len", len(r.content), flush=True)
with httpx.Client(timeout=25, follow_redirects=True) as c:
r = c.get(f"https://{DOMAIN}/ui")
print("follow /ui ->", r.url, r.status_code, "has_base", '