# -*- coding: utf-8 -*- """Install DouyinWEB-owned sing-box binary + fix residential apply on both VPS. NEVER uses /etc/v2ray-agent/sing-box/sing-box. NEVER restarts host sing-box.service. """ from __future__ import annotations import io import sys import tarfile from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(__file__).resolve().parents[1] PASS = "Xiao4669805" JUMP = "107.173.30.245" HOSTS = [("dylive", "107.173.30.245", False), ("dy", "108.174.50.68", True)] FILES = [ "backend/app/singbox_manager.py", "scripts/linux/douyinweb-singbox.service", ] REMOTE = r''' set -e REPO=/opt/douyinWEB # stop crash-loop first systemctl stop douyinweb-singbox 2>/dev/null || true systemctl reset-failed douyinweb-singbox 2>/dev/null || true tar -xzf /tmp/sb_fix.tgz -C "$REPO" install -d -m 755 /opt/douyinWEB/bin /opt/douyinWEB/config # Download dedicated binary into APP dir (NOT v2ray-agent) VER=1.11.15 ASSET="sing-box-${VER}-linux-amd64.tar.gz" URL="https://github.com/SagerNet/sing-box/releases/download/v${VER}/${ASSET}" cd /tmp rm -rf _sb_dl && mkdir _sb_dl && cd _sb_dl if ! curl -fsSL -o "$ASSET" "$URL"; then wget -q -O "$ASSET" "$URL" fi tar -xzf "$ASSET" BIN=$(find . -type f -name sing-box | head -1) test -n "$BIN" install -m 755 "$BIN" /opt/douyinWEB/bin/sing-box # prove isolation realpath /opt/douyinWEB/bin/sing-box test "$(realpath /opt/douyinWEB/bin/sing-box)" != "$(realpath /etc/v2ray-agent/sing-box/sing-box 2>/dev/null || echo x)" /opt/douyinWEB/bin/sing-box version | head -3 install -m 644 "$REPO/scripts/linux/douyinweb-singbox.service" /etc/systemd/system/douyinweb-singbox.service systemctl daemon-reload cd "$REPO/backend" .venv/bin/python -m py_compile app/singbox_manager.py systemctl restart douyinweb-api sleep 3 .venv/bin/python - <<'PY' from app import db, egress_hub, singbox_manager m = db.get_settings_map() print("host_sing-box before=", singbox_manager.host_v2ray_agent_status().get("active")) # apply residential if profile exists if m.get("global_socks_enabled") or str(m.get("egress_mode") or "") == "residential": r = egress_hub.apply_mode("residential", chain=False, refresh_neko=False, cfg=m) print("apply=", r.get("ok"), r.get("residential", {}).get("ok"), r.get("residential", {}).get("error") or r.get("residential", {}).get("via")) else: # just ensure binary + unit; leave mode off print("bin=", singbox_manager.ensure_binary()) print("skip apply (residential not enabled in settings)") print("app_running=", singbox_manager.is_running()) print("app_bin=", singbox_manager._find_binary()) print("host_sing-box after=", singbox_manager.host_v2ray_agent_status().get("active")) st = egress_hub.status() print("mode=", st.get("mode"), "matrix_uploads=", st.get("matrix")) print("FIX_OK") PY systemctl is-active sing-box systemctl is-active douyinweb-singbox || true ss -lntp | grep 17891 || true curl -sS -m 5 -o /dev/null -w "api=%{http_code}\n" http://127.0.0.1:8014/api/health # quick probe via mixed if running if ss -lntp | grep -q 17891; then curl -sS -m 12 -x http://127.0.0.1:17891 https://api.ipify.org && echo fi echo DONE ''' def main() -> int: buf = io.BytesIO() with tarfile.open(fileobj=buf, mode="w:gz") as tar: for rel in FILES: tar.add(ROOT / rel, arcname=rel) blob = buf.getvalue() c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(JUMP, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False) with c.open_sftp() as sf: with sf.file("/tmp/sb_fix.tgz", "wb") as f: f.write(blob) with sf.file("/tmp/_fix_sb_iso.sh", "w") as f: f.write(REMOTE.replace("\r\n", "\n")) for label, host, via in HOSTS: print(f"\n===== {label} =====", flush=True) if via: c.exec_command( f"SSHPASS={PASS} sshpass -e scp -o StrictHostKeyChecking=no " f"/tmp/sb_fix.tgz /tmp/_fix_sb_iso.sh root@{host}:/tmp/", timeout=60, )[1].channel.recv_exit_status() _, o, e = c.exec_command( f"SSHPASS={PASS} sshpass -e ssh -o StrictHostKeyChecking=no root@{host} " f"bash /tmp/_fix_sb_iso.sh", timeout=300, ) else: _, o, e = c.exec_command("bash /tmp/_fix_sb_iso.sh", timeout=300) out = (o.read() + e.read()).decode("utf-8", "replace") print(out[-10000:] if len(out) > 10000 else out, flush=True) if "FIX_OK" not in out: raise SystemExit(f"{label} failed") c.close() print("\nBOTH_FIXED_ISOLATED", flush=True) return 0 if __name__ == "__main__": raise SystemExit(main())