# -*- coding: utf-8 -*- """Deploy TUIC-first shellcrash + VLESS soft-guard (upload remote.sh; no nested heredoc).""" from __future__ import annotations import json import sys import time from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB") TUIC = "1d2da142-singbox_tuic5" REMOTE_SH = r"""#!/bin/bash set -euo pipefail TUIC='1d2da142-singbox_tuic5' mkdir -p /opt/douyinWEB/scripts/linux /opt/douyinWEB/backend/app /etc/systemd/system/douyinweb-api.service.d install -m 644 /tmp/_dy_sc_hub.py /opt/douyinWEB/backend/app/shellcrash_hub.py # Force LF: Windows SFTP can leave CRLF which makes bash exit 2 immediately. python3 - <<'PY' from pathlib import Path src=Path('/tmp/_dy_sc_guard.py') dst=Path('/opt/douyinWEB/scripts/linux/shellcrash_tuic_guard.py') text=src.read_bytes().replace(b'\r\n', b'\n').replace(b'\r', b'\n') dst.write_bytes(text) dst.chmod(0o755) print('wrote', dst, 'bytes', len(text)) PY install -m 644 /tmp/_dy_sc_guard.service /etc/systemd/system/douyinweb-shellcrash-tuic-guard.service cat >/etc/systemd/system/douyinweb-api.service.d/shellcrash-tuic.conf </dev/null; then echo "scrub $f" sed -i "s|Environment=DY_SHELLCRASH_DEFAULT_NODE=.*|Environment=DY_SHELLCRASH_DEFAULT_NODE=${TUIC}|g" "$f" || true fi done shopt -u nullglob systemctl daemon-reload systemctl reset-failed douyinweb-shellcrash-tuic-guard.service || true systemctl enable --now douyinweb-shellcrash-tuic-guard.service # Hub/env already live from prior deploy; avoid another API bounce unless module missing TUIC default. need_api=0 if ! grep -q 'singbox_tuic5' /opt/douyinWEB/backend/app/shellcrash_hub.py; then need_api=1; fi if ! systemctl show douyinweb-api.service -p Environment --no-pager 2>/dev/null | grep -q 'DY_SHELLCRASH_DEFAULT_NODE=1d2da142-singbox_tuic5'; then need_api=1; fi if [ "$need_api" = 1 ]; then systemctl restart douyinweb-api.service sleep 5 fi sleep 2 echo "=== units ===" systemctl is-active douyinweb-api.service douyinweb-shellcrash-tuic-guard.service || true systemctl --no-pager -l status douyinweb-shellcrash-tuic-guard.service 2>/dev/null | sed -n '1,16p' || true systemctl is-active douyinweb-wireguard-watch.service douyinweb-neko-watch.service douyinweb-api-watch.service 2>/dev/null || true echo "=== clash now ===" python3 <<'PY' import json, urllib.request, urllib.parse, os ctrl='http://127.0.0.1:9999' tuic='1d2da142-singbox_tuic5' try: with urllib.request.urlopen(ctrl+'/proxies', timeout=5) as r: ps=json.load(r).get('proxies') or {} except Exception as e: print('clash_api', e) ps={} now=None for g in ('🚀 节点选择','GLOBAL'): now=(ps.get(g) or {}).get('now') if now: print('selected_before', g, now) break if now and 'vless' in now.lower(): for g in ('🚀 节点选择','GLOBAL'): if g != 'GLOBAL' and g not in ps: continue enc=urllib.parse.quote(g, safe='') req=urllib.request.Request( ctrl+'/proxies/'+enc, data=json.dumps({'name': tuic}).encode(), method='PUT', headers={'Content-Type':'application/json'}, ) try: urllib.request.urlopen(req, timeout=5) print('reverted', g, '->', tuic) except Exception as e: print('revert_fail', g, e) else: print('keep', now) PY echo "=== code defaults ===" python3 <<'PY' import importlib.util spec=importlib.util.spec_from_file_location('sc','/opt/douyinWEB/backend/app/shellcrash_hub.py') m=importlib.util.module_from_spec(spec) spec.loader.exec_module(m) print('DEFAULT_NODE', m.DEFAULT_NODE) print('ALLOW_VLESS', m.ALLOW_VLESS_AUTOPICK) print('family', m._node_family(m.DEFAULT_NODE)) print('candidates0', m.CANDIDATE_NODES[0]) PY echo "=== api env ===" systemctl show douyinweb-api.service -p Environment --no-pager 2>/dev/null | tr ' ' '\n' | grep -E 'SHELLCRASH|VLESS|TUIC' || true echo "=== grep hub ===" grep -nE 'DEFAULT_NODE|ALLOW_VLESS|never auto|TUIC only' /opt/douyinWEB/backend/app/shellcrash_hub.py | head -15 """ GUARD_UNIT = """[Unit] Description=douyinWEB ShellCrash TUIC guard (block VLESS autopick) After=network.target [Service] Type=simple Environment=DY_SHELLCRASH_DEFAULT_NODE=1d2da142-singbox_tuic5 Environment=DY_SHELLCRASH_ALLOW_VLESS= Environment=DY_SHELLCRASH_GUARD_INTERVAL=60 ExecStart=/usr/bin/python3 /opt/douyinWEB/scripts/linux/shellcrash_tuic_guard.py Restart=always RestartSec=15 [Install] WantedBy=multi-user.target """ def deploy(label: str, host: str, port: int, user: str, pw: str, *, sudo: bool) -> None: print(f"\n===== deploy {label} =====") c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host, port, user, pw, timeout=45, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() sftp.put(str(ROOT / "backend/app/shellcrash_hub.py"), "/tmp/_dy_sc_hub.py") # Upload guard as bytes with LF only guard = (ROOT / "scripts/linux/shellcrash_tuic_guard.py").read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") with sftp.file("/tmp/_dy_sc_guard.py", "wb") as f: f.write(guard) with sftp.file("/tmp/_dy_sc_guard.service", "w") as f: f.write(GUARD_UNIT.replace("\r\n", "\n")) remote = REMOTE_SH.replace("\r\n", "\n") with sftp.file("/tmp/_dy_sc_remote.sh", "w") as f: f.write(remote) sftp.close() if sudo: cmd = f"echo '{pw}' | sudo -S -p '' bash /tmp/_dy_sc_remote.sh" else: cmd = "bash /tmp/_dy_sc_remote.sh" _, o, e = c.exec_command(cmd, timeout=180) out = (o.read() + e.read()).decode("utf-8", "replace") print(out) c.close() def public_sc_status(base: str) -> None: import http.cookiejar import urllib.request cj = http.cookiejar.CookieJar() op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) data = json.dumps({"username": "hackrobot", "password": "Xiao4669805@"}).encode() req = urllib.request.Request( base + "/api/auth/login", data=data, method="POST", headers={"Content-Type": "application/json"} ) try: op.open(req, timeout=35) with op.open(urllib.request.Request(base + "/api/network/shellcrash/status"), timeout=45) as r: d = json.loads(r.read().decode()) sc = d.get("shellcrash") or d print( base, "selected=", sc.get("selected_node"), "family=", sc.get("selected_family"), "default=", sc.get("default_node"), "allow_vless=", sc.get("allow_vless_autopick"), "running=", sc.get("running"), ) except Exception as exc: print(base, "status_err", exc) deploy("phys", "107.173.30.245", 4014, "eric", "4669805", sudo=True) deploy("vps", "107.173.30.245", 22, "root", "Xiao4669805", sudo=False) time.sleep(4) print("\n===== public verify =====") for b in ("https://dy.nomadro.com", "https://dylive.nomadro.com"): public_sc_status(b) print("done")