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

162 lines
5.8 KiB
Python

# -*- coding: utf-8 -*-
"""Install ShellCrash on physical guest via FRP; temp LAN Clash proxy then clear."""
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"
LAN_PROXY = "http://192.168.21.36:7897"
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 = 300) -> str:
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
def put_lf(sftp, local: Path, remote: str) -> None:
data = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
with sftp.file(remote, "wb") as f:
f.write(data)
def main() -> int:
g = ssh_guest()
print("=== probe LAN proxy from guest ===", flush=True)
print(
run(
g,
"curl -sS -m 5 -o /dev/null -w 'direct_google:%{http_code}\\n' https://www.google.com/generate_204 || echo direct_google:fail; "
f"curl -sS -m 8 -x '{LAN_PROXY}' -o /dev/null -w 'via_lan_proxy:%{{http_code}}\\n' https://www.google.com/generate_204 || echo via_lan_proxy:fail; "
f"curl -sS -m 5 -o /dev/null -w 'proxy_tcp:%{{http_code}}\\n' {LAN_PROXY.replace('http://', 'http://')} || true; "
"ip -br a | head -8; ping -c1 -W2 192.168.21.36 >/dev/null && echo ping_proxy_host:ok || echo ping_proxy_host:fail",
40,
),
flush=True,
)
sftp = g.open_sftp()
put_lf(sftp, ROOT / "scripts/linux/install_shellcrash.sh", "/tmp/install_shellcrash_dy.sh")
yaml = ROOT / "backend/app/clash_assets/clash.yaml"
if yaml.is_file():
sftp.put(str(yaml), "/tmp/clash_stage.yaml")
print("put clash.yaml", flush=True)
else:
print("WARN no clash.yaml", flush=True)
sftp.close()
# Long install — Core + geo via proxy
print("=== install ShellCrash ===", flush=True)
out = run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
"set -e; "
"chmod +x /tmp/install_shellcrash_dy.sh; "
"install -m 755 /tmp/install_shellcrash_dy.sh /opt/douyinWEB/scripts/linux/install_shellcrash.sh; "
f"export LIVE_HTTP_PROXY={LAN_PROXY}; "
f"export LIVE_HTTPS_PROXY={LAN_PROXY}; "
"export STAGE_YAML=/tmp/clash_stage.yaml; "
"export ENABLE_NOW=1; "
"export SHELLCRASH_TRUSTED_CIDRS=10.8.0.0/24,172.16.0.0/12,192.168.0.0/16,10.0.0.0/8; "
"bash /opt/douyinWEB/scripts/linux/install_shellcrash.sh; "
"echo INSTALL_EXIT:$?; "
"# ensure proxy leftovers gone "
"rm -f /etc/apt/apt.conf.d/99douyinweb-tmp-proxy; "
"unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy; "
"echo PROXY_CLEARED; "
"systemctl is-active shellcrash.service || true; "
"test -x /etc/ShellCrash/start.sh && echo HAS_START; "
"test -x /tmp/ShellCrash/CrashCore && echo HAS_CORE; "
"ss -ltn | grep -E \":(7890|9999)[[:space:]]\" || true; "
"curl -sS -m 5 -o /dev/null -w via_sc_mixed:%{http_code}\\\\n -x http://127.0.0.1:7890 https://www.google.com/generate_204 || echo via_sc_mixed:fail"
"'",
600,
)
print(out, flush=True)
# Double-clear proxy + don't leave env in profile
print(
run(
g,
f"echo '{GUEST_PW}' | sudo -S -p '' bash -c '"
"rm -f /etc/apt/apt.conf.d/99douyinweb-tmp-proxy /etc/apt/apt.conf.d/99live-proxy; "
"if [ -f /tmp/.douyinweb_docker_proxy_marker ]; then "
" rm -f /etc/systemd/system/docker.service.d/http-proxy.conf /tmp/.douyinweb_docker_proxy_marker; "
" systemctl daemon-reload; "
"fi; "
"echo cleared; ls /etc/apt/apt.conf.d/ | grep -i proxy || echo no_apt_proxy"
"'",
),
flush=True,
)
# Keep WG + app healthy
print(
run(
g,
"systemctl is-active wg-quick@wg-dy douyinweb-api douyinweb-web frpp shellcrash 2>/dev/null; "
"curl -sS -m 3 -o /dev/null -w local_api:%{http_code}\\\\n http://127.0.0.1:8014/api/health",
),
flush=True,
)
g.close()
print("=== API /network shellcrash ===", flush=True)
with httpx.Client(base_url="https://dy.nomadro.com", timeout=25.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(
"installed=",
sc.get("installed"),
"running=",
sc.get("running"),
"unit=",
sc.get("unit"),
"mixed=",
sc.get("mixed_port"),
"yaml=",
(sc.get("config") or {}).get("path"),
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())