douyinWEB/scripts/_fix_neko_force_slash.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

151 lines
4.7 KiB
Python

# -*- coding: utf-8 -*-
"""Force /neko/{id} → /neko/{id}/ on both edges; verify non-empty HTML + assets."""
from __future__ import annotations
import re
import sys
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
REDIR_CADDY = """
\t# Force trailing slash so relative Neko assets resolve under /neko/{id}/
\t@neko_noslash path_regexp neko_id ^/neko/([0-9]+)$
\tredir @neko_noslash /neko/{re.neko_id.1}/ 308
"""
REDIR_NGINX = """
# Force trailing slash for Neko relative assets
location ~ ^/neko/([0-9]+)$ {
return 308 /neko/$1/;
}
"""
def ssh(h, p, u, pw):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(h, p, u, pw, timeout=45, allow_agent=False, look_for_keys=False)
return c
def run(c, cmd, t=90):
_, o, e = c.exec_command(cmd, timeout=t)
return (o.read() + e.read()).decode("utf-8", "replace")
def main():
v = ssh("107.173.30.245", 22, "root", "Xiao4669805")
path = "/etc/caddy/sites/dy.nomadro.com.caddy"
text = run(v, f"cat {path}")
# remove old per-id exact redir handles (keep handle_path)
text2 = re.sub(
r"\n\thandle /neko/\d+ \{\n\t\tredir /neko/\d+/ 308\n\t\}\n",
"\n",
text,
)
text2 = re.sub(
r"\n\t# Force trailing slash[\s\S]*?redir @neko_noslash /neko/\{re\.neko_id\.1\}/ 308\n",
"\n",
text2,
)
# insert redirect block immediately before first handle_path /neko/
if "handle_path /neko/1/*" in text2:
text2 = text2.replace(
"\thandle_path /neko/1/*",
REDIR_CADDY + "\thandle_path /neko/1/*",
1,
)
else:
raise SystemExit("cannot find handle_path /neko/1")
run(v, f"cp -a {path} {path}.bak-slash-$(date +%s)")
sftp = v.open_sftp()
with sftp.file(path, "w") as f:
f.write(text2)
sftp.close()
print(
run(
v,
"caddy validate --config /etc/caddy/Caddyfile 2>&1 | tail -5; "
"systemctl reload caddy; sleep 1; systemctl is-active caddy",
),
flush=True,
)
d = ssh("82.157.112.245", 22, "ubuntu", "Xiao4669805")
npath = "/www/server/panel/vhost/nginx/dy.nomadro.cn.conf"
ntext = run(d, f"sudo cat {npath}")
ntext2 = re.sub(
r"\n\s*# Force trailing slash[\s\S]*?location ~ \^/neko/\(\[0-9\]\+\)\$ \{[\s\S]*?\n\s*\}\n",
"\n",
ntext,
)
# remove per-id exact locations that only redirect
ntext2 = re.sub(
r"\n\s*location = /neko/\d+ \{ return 308 /neko/\d+/; \}\n",
"\n",
ntext2,
)
if "location /neko/1/" in ntext2:
ntext2 = ntext2.replace(
" location /neko/1/",
REDIR_NGINX + "\n location /neko/1/",
1,
)
else:
raise SystemExit("nginx neko/1 missing")
run(d, f"sudo cp -a {npath} {npath}.bak-slash-$(date +%s)")
sftp = d.open_sftp()
with sftp.file("/tmp/dy.cn.slash.conf", "w") as f:
f.write(ntext2)
sftp.close()
print(
run(
d,
f"sudo mv /tmp/dy.cn.slash.conf {npath}; sudo nginx -t 2>&1 | tail -6; "
"sudo nginx -s reload; echo nginx_ok",
),
flush=True,
)
d.close()
import time
time.sleep(2)
print("=== SELFTEST ===", flush=True)
with httpx.Client(follow_redirects=False, timeout=20) as c:
r = c.get("https://dy.nomadro.com/neko/5")
print("bare", r.status_code, "loc", r.headers.get("location"), "len", len(r.content))
assert r.status_code in (301, 302, 307, 308), f"expected redirect got {r.status_code}"
assert r.headers.get("location", "").rstrip("/").endswith("/neko/5") or "/neko/5/" in r.headers.get(
"location", ""
), r.headers.get("location")
with httpx.Client(follow_redirects=True, timeout=25) as c:
r = c.get("https://dy.nomadro.com/neko/5")
print("final", r.url, "html", r.status_code, "len", len(r.content))
assert str(r.url).endswith("/neko/5/")
assert len(r.content) > 500
assets = re.findall(r'src="([^"]+)"', r.text)
base = str(r.url)
bad = []
for a in assets:
url = base + a
ar = c.get(url)
print(" asset", a, ar.status_code, len(ar.content))
if ar.status_code != 200:
bad.append(a)
assert not bad, bad
# compare neko/1
r1 = c.get("https://dy.nomadro.com/neko/1/")
print("neko1", r1.status_code, len(r1.content))
print("PASS", flush=True)
# show caddy snippet
print(run(v, "grep -n 'neko_noslash\\|Force trailing' /etc/caddy/sites/dy.nomadro.com.caddy | head"), flush=True)
v.close()
if __name__ == "__main__":
main()