# -*- coding: utf-8 -*- """Proxy /api/social/ on api.nomadro.cn → nomadro-social:8020 (weapp whitelist).""" from __future__ import annotations import time import paramiko HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805" def main() -> int: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect( HOST, username=USER, password=PASS, timeout=60, allow_agent=False, look_for_keys=False, ) cmd = r""" set -e EXT_DIR=/www/server/panel/vhost/nginx/extension/api.nomadro.cn sudo mkdir -p "$EXT_DIR" sudo tee "$EXT_DIR/social-api.conf" >/dev/null <<'EOF' # weapp 合法域名多为 api.nomadro.cn — 社交 live/join 必须可直连 location ^~ /api/social/ { proxy_pass http://127.0.0.1:8020; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Social-Guest-Id $http_x_social_guest_id; } EOF # ensure api conf includes extension/*.conf F=/www/server/panel/vhost/nginx/api.nomadro.cn.conf sudo grep -n 'extension/api.nomadro.cn\|include' "$F" | head -20 if ! sudo grep -q 'extension/api.nomadro.cn/\*\.conf' "$F"; then # inject include after server_name line in https server sudo python3 - <<'PY' from pathlib import Path p = Path("/www/server/panel/vhost/nginx/api.nomadro.cn.conf") text = p.read_text(encoding="utf-8") inc = " include /www/server/panel/vhost/nginx/extension/api.nomadro.cn/*.conf;\n" if "extension/api.nomadro.cn" in text: print("include already") else: # after first ssl server_name api.nomadro.cn; needle = "server_name api.nomadro.cn;" # prefer the ssl block (second occurrence often); insert after both parts = text.split(needle) if len(parts) < 2: raise SystemExit("no server_name") out = parts[0] for i, part in enumerate(parts[1:], 1): out += needle if "extension/api.nomadro.cn" not in part[:200]: out += "\n" + inc out += part p.write_text(out, encoding="utf-8") print("include injected") PY fi sudo nginx -t sudo systemctl reload nginx echo '=== api social health ===' curl -sk --max-time 5 'https://api.nomadro.cn/api/social/v1/health' | head -c 300; echo echo '=== api live join ===' curl -sk --max-time 8 -X POST 'https://api.nomadro.cn/api/social/v1/live/rooms/office/join' \ -H 'Content-Type: application/json' \ -H 'X-Social-Guest-Id: weapp_real_1' \ --data-binary '{"nick":"真机"}' | head -c 400; echo """ _, stdout, _ = c.exec_command(cmd, timeout=90, get_pty=True) while not stdout.channel.exit_status_ready(): if stdout.channel.recv_ready(): print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True) time.sleep(0.05) while stdout.channel.recv_ready(): print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True) code = stdout.channel.recv_exit_status() print(f"[exit {code}]", flush=True) c.close() return code if __name__ == "__main__": raise SystemExit(main())