# -*- coding: utf-8 -*- """Wire LIVE online/broadcast into VIP ops SPA on admin.hackrobot.cn. - Accept VIP ops HMAC token on /api/admin/live/* - Sync VIP_OPS_SECRET into nomadmini .env - Inject admin-live.js dock into VIP 运营台 """ from __future__ import annotations import sys from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True) ROOT = Path(__file__).resolve().parents[1] LIVE_OPS = ROOT / "apps" / "api" / "services" / "ebook_svc" / "routers" / "live_ops.py" ADMIN_LIVE_JS = ROOT / "apps" / "admin" / "deploy" / "_vip_live_assets" / "admin-live.js" HOST, USER, PASSWORD = "129.204.21.129", "ubuntu", "Xiao4669805" REMOTE_SH = r'''#!/bin/bash set -euo pipefail APP=/opt/nomadmini/backend/app ENVF=/opt/nomadmini/backend/.env echo '=== sync VIP_OPS_SECRET ===' SECRET=$(sudo python3 - <<'PY' from pathlib import Path p=Path('/www/wwwroot/nomadvip/.env.local') for line in p.read_text(encoding='utf-8').splitlines(): if line.startswith('VIP_OPS_SECRET='): print(line.split('=',1)[1].strip().strip('"').strip("'")) break PY ) if [ -z "$SECRET" ]; then echo 'missing VIP_OPS_SECRET'; exit 1; fi printf '%s' "$SECRET" | sudo tee /tmp/vip_ops_secret.txt >/dev/null sudo python3 - <<'PY' from pathlib import Path secret = Path('/tmp/vip_ops_secret.txt').read_text(encoding='utf-8').strip() p = Path('/opt/nomadmini/backend/.env') t = p.read_text(encoding='utf-8') lines = [ln for ln in t.splitlines() if not ln.startswith('VIP_OPS_SECRET=')] lines.append(f'VIP_OPS_SECRET={secret}') p.write_text('\n'.join(lines) + '\n', encoding='utf-8') print('VIP_OPS_SECRET synced len', len(secret)) PY sudo rm -f /tmp/vip_ops_secret.txt echo '=== install live_ops ===' sudo cp /tmp/live_ops.py "$APP/routers/live_ops.py" sudo chown ubuntu:ubuntu "$APP/routers/live_ops.py" echo '=== install admin-live.js ===' R=/www/wwwroot/admin.hackrobot.cn sudo cp /tmp/admin-live.js "$R/admin-live.js" sudo chmod 644 "$R/admin-live.js" sudo python3 - <<'PY' from pathlib import Path p=Path('/www/wwwroot/admin.hackrobot.cn/index.html') t=p.read_text(encoding='utf-8') needle='admin-live.js' if needle in t: print('index already has admin-live.js') else: if '' not in t: raise SystemExit('no ') t=t.replace('', '', 1) p.write_text(t, encoding='utf-8') print('injected admin-live.js') print(p.read_text(encoding='utf-8')[-400:]) PY echo '=== restart api ===' sudo systemctl restart nomadmini-api sleep 3 systemctl is-active nomadmini-api echo '=== smoke ops token online/broadcast ===' TOK=$(curl -sk -X POST https://admin.hackrobot.cn/api/ops/login \ -H 'content-type: application/json' \ -d '{"username":"hackrobot","password":"Xiao4669805@"}' \ | python3 -c 'import sys,json;print(json.load(sys.stdin)["token"])') echo "tok_len=${#TOK}" curl -sk -o /tmp/online.json -w 'online:%{http_code}\n' \ -H "Authorization: Bearer $TOK" \ https://admin.hackrobot.cn/api/admin/live/online head -c 600 /tmp/online.json; echo curl -sk -o /tmp/presence.json -w 'presence:%{http_code}\n' -X POST \ https://vip.hackrobot.cn/api/vip/live/presence \ -H 'content-type: application/json' \ -d '{"visitorId":"wire_smoke","nick":"wire","email":"wire@test.local","page":"/","role":"guest"}' head -c 200 /tmp/presence.json; echo curl -sk -o /tmp/online2.json -w 'online2:%{http_code}\n' \ -H "Authorization: Bearer $TOK" \ https://admin.hackrobot.cn/api/admin/live/online python3 - <<'PY' import json from pathlib import Path d=json.loads(Path('/tmp/online2.json').read_text(encoding='utf-8')) web=d.get('webOnline') or [] print('webOnline', len(web), [x.get('nick') or x.get('id') for x in web[:5]]) PY curl -sk -o /tmp/bc.json -w 'broadcast:%{http_code}\n' -X POST \ -H "Authorization: Bearer $TOK" \ -H 'content-type: application/json' \ -d '{"title":"运营台联通测试","body":"实时通知已接通","cta":"知道了","href":"https://vip.hackrobot.cn/","tone":"amber","ttlSec":30}' \ https://admin.hackrobot.cn/api/admin/live/broadcast head -c 400 /tmp/bc.json; echo curl -sk -o /dev/null -w 'admin-live.js:%{http_code}\n' https://admin.hackrobot.cn/admin-live.js curl -sk https://admin.hackrobot.cn/ | tr '\n' ' ' | grep -o 'admin-live.js' | head -1 || echo 'WARN: html missing admin-live.js' echo DONE ''' def main() -> int: if not LIVE_OPS.is_file() or not ADMIN_LIVE_JS.is_file(): print("missing source files") return 1 text = LIVE_OPS.read_text(encoding="utf-8").replace("ebook_svc.", "app.") if "ebook_svc." in text: print("rewrite failed") return 1 if "require_admin_or_ops" not in text: print("dual auth missing") return 1 c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASSWORD, timeout=40, allow_agent=False, look_for_keys=False) try: sftp = c.open_sftp() with sftp.file("/tmp/live_ops.py", "wb") as f: f.write(text.encode("utf-8")) with sftp.file("/tmp/admin-live.js", "wb") as f: f.write(ADMIN_LIVE_JS.read_bytes()) with sftp.file("/tmp/wire_vip_ops_live_129.sh", "wb") as f: f.write(REMOTE_SH.replace("\r\n", "\n").encode("utf-8")) sftp.close() _, o, e = c.exec_command("bash /tmp/wire_vip_ops_live_129.sh", timeout=120) print(o.read().decode("utf-8", "replace"), flush=True) err = e.read().decode("utf-8", "replace") if err.strip(): print(err[-2000:], file=sys.stderr, flush=True) return o.channel.recv_exit_status() finally: c.close() if __name__ == "__main__": raise SystemExit(main())