douyinWEB/scripts/_fix_dylive_local_neko_ports.py
2026-09-13 22:10:49 -05:00

222 lines
8.2 KiB
Python

#!/usr/bin/env python3
"""Free VPS :9200-9207 for dylive local Neko; remap physical FRP-aux to :19200-19207.
Also deploy host_profile.py (VPS profile no longer wipes assign Neko).
Authorized: operator asked for dylive assign local neko start/stop like physical.
"""
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]
HOST = "107.173.30.245"
ROOT_PW = "Xiao4669805"
def ssh():
last = None
for i in range(8):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
HOST,
22,
"root",
ROOT_PW,
timeout=45,
banner_timeout=120,
allow_agent=False,
look_for_keys=False,
)
return c
except Exception as ex:
last = ex
print("retry", i, ex, flush=True)
time.sleep(3)
raise SystemExit(last)
def run(c, cmd, t=180):
print(f"\n$ {cmd[:200]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
print(out[-12000:] if len(out) > 12000 else out, flush=True)
return code, out
def sftp_put(c, local: Path, remote: str):
sftp = c.open_sftp()
sftp.put(str(local), remote)
sftp.close()
print(f"put {local.name} -> {remote}", flush=True)
REMOTE = r"""
set -euo pipefail
TS=$(date +%Y%m%d_%H%M%S)
# --- 1) FRP-aux: remap physical neko HTTP 9200-9207 -> VPS local 19200-19207 ---
UNIT=/etc/systemd/system/dy-frp-http-aux.service
cp -a "$UNIT" "$UNIT.bak.neko_ports_$TS"
cat >"$UNIT" <<'EOF'
[Unit]
Description=dy.nomadro.com FRP-aux tunnels (HTTP+ShellCrash+Neko remapped+S3+ops-neko via :4014)
After=network-online.target frpp.service
Wants=network-online.target
[Service]
Type=simple
Restart=always
RestartSec=3
Environment=SSHPASS=4669805
# Physical assign neko :920x mapped to VPS :1920x so dylive can own :9200-9207
ExecStart=/usr/bin/sshpass -e ssh -N -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -L 127.0.0.1:18014:127.0.0.1:8014 -L 127.0.0.1:14015:127.0.0.1:4015 -L 127.0.0.1:9999:127.0.0.1:9999 -L 127.0.0.1:19200:127.0.0.1:9200 -L 127.0.0.1:19201:127.0.0.1:9201 -L 127.0.0.1:19202:127.0.0.1:9202 -L 127.0.0.1:19203:127.0.0.1:9203 -L 127.0.0.1:19204:127.0.0.1:9204 -L 127.0.0.1:19205:127.0.0.1:9205 -L 127.0.0.1:19206:127.0.0.1:9206 -L 127.0.0.1:19207:127.0.0.1:9207 -L 127.0.0.1:18200:127.0.0.1:18200 -L 127.0.0.1:18201:127.0.0.1:18201 -L 127.0.0.1:18202:127.0.0.1:18202 -L 127.0.0.1:18203:127.0.0.1:18203 -L 127.0.0.1:18204:127.0.0.1:18204 -L 127.0.0.1:18205:127.0.0.1:18205 -L 127.0.0.1:18206:127.0.0.1:18206 -L 127.0.0.1:18207:127.0.0.1:18207 -L 127.0.0.1:28333:127.0.0.1:8333 -p 4014 eric@127.0.0.1
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl restart dy-frp-http-aux.service
sleep 2
systemctl is-active dy-frp-http-aux.service
# --- 2) Caddy dy.nomadro.com: /neko/1-8 -> 1920x (not dylive 920x) ---
SITE=/etc/caddy/sites/dy.nomadro.com.caddy
cp -a "$SITE" "$SITE.bak.neko_ports_$TS"
python3 - <<'PY'
from pathlib import Path
p = Path("/etc/caddy/sites/dy.nomadro.com.caddy")
text = p.read_text(encoding="utf-8")
# high ports first to avoid partial replace
for i in range(7, -1, -1):
old = f"reverse_proxy 127.0.0.1:{9200+i}"
new = f"reverse_proxy 127.0.0.1:{19200+i}"
text = text.replace(old, new)
p.write_text(text, encoding="utf-8")
print("rewritten neko1-8 proxies to 1920x")
for i, line in enumerate(p.read_text(encoding="utf-8").splitlines(), 1):
if "reverse_proxy" in line and ("1920" in line or ":920" in line) and i < 120:
print(f"{i}:{line}")
PY
caddy validate --config /etc/caddy/Caddyfile
systemctl reload caddy
systemctl is-active caddy
# --- 3) Ensure VPS NAT1TO1 for local neko WebRTC ---
# Prefer EnvironmentFile or unit Environment=
APIU=/etc/systemd/system/douyinweb-api.service
cp -a "$APIU" "$APIU.bak.neko_nat_$TS" 2>/dev/null || true
if ! grep -q 'DY_NEKO_NAT1TO1=' "$APIU" 2>/dev/null; then
if grep -q 'Environment=DY_PUBLIC_BASE_URL=' "$APIU"; then
sed -i '/Environment=DY_PUBLIC_BASE_URL=/a Environment=DY_NEKO_NAT1TO1=107.173.30.245' "$APIU"
else
sed -i '/\[Service\]/a Environment=DY_NEKO_NAT1TO1=107.173.30.245' "$APIU"
fi
echo "added DY_NEKO_NAT1TO1 to api unit"
else
echo "DY_NEKO_NAT1TO1 already in api unit"
fi
# clear cached wrong ICE if any
rm -f /opt/douyinWEB/backend/data/neko/nat1to1.txt 2>/dev/null || true
# --- 4) Deploy host_profile.py ---
install -m 0644 /tmp/host_profile.py /opt/douyinWEB/backend/app/host_profile.py
# youtube_neko compose fallbacks (optional, already uploaded if present)
if [[ -f /tmp/youtube_neko.py ]]; then
install -m 0644 /tmp/youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py
fi
systemctl daemon-reload
systemctl restart douyinweb-api
sleep 3
systemctl is-active douyinweb-api
echo '--- ports ---'
ss -lntp | grep -E ':(920[0-7]|1920[0-7]|18014|14015)\b' | head -30 || true
echo '--- health ---'
curl -sS -m 8 http://127.0.0.1:8014/api/health | head -c 220; echo
curl -sS -m 8 -o /dev/null -w 'dy_api=%{http_code}\n' https://dy.nomadro.com/api/health
curl -sS -m 8 -o /dev/null -w 'dylive_api=%{http_code}\n' https://dylive.nomadro.com/api/health
"""
def main():
c = ssh()
try:
sftp_put(c, ROOT / "backend/app/host_profile.py", "/tmp/host_profile.py")
sftp_put(c, ROOT / "backend/app/youtube_neko.py", "/tmp/youtube_neko.py")
code, _ = run(c, REMOTE, t=240)
if code != 0:
raise SystemExit(f"remote failed {code}")
# Start local neko for group with neko_enabled (instance 3) to verify ports free
run(
c,
r"""
set +e
CS=$(mktemp)
curl -sS -m 15 -c "$CS" -b "$CS" -X POST http://127.0.0.1:8014/api/auth/login \
-H 'Content-Type: application/json' \
-d '{"username":"hackrobot","password":"Xiao4669805@"}' >/dev/null
# find group with neko_instance_id=3
GID=$(curl -sS -m 15 -b "$CS" http://127.0.0.1:8014/api/youtube/assign/groups | python3 -c '
import sys,json
rows=json.load(sys.stdin)
rows=rows if isinstance(rows,list) else rows.get("groups") or []
for g in rows:
if int(g.get("neko_instance_id") or 0)==3:
print(g.get("id") or ""); break
')
echo "gid3=$GID"
if [[ -n "$GID" ]]; then
curl -sS -m 120 -b "$CS" -X POST "http://127.0.0.1:8014/api/youtube/assign/groups/${GID}/neko" \
-H 'Content-Type: application/json' \
-d '{"action":"start","enabled":true}' | python3 -c 'import sys,json; d=json.load(sys.stdin); print({k:d.get(k) for k in ("ok","error")}); n=d.get("neko") or {}; print("neko", {k:n.get(k) for k in ("id","running","ready","status","studio_url")})'
fi
sleep 2
docker ps --format '{{.Names}} {{.Status}} {{.Ports}}' | grep douyinweb-neko | head
ss -lntp | grep ':9202' | head
curl -sS -m 8 -o /dev/null -w 'local9202=%{http_code}\n' http://127.0.0.1:9202/
curl -sS -m 8 -o /dev/null -w 'dylive_neko3=%{http_code}\n' https://dylive.nomadro.com/neko/3/
curl -sS -m 8 -o /dev/null -w 'dy_neko1=%{http_code}\n' https://dy.nomadro.com/neko/1/
curl -sS -m 8 -o /dev/null -w 'dy_cn_neko1=%{http_code}\n' https://dy.nomadro.cn/neko/1/
# inspect env on started container
docker inspect douyinweb-neko-3 --format '{{json .Config.Env}}' 2>/dev/null | tr ',' '\n' | grep -E 'NAT1TO1|BITRATE|MAX_FPS|DESKTOP_SCREEN|BROWSER_PROXY' || true
rm -f "$CS"
""",
t=180,
)
finally:
c.close()
print("\n===== public verify =====", flush=True)
with httpx.Client(follow_redirects=False, timeout=25) as cl:
for u in [
"https://dylive.nomadro.com/api/health",
"https://dylive.nomadro.com/neko/3/",
"https://dy.nomadro.com/api/health",
"https://dy.nomadro.com/neko/1/",
"https://dy.nomadro.cn/ops-neko/1/",
]:
try:
r = cl.get(u)
print(f"{r.status_code} {u}", flush=True)
except Exception as ex:
print(f"ERR {u} {ex}", flush=True)
print("DONE", flush=True)
if __name__ == "__main__":
main()