nomadro/scripts/fix_admin_hk_api_to_vip4007.py
Eric 29b8bdc55e Document VIP ops vs Nomadro admin hosts and ship LIVE/tag assets.
Hard-separate admin.hackrobot.cn (VIP ops) from admin.nomadro.cn forever; add live_ops bus, overlay tags, and deploy helpers.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-15 02:38:03 +08:00

167 lines
5.6 KiB
Python

# -*- coding: utf-8 -*-
"""Point admin.hackrobot.cn /api → nomadvip :4007 (/api/ops). Keep /api/admin/live → :8010."""
from __future__ import annotations
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
REMOTE = r"""#!/bin/bash
set -euo pipefail
echo '=== local ops on 4007 ==='
curl -sS -o /tmp/ops4007.json -w 'ops4007:%{http_code}\n' --max-time 8 \
-H 'Content-Type: application/json' \
-d '{"username":"hackrobot","password":"Xiao4669805@"}' \
http://127.0.0.1:4007/api/ops/login || true
head -c 220 /tmp/ops4007.json; echo
CONF=/www/server/panel/vhost/nginx/admin.hackrobot.cn.conf
sudo cp -a "$CONF" "/tmp/admin.conf.before-4007-$(date -u +%Y%m%dT%H%M%SZ)"
sudo python3 - <<'PY'
from pathlib import Path
p = Path("/www/server/panel/vhost/nginx/admin.hackrobot.cn.conf")
raw = p.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
t = raw.decode("utf-8")
# Ensure extension include for live SSE (8010) remains before catch-all /api/
inc = "include /www/server/panel/vhost/nginx/extension/admin.hackrobot.cn/*.conf;"
if inc not in t:
# insert before location ^~ /api/
needle = " location ^~ /api/ {"
if needle not in t:
raise SystemExit("no /api/ location")
t = t.replace(
needle,
" # Live ops SSE (8010) — more specific than /api/\n " + inc + "\n\n" + needle,
1,
)
print("inserted extension include")
# Force catch-all /api/ → VIP Next 4007 (hosts /api/ops for VIP 运营台)
import re
pat = re.compile(
r"(location \^~ /api/ \{.*?proxy_pass http://127\.0\.0\.1:)(\d+)(;)",
re.S,
)
m = pat.search(t)
if not m:
raise SystemExit("cannot find /api/ proxy_pass")
old_port = m.group(2)
t = pat.sub(r"\g<1>4007\g<3>", t, count=1)
# Host header for Next should be localhost-ish as before
t = t.replace(
"proxy_set_header Host $host;\n proxy_set_header Authorization $http_authorization;",
"proxy_set_header Host 127.0.0.1;",
1,
)
# comment
t = t.replace("# Ops API -> nomadmini (8010)", "# Ops API -> VIP next (4007) /api/ops", 1)
t = t.replace("# Ops API -> VIP next (4007)", "# Ops API -> VIP next (4007) /api/ops", 1)
p.write_bytes(t.encode("utf-8"))
print(f"api proxy_pass {old_port} -> 4007")
PY
# live SSE conf must exist and stay on 8010
sudo mkdir -p /www/server/panel/vhost/nginx/extension/admin.hackrobot.cn
sudo tee /www/server/panel/vhost/nginx/extension/admin.hackrobot.cn/admin-live-sse.conf >/dev/null <<'NGX'
location ^~ /api/admin/live/ {
proxy_pass http://127.0.0.1:8010;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Authorization $http_authorization;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Connection '';
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 3600s;
chunked_transfer_encoding off;
}
NGX
sudo sed -i 's/\r$//' /www/server/panel/vhost/nginx/extension/admin.hackrobot.cn/admin-live-sse.conf
# SPA must be VIP ops (not wechat shell)
R=/www/wwwroot/admin.hackrobot.cn
if ! grep -q 'index-OYkETFeq.js\|/api/ops/' "$R"/assets/index-*.js 2>/dev/null; then
echo 'SPA not VIP ops — restoring from /tmp backup'
if [ -d /tmp/admin-restore-20261111T061920Z ]; then
sudo rsync -a --delete /tmp/admin-restore-20261111T061920Z/ "$R"/
elif [ -f /var/backups/nomadro-upgrade/20261111T060203Z/admin-hackrobot.tgz ]; then
sudo rm -rf /tmp/admin-vip-unpack && mkdir -p /tmp/admin-vip-unpack
sudo tar -xzf /var/backups/nomadro-upgrade/20261111T060203Z/admin-hackrobot.tgz -C /tmp/admin-vip-unpack
# tarball may be flat or nested
if [ -f /tmp/admin-vip-unpack/index.html ]; then
sudo rsync -a --delete /tmp/admin-vip-unpack/ "$R"/
else
INNER=$(find /tmp/admin-vip-unpack -name index.html | head -1 | xargs dirname)
sudo rsync -a --delete "$INNER"/ "$R"/
fi
else
echo 'WARN: no VIP SPA backup'
fi
fi
sudo nginx -t
sudo nginx -s reload
sleep 1
echo '=== smoke ==='
curl -sk -o /tmp/ops_pub.json -w 'ops_login:%{http_code}\n' --max-time 12 \
-H 'Content-Type: application/json' \
-d '{"username":"hackrobot","password":"Xiao4669805@"}' \
https://admin.hackrobot.cn/api/ops/login
head -c 220 /tmp/ops_pub.json; echo
curl -sk -o /dev/null -w 'admin:%{http_code}\n' --max-time 12 https://admin.hackrobot.cn/
curl -sk -o /dev/null -w 'vip:%{http_code}\n' --max-time 12 https://vip.hackrobot.cn/
# confirm no wechat shell
JS=$(ls "$R"/assets/index-*.js | head -1)
echo "JS=$JS"
python3 - <<PY
from pathlib import Path
t=Path("$JS").read_text(encoding="utf-8", errors="replace")
print("api/ops", t.count("/api/ops"))
print("wechat", t.count("wechat"))
print("微信后台", t.count("微信后台"))
print("vip.hackrobot", t.count("vip.hackrobot"))
PY
grep -n 'proxy_pass' /www/server/panel/vhost/nginx/admin.hackrobot.cn.conf | head -10
echo DONE
"""
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
"129.204.21.129",
22,
"ubuntu",
"Xiao4669805",
timeout=40,
allow_agent=False,
look_for_keys=False,
)
try:
sftp = c.open_sftp()
with sftp.file("/tmp/fix_admin_hk_api_to_vip4007.sh", "wb") as f:
f.write(REMOTE.replace("\r\n", "\n").encode("utf-8"))
sftp.close()
_, o, e = c.exec_command("bash /tmp/fix_admin_hk_api_to_vip4007.sh", timeout=90)
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())