douyinWEB/scripts/_fix_nomadro_caddy_next.py
2026-09-13 22:10:49 -05:00

89 lines
2.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Fix Caddy: remove duplicate site; apex nomadro.com → Next :4003 only."""
from __future__ import annotations
import sys
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
REMOTE = r"""#!/bin/bash
set -e
# remove conflicting import site we created
rm -f /etc/caddy/sites/nomadro.com.caddy
echo 'removed sites/nomadro.com.caddy'
# backup then point apex at Next :4003 (skip audience router / WP)
cp -a /etc/caddy/Caddyfile /etc/caddy/Caddyfile.bak.nomadro_next_$(date +%Y%m%d_%H%M%S)
python3 - <<'PY'
from pathlib import Path
p = Path("/etc/caddy/Caddyfile")
text = p.read_text(encoding="utf-8")
old = """# Audience router: zh → Next.js, en/bots → WordPress
nomadro.com {
encode gzip
reverse_proxy 127.0.0.1:4403
}"""
new = """# Next.js only (WordPress / audience router not used)
nomadro.com {
encode gzip
reverse_proxy 127.0.0.1:4003
}"""
if old not in text:
# fallback replace port only inside nomadro.com block
import re
text2, n = re.subn(
r"(nomadro\.com\s*\{[^}]*reverse_proxy\s+)127\.0\.0\.1:4403",
r"\g<1>127.0.0.1:4003",
text,
count=1,
flags=re.S,
)
if n != 1:
raise SystemExit(f"patch failed n={n}")
text = text2
else:
text = text.replace(old, new, 1)
p.write_text(text, encoding="utf-8")
print("patched Caddyfile")
PY
head -20 /etc/caddy/Caddyfile
caddy validate --config /etc/caddy/Caddyfile
systemctl reload caddy || systemctl restart caddy
sleep 1
systemctl is-active caddy nomadro.service
# ensure next is up
systemctl restart nomadro.service
sleep 2
systemctl is-active nomadro.service
ss -lntp | grep ':4003' || true
curl -sS -m 10 -o /dev/null -w 'local=%{http_code}\n' http://127.0.0.1:4003/
curl -sS -m 15 -o /dev/null -w 'public=%{http_code}\n' -L https://nomadro.com/
curl -sS -m 15 -o /dev/null -w 'www=%{http_code}\n' -L https://www.nomadro.com/
# do not touch wordpress containers
echo 'wp_left_as_is:'
docker ps --format '{{.Names}} {{.Status}}' | grep -i wordpress || true
systemctl is-active nomadro-router.service || echo 'router=inactive (ok)'
"""
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", 22, "root", "Xiao4669805", timeout=40, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
with sftp.file("/tmp/_fix_nomadro_caddy.sh", "w") as f:
f.write(REMOTE)
sftp.close()
_, o, e = c.exec_command("bash /tmp/_fix_nomadro_caddy.sh", timeout=90)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())