231 lines
7.9 KiB
Python
231 lines
7.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Finish S3 image fix: nginx dedupe, Caddy local S3, rewrite avatar hosts, verify."""
|
|
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 " + host)
|
|
|
|
|
|
def run(c, script, timeout=120):
|
|
_i, o, e = c.exec_command(script, timeout=timeout)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
def code(url):
|
|
try:
|
|
with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "v"}), timeout=20, context=CTX) as r:
|
|
return f"{r.status}"
|
|
except urllib.error.HTTPError as e:
|
|
return f"HTTP{e.code}"
|
|
except Exception as ex:
|
|
return f"ERR:{type(ex).__name__}"
|
|
|
|
|
|
# 1) Fix nginx on dy: remove duplicate inserted block, ensure one good location
|
|
print("=== fix nginx dy ===", flush=True)
|
|
c = connect(DY)
|
|
print(run(c, r"""
|
|
set -e
|
|
CONF=/etc/nginx/conf.d/dy-nomadro-live.conf
|
|
# restore from bak.covers or bak.s3fix earliest good, then patch cleanly
|
|
ls -lt ${CONF}.bak* 2>/dev/null | head -10
|
|
# Prefer bak.covers if present (known prior state), else newest bak.s3fix before our bad insert
|
|
if [ -f ${CONF}.bak.covers ]; then
|
|
cp -a ${CONF}.bak.covers ${CONF}.frombak
|
|
echo using bak.covers
|
|
elif ls ${CONF}.bak.s3fix.* >/dev/null 2>&1; then
|
|
# first bak created by our script is pre-insert
|
|
FIRST=$(ls -1t ${CONF}.bak.s3fix.* | tail -1)
|
|
cp -a "$FIRST" ${CONF}.frombak
|
|
echo using $FIRST
|
|
else
|
|
cp -a "$CONF" ${CONF}.frombak
|
|
fi
|
|
python3 - <<'PY'
|
|
from pathlib import Path
|
|
import re
|
|
src=Path('/etc/nginx/conf.d/dy-nomadro-live.conf.frombak')
|
|
dst=Path('/etc/nginx/conf.d/dy-nomadro-live.conf')
|
|
text=src.read_text(encoding='utf-8', errors='replace')
|
|
# strip any existing /douyin-covers locations
|
|
text2=re.sub(r"\n\s*location\s+/douyin-covers/\s*\{[\s\S]*?\n\s*\}", "\n", text)
|
|
block='''
|
|
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;
|
|
}
|
|
'''
|
|
# insert inside first server { after server_name line
|
|
m=re.search(r"(server_name\s+[^;]+;)", text2)
|
|
if not m:
|
|
raise SystemExit('no server_name')
|
|
ins=m.end()
|
|
text2=text2[:ins]+"\n"+block+text2[ins:]
|
|
dst.write_text(text2, encoding='utf-8')
|
|
print('wrote clean nginx conf')
|
|
# count locations
|
|
print('loc_count', text2.count('location /douyin-covers/'))
|
|
PY
|
|
nginx -t
|
|
systemctl reload nginx
|
|
echo nginx_ok
|
|
curl -sS -m 5 -o /dev/null -w 'https_cover:%{http_code}\n' https://dy.nomadro.live/douyin-covers/covers/ab13d4f5-9639-40db-a40f-6af8443f3757.jpg || true
|
|
"""), flush=True)
|
|
|
|
# rewrite avatar hosts in DB on dy
|
|
print(run(c, r"""
|
|
set -e
|
|
cd /opt/douyinWEB/backend
|
|
./.venv/bin/python - <<'PY'
|
|
from app.db import _connect, _lock
|
|
repls=[
|
|
('https://dy.nomadro.com/douyin-covers','https://s3.nomadro.com/douyin-covers'),
|
|
('http://dy.nomadro.com/douyin-covers','https://s3.nomadro.com/douyin-covers'),
|
|
]
|
|
with _lock:
|
|
conn=_connect()
|
|
total=0
|
|
for a,b in repls:
|
|
cur=conn.execute("SELECT COUNT(*) FROM rooms WHERE avatar_url LIKE ?", (a+'%',))
|
|
n=cur.fetchone()[0]
|
|
if n:
|
|
conn.execute("UPDATE rooms SET avatar_url=REPLACE(avatar_url, ?, ?) WHERE avatar_url LIKE ?", (a,b,a+'%'))
|
|
total+=n
|
|
print('rooms', a, '->', b, n)
|
|
# also pool covers if any
|
|
try:
|
|
for a,b in repls:
|
|
cur=conn.execute("SELECT COUNT(*) FROM yt_assign_pool_urls WHERE cover_url LIKE ?", (a+'%',))
|
|
n=cur.fetchone()[0]
|
|
if n:
|
|
conn.execute("UPDATE yt_assign_pool_urls SET cover_url=REPLACE(cover_url, ?, ?) WHERE cover_url LIKE ?", (a,b,a+'%'))
|
|
print('pool', a, n)
|
|
except Exception as e:
|
|
print('pool_skip', e)
|
|
conn.commit()
|
|
print('rewrote_rooms', total)
|
|
# sample
|
|
for r in conn.execute("SELECT substr(avatar_url,1,100) FROM rooms WHERE avatar_url LIKE 'http%' LIMIT 5"):
|
|
print('sample', r[0])
|
|
PY
|
|
"""), flush=True)
|
|
c.close()
|
|
|
|
# 2) dylive: Caddy point to local S3; rewrite DB avatars
|
|
print("=== fix dylive caddy+db ===", flush=True)
|
|
c = connect(DYLIVE)
|
|
print(run(c, r"""
|
|
set -e
|
|
CFG=/etc/caddy/sites/dy.nomadro.com.caddy
|
|
# point to local seaweed (s3.nomadro.com backend) — both VPS; avatars rewritten to s3 host anyway
|
|
python3 - <<'PY'
|
|
from pathlib import Path
|
|
import re
|
|
p=Path('/etc/caddy/sites/dy.nomadro.com.caddy')
|
|
text=p.read_text(encoding='utf-8')
|
|
text2,n=re.subn(
|
|
r'(handle /douyin-covers/\* \{\n\s*reverse_proxy )[^\s{]+',
|
|
r'\g<1>127.0.0.1:8333',
|
|
text,
|
|
count=1,
|
|
)
|
|
print('n', n)
|
|
if not n:
|
|
raise SystemExit('patch fail')
|
|
p.write_text(text2, encoding='utf-8')
|
|
print(Path('/etc/caddy/sites/dy.nomadro.com.caddy').read_text().split('douyin-covers')[1][:200])
|
|
PY
|
|
caddy validate --config /etc/caddy/Caddyfile >/tmp/caddyval.out 2>&1 || true
|
|
tail -3 /tmp/caddyval.out
|
|
systemctl reload caddy
|
|
echo caddy_ok
|
|
"""), flush=True)
|
|
|
|
print(run(c, r"""
|
|
set -e
|
|
cd /opt/douyinWEB/backend
|
|
./.venv/bin/python - <<'PY'
|
|
from app.db import _connect, _lock
|
|
repls=[
|
|
('https://dy.nomadro.com/douyin-covers','https://s3.nomadro.com/douyin-covers'),
|
|
('http://dy.nomadro.com/douyin-covers','https://s3.nomadro.com/douyin-covers'),
|
|
('https://dy.nomadro.live/douyin-covers','https://s3.nomadro.com/douyin-covers'),
|
|
]
|
|
with _lock:
|
|
conn=_connect()
|
|
for a,b in repls:
|
|
n=conn.execute("SELECT COUNT(*) FROM rooms WHERE avatar_url LIKE ?", (a+'%',)).fetchone()[0]
|
|
if n:
|
|
conn.execute("UPDATE rooms SET avatar_url=REPLACE(avatar_url, ?, ?) WHERE avatar_url LIKE ?", (a,b,a+'%'))
|
|
print('rooms', a, n)
|
|
try:
|
|
for a,b in repls:
|
|
n=conn.execute("SELECT COUNT(*) FROM yt_assign_pool_urls WHERE cover_url LIKE ?", (a+'%',)).fetchone()[0]
|
|
if n:
|
|
conn.execute("UPDATE yt_assign_pool_urls SET cover_url=REPLACE(cover_url, ?, ?) WHERE cover_url LIKE ?", (a,b,a+'%'))
|
|
print('pool', a, n)
|
|
except Exception as e:
|
|
print('pool_skip', e)
|
|
conn.commit()
|
|
for r in conn.execute("SELECT substr(avatar_url,1,110) FROM rooms WHERE avatar_url LIKE 'http%' LIMIT 6"):
|
|
print('sample', r[0])
|
|
# existence probe via local s3 for first 8 avatars
|
|
import urllib.request
|
|
rows=conn.execute("SELECT avatar_url FROM rooms WHERE avatar_url LIKE 'https://s3.nomadro.com/%' LIMIT 12").fetchall()
|
|
ok=bad=0
|
|
for (u,) in rows:
|
|
key=u.split('douyin-covers/',1)[-1]
|
|
try:
|
|
code=urllib.request.urlopen('http://127.0.0.1:8333/douyin-covers/'+key, timeout=3).status
|
|
ok+=1
|
|
except Exception:
|
|
bad+=1
|
|
print('missing', u[-60:])
|
|
print('s3_probe_ok', ok, 'bad', bad)
|
|
PY
|
|
"""), flush=True)
|
|
c.close()
|
|
|
|
# 3) FE fallback: if avatar 404, browsers still blank — improve sessionCoverSrc to prefer cover when
|
|
# avatar host historically broken. Safer: keep s3 rewrite; add small FE onerror is heavier deploy.
|
|
# Also ensure dylive DY_S3_PUBLIC_BASE stays s3.nomadro.com for new uploads.
|
|
|
|
print("=== public verify ===", flush=True)
|
|
urls = [
|
|
"https://dy.nomadro.live/douyin-covers/covers/ab13d4f5-9639-40db-a40f-6af8443f3757.jpg",
|
|
"https://s3.nomadro.com/douyin-covers/covers/avatars/562629369787.jpg",
|
|
"https://s3.nomadro.com/douyin-covers/covers/avatars/288944177940.jpg",
|
|
"https://dy.nomadro.com/douyin-covers/covers/avatars/288944177940.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:
|
|
print(code(u), u, flush=True)
|
|
|
|
print("ALL_DONE", flush=True)
|