douyinWEB/scripts/_deploy_residential_test3.py
eric f7dc2b6b4e Add VPS static residential IP via isolated douyinweb-singbox.
US VPS can use egress_mode=residential (127.0.0.1:17891) without touching host sing-box.service / v2ray-agent; default remains datacenter direct.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-21 21:03:34 -05:00

138 lines
4.9 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy test3 residential egress to both VPS — preserve DB, default residential OFF.
Safety:
- Never touch sing-box.service / /etc/v2ray-agent
- Never wipe monitor.db / pb_data / neko profiles
- Install douyinweb-singbox.service but do NOT enable residential
- Restart only douyinweb-api / douyinweb-web
"""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
PASS = "Xiao4669805"
REMOTE = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git"
BRANCH = "test3"
JUMP = "107.173.30.245"
HOSTS = [
("dylive", "107.173.30.245", False),
("dy", "108.174.50.68", True),
]
UNIT_SRC = (ROOT / "scripts/linux/douyinweb-singbox.service").read_text(encoding="utf-8")
REMOTE_SH = r'''
set -e
REPO=/opt/douyinWEB
BRANCH=test3
ts=$(date +%Y%m%d_%H%M%S)
mkdir -p "$REPO/backend/data"
cp -a "$REPO/backend/data/monitor.db" "$REPO/backend/data/monitor.db.bak.$ts" 2>/dev/null || true
cd "$REPO"
git remote remove origin 2>/dev/null || true
git remote add origin "__REMOTE__"
git fetch origin "$BRANCH"
git clean -fd -e backend/data -e frontend/.next -e node_modules -e frontend/node_modules \
-e backend/.venv -e .venv -e "*.db" -e "*.db-*" -e pb_data -e backend/pb_data \
-e config -e backend/data/neko -e backend/data/ops_neko -e backend/data/clips \
-e backend/data/covers 2>/dev/null || true
git checkout -f -B "$BRANCH" "origin/$BRANCH"
git reset --hard "origin/$BRANCH"
git log -1 --oneline
install -d -m 755 /opt/douyinWEB/config
install -m 644 /tmp/douyinweb-singbox.service /etc/systemd/system/douyinweb-singbox.service
if [ ! -x /usr/bin/sing-box ] && command -v sing-box >/dev/null; then
BIN=$(command -v sing-box)
sed -i "s|/usr/bin/sing-box|$BIN|" /etc/systemd/system/douyinweb-singbox.service
fi
systemctl daemon-reload
systemctl stop douyinweb-singbox 2>/dev/null || true
systemctl disable douyinweb-singbox 2>/dev/null || true
cd "$REPO/backend"
.venv/bin/python -m py_compile app/egress_hub.py app/singbox_manager.py app/tiktok_socks.py app/youtube_live.py
echo py_compile_ok
cd "$REPO/frontend"
npm ci --prefer-offline 2>/dev/null || npm install
npm run build
pkill -9 -x usr 2>/dev/null || true
pkill -9 -x pm2 2>/dev/null || true
bash "$REPO/scripts/linux/build_orphan_watch.sh" 2>/dev/null || true
systemctl restart douyinweb-api
systemctl restart douyinweb-web
sleep 5
echo "=== VERIFY ==="
systemctl is-active douyinweb-api douyinweb-web
echo -n "host_sing-box="; systemctl is-active sing-box 2>/dev/null || true
echo -n "app_douyinweb-singbox="; systemctl is-active douyinweb-singbox 2>/dev/null || echo inactive
curl -sS -m 8 -o /dev/null -w "health=%{http_code} t=%{time_total}\n" http://127.0.0.1:8014/api/health
cd "$REPO/backend"
.venv/bin/python - <<'PY'
from app import egress_hub, singbox_manager
st = egress_hub.status()
print("egress_mode=", st.get("mode"))
print("v2ray_agent=", st.get("v2ray_agent"))
print("app_sb=", st.get("douyinweb_singbox"))
va = singbox_manager.host_v2ray_agent_status()
print("host_singbox_active=", va.get("active"))
# Host sing-box should remain active (v2ray-agent). Soft warn only if missing.
print("DEPLOY_OK")
PY
echo DONE
'''
def main() -> int:
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)
script = REMOTE_SH.replace("__REMOTE__", REMOTE).replace("\r\n", "\n")
with c.open_sftp() as sf:
with sf.file("/tmp/douyinweb-singbox.service", "w") as f:
f.write(UNIT_SRC.replace("\r\n", "\n"))
with sf.file("/tmp/_deploy_res_test3.sh", "w") as f:
f.write(script)
for label, host, via_jump in HOSTS:
print(f"\n===== {label} ({host}) =====", flush=True)
if via_jump:
code = c.exec_command(
f"SSHPASS={PASS} sshpass -e scp -o StrictHostKeyChecking=no "
f"/tmp/douyinweb-singbox.service /tmp/_deploy_res_test3.sh root@{host}:/tmp/",
timeout=90,
)[1].channel.recv_exit_status()
if code != 0:
raise SystemExit(f"scp to {label} failed: {code}")
_, o, e = c.exec_command(
f"SSHPASS={PASS} sshpass -e ssh -o StrictHostKeyChecking=no root@{host} "
f"bash /tmp/_deploy_res_test3.sh",
timeout=1200,
)
else:
_, o, e = c.exec_command("bash /tmp/_deploy_res_test3.sh", timeout=1200)
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-14000:] if len(out) > 14000 else out, flush=True)
if "DEPLOY_OK" not in out:
raise SystemExit(f"{label} deploy failed")
c.close()
print("\nBOTH_OK — residential OFF by default; fill SOCKS in 网络页 then enable", flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())