# -*- coding: utf-8 -*- """Proxy Centrifugo /rt/ on api.nomadro.cn for weapp socket合法域名.""" 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, ) # also push latest social api router from pathlib import Path repo = Path(__file__).resolve().parents[3] sftp = c.open_sftp() for rel, remote in ( ("apps/api/services/social_svc/routers/api.py", "/tmp/social_routers_api.py"), ): data = (repo / rel).read_bytes() with sftp.file(remote, "wb") as rf: rf.write(data) sftp.close() cmd = r""" set -e EXT=/www/server/panel/vhost/nginx/extension/api.nomadro.cn sudo mkdir -p "$EXT" # copy www centrifugo snippet if exists, else write if sudo test -f /www/server/panel/vhost/nginx/extension/www.nomadro.cn/centrifugo-rt.conf; then sudo cp /www/server/panel/vhost/nginx/extension/www.nomadro.cn/centrifugo-rt.conf "$EXT/centrifugo-rt.conf" else sudo tee "$EXT/centrifugo-rt.conf" >/dev/null <<'EOF' location ^~ /rt/ { proxy_pass http://127.0.0.1:8008/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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_read_timeout 3600s; proxy_send_timeout 3600s; } EOF fi # ensure social-api still there sudo test -f "$EXT/social-api.conf" || true sudo nginx -t sudo systemctl reload nginx sudo cp /tmp/social_routers_api.py /var/www/nomadro.cn/apps/api/services/social_svc/routers/api.py sudo systemctl restart nomadro-social sleep 2 echo '=== token ===' curl -sk --max-time 5 'https://api.nomadro.cn/api/social/v1/realtime/token?channel_id=general&guest=chat1' | head -c 280; echo echo '=== list ===' curl -sk --max-time 5 'https://api.nomadro.cn/api/social/v1/channels/general/messages?guest=chat1' | head -c 200; echo echo '=== post ===' curl -sk --max-time 8 -X POST 'https://api.nomadro.cn/api/social/v1/channels/general/messages?guest=chat1' \ -H 'Content-Type: application/json' --data-binary '{"author":"系统","text":"Centrifugo 聊天通路已接通"}' | head -c 240; echo echo '=== list2 ===' curl -sk --max-time 5 'https://api.nomadro.cn/api/social/v1/channels/general/messages?guest=chat1' | head -c 400; echo systemctl is-active centrifugo nomadro-social """ _, 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())