#!/usr/bin/env python3 """Write clean nginx conf (one /douyin-covers); probe avatars correctly; deploy FE api.js.""" from __future__ import annotations import ssl import sys import time import urllib.error import urllib.request import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") CTX = ssl.create_default_context() DYLIVE, DY, PASS = "107.173.30.245", "108.174.50.68", "Xiao4669805" def connect(host): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for i in range(10): try: c.connect(host, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False, banner_timeout=120) return c except Exception as e: print("retry", host, e, flush=True) time.sleep(2 + i) raise SystemExit("ssh fail") def run(c, script, timeout=300): _i, o, e = c.exec_command(script, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace") def put(c, data: bytes, remote: str): sftp = c.open_sftp() with sftp.file(remote, "wb") as f: f.write(data) sftp.close() NGINX = r''' server { server_name dy.nomadro.live; client_max_body_size 200m; location ^~ /douyin-covers/ { proxy_http_version 1.1; proxy_pass http://127.0.0.1:8333; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; proxy_buffering off; } location ^~ /api/ { proxy_pass http://127.0.0.1:8014; 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 Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 3600s; } # files only — do not proxy the /covers page (Next) location ^~ /covers/ { proxy_pass http://127.0.0.1:8014; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; } location ^~ /_/ { proxy_pass http://127.0.0.1:8092; proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme; } include /etc/nginx/conf.d/douyinweb-neko-locations.inc; location / { proxy_pass http://127.0.0.1:4015; 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 Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 60s; } listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/dy.nomadro.live/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/dy.nomadro.live/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = dy.nomadro.live) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name dy.nomadro.live; return 404; # managed by Certbot } ''' print("=== write clean nginx ===", flush=True) c = connect(DY) put(c, NGINX.lstrip("\n").encode(), "/etc/nginx/conf.d/dy-nomadro-live.conf") print(run(c, r""" set -e cp -a /etc/nginx/conf.d/dy-nomadro-live.conf /etc/nginx/conf.d/dy-nomadro-live.conf.bak.cleanwrite.$(date +%Y%m%d%H%M%S) nginx -t systemctl reload nginx echo nginx_ok grep -n douyin-covers /etc/nginx/conf.d/dy-nomadro-live.conf """), flush=True) # Correct avatar existence probe + rewrite print(run(c, r""" cd /opt/douyinWEB/backend ./.venv/bin/python - <<'PY' from app.db import _connect, _lock import urllib.request, urllib.error from collections import Counter def exists(url: str) -> bool: try: urllib.request.urlopen(url, timeout=3).read(8) return True except Exception: return False with _lock: conn=_connect() rows=conn.execute("SELECT id, avatar_url FROM rooms WHERE avatar_url LIKE 'http%'").fetchall() c=Counter(); on_dy=on_s3=0 for rid,u in rows: u=str(u or '') if '/douyin-covers/' not in u: continue key=u.split('/douyin-covers/',1)[-1] local=exists('http://127.0.0.1:8333/douyin-covers/'+key) if local: on_dy+=1 new='https://dy.nomadro.live/douyin-covers/'+key else: # keep s3 host — may exist on dylive bucket new='https://s3.nomadro.com/douyin-covers/'+key if exists(new): on_s3+=1 if new!=u: conn.execute('UPDATE rooms SET avatar_url=? WHERE id=?', (new, rid)) host='dy.live' if new.startswith('https://dy.nomadro.live') else ('s3' if new.startswith('https://s3.nomadro.com') else 'other') c[host]+=1 conn.commit() print('on_dy_bucket', on_dy, 'confirmed_on_s3_public', on_s3) print('hosts', dict(c)) # smoke a few for (u,) in conn.execute("SELECT avatar_url FROM rooms WHERE avatar_url LIKE 'http%' LIMIT 8"): print('sample', u, 'ok' if exists(u) else 'MISS') PY """), flush=True) c.close() # dylive: rewrite avatars based on local existence vs dy.live print("=== dylive avatar rewrite ===", flush=True) c = connect(DYLIVE) print(run(c, r""" cd /opt/douyinWEB/backend ./.venv/bin/python - <<'PY' from app.db import _connect, _lock import urllib.request from collections import Counter def exists(url: str) -> bool: try: urllib.request.urlopen(url, timeout=4).read(8) return True except Exception: return False with _lock: conn=_connect() rows=conn.execute("SELECT id, avatar_url FROM rooms WHERE avatar_url LIKE 'http%'").fetchall() c=Counter() for rid,u in rows: u=str(u or '') if '/douyin-covers/' not in u: continue key=u.split('/douyin-covers/',1)[-1] if exists('http://127.0.0.1:8333/douyin-covers/'+key): new='https://s3.nomadro.com/douyin-covers/'+key elif exists('https://dy.nomadro.live/douyin-covers/'+key): new='https://dy.nomadro.live/douyin-covers/'+key else: new='https://s3.nomadro.com/douyin-covers/'+key # leave; cover API still works if new!=u: conn.execute('UPDATE rooms SET avatar_url=? WHERE id=?', (new, rid)) c['s3' if 's3.nomadro.com' in new else ('live' if 'dy.nomadro.live' in new else 'other')]+=1 conn.commit() print('hosts', dict(c)) ok=miss=0 for (u,) in conn.execute("SELECT avatar_url FROM rooms WHERE avatar_url LIKE 'http%' LIMIT 15"): e=exists(u) ok+=e; miss+= (not e) print('sample', 'OK' if e else 'MISS', u[-70:]) print('probe_ok', ok, 'miss', miss) PY """), flush=True) c.close() # Deploy api.js + rebuild frontend on both (or at least copy into .next if standalone hard) api_js = open(r"c:\Users\admin\Desktop\douyinWEB\frontend\src\lib\api.js", "rb").read() for label, host in [("dy", DY), ("dylive", DYLIVE)]: print(f"=== deploy api.js + rebuild FE {label} ===", flush=True) c = connect(host) put(c, api_js, "/opt/douyinWEB/frontend/src/lib/api.js") print( run( c, r""" set -e cd /opt/douyinWEB/frontend # quick rebuild — may take a few minutes if [ -f package.json ]; then export NODE_OPTIONS=--max-old-space-size=3072 npm run build 2>&1 | tail -40 systemctl restart douyinweb-web sleep 2 systemctl is-active douyinweb-web fi """, timeout=600, ), flush=True, ) c.close() print("=== final public verify ===", flush=True) urls = [ "https://dy.nomadro.live/douyin-covers/covers/ab13d4f5-9639-40db-a40f-6af8443f3757.jpg", "https://dy.nomadro.live/douyin-covers/covers/avatars/16693589495.jpg", "https://s3.nomadro.com/douyin-covers/covers/avatars/562629369787.jpg", "https://dy.nomadro.com/douyin-covers/covers/avatars/562629369787.jpg", "https://dylive.nomadro.com/api/sessions/b9197cd9-b9af-432f-8e2a-880370ce75da/cover", "https://dy.nomadro.live/api/sessions/ab13d4f5-9639-40db-a40f-6af8443f3757/cover", ] for u in urls: try: r = urllib.request.urlopen(urllib.request.Request(u, headers={"User-Agent": "v"}), timeout=20, context=CTX) print(r.status, r.headers.get("content-type"), u) except urllib.error.HTTPError as e: print("HTTP", e.code, u) except Exception as ex: print("ERR", ex, u) print("ALL_DONE", flush=True)