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

183 lines
6.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Fix dy.nomadro.com: FRP-aux S3 + ops-neko tunnels; Caddy covers + /ops-neko/*."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST, PORT, USER, PW = "107.173.30.245", 22, "root", "Xiao4669805"
CADDY = "/etc/caddy/sites/dy.nomadro.com.caddy"
UNIT = "/etc/systemd/system/dy-frp-http-aux.service"
TS = time.strftime("%Y%m%d_%H%M%S")
def run(c, cmd, t=120):
print(f"\n$ {cmd[:220]}", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
code = o.channel.recv_exit_status()
out = (o.read() + e.read()).decode("utf-8", "replace")
if out.strip():
print(out[-12000:] if len(out) > 12000 else out, flush=True)
print(f"→ {code}", flush=True)
return code, out
NEW_UNIT = r"""[Unit]
Description=dy.nomadro.com FRP-aux tunnels (HTTP+ShellCrash+Neko+S3+ops-neko via :4014)
After=network-online.target frpp.service
Wants=network-online.target
[Service]
Type=simple
Restart=always
RestartSec=3
Environment=SSHPASS=4669805
ExecStart=/usr/bin/sshpass -e ssh -N -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes \
-L 127.0.0.1:18014:127.0.0.1:8014 \
-L 127.0.0.1:14015:127.0.0.1:4015 \
-L 127.0.0.1:9999:127.0.0.1:9999 \
-L 127.0.0.1:9200:127.0.0.1:9200 \
-L 127.0.0.1:9201:127.0.0.1:9201 \
-L 127.0.0.1:9202:127.0.0.1:9202 \
-L 127.0.0.1:9203:127.0.0.1:9203 \
-L 127.0.0.1:9204:127.0.0.1:9204 \
-L 127.0.0.1:9205:127.0.0.1:9205 \
-L 127.0.0.1:9206:127.0.0.1:9206 \
-L 127.0.0.1:9207:127.0.0.1:9207 \
-L 127.0.0.1:18200:127.0.0.1:18200 \
-L 127.0.0.1:18201:127.0.0.1:18201 \
-L 127.0.0.1:18202:127.0.0.1:18202 \
-L 127.0.0.1:18203:127.0.0.1:18203 \
-L 127.0.0.1:18204:127.0.0.1:18204 \
-L 127.0.0.1:18205:127.0.0.1:18205 \
-L 127.0.0.1:18206:127.0.0.1:18206 \
-L 127.0.0.1:18207:127.0.0.1:18207 \
-L 127.0.0.1:28333:127.0.0.1:8333 \
-p 4014 eric@127.0.0.1
[Install]
WantedBy=multi-user.target
"""
# Python patch for caddy on remote
PATCH_PY = r'''
from pathlib import Path
import re
p = Path("/etc/caddy/sites/dy.nomadro.com.caddy")
text = p.read_text(encoding="utf-8")
bak = Path(f"/etc/caddy/sites/dy.nomadro.com.caddy.bak.{ts}")
bak.write_text(text, encoding="utf-8")
print("backup", bak)
# 1) douyin-covers → FRP-aux local seaweed
old = """\thandle /douyin-covers/* {
\t\treverse_proxy 10.8.0.2:8333 {
\t\t\theader_up Host {host}
\t\t}
\t}"""
new = """\thandle /douyin-covers/* {
\t\treverse_proxy 127.0.0.1:28333 {
\t\t\theader_up Host {host}
\t\t}
\t}"""
if "127.0.0.1:28333" not in text:
if old not in text:
# tolerant replace
text2, n = re.subn(
r"handle /douyin-covers/\* \{\s*reverse_proxy 10\.8\.0\.2:8333 \{",
"handle /douyin-covers/* {\n\t\treverse_proxy 127.0.0.1:28333 {",
text,
count=1,
)
if n != 1:
raise SystemExit("douyin-covers block not found")
text = text2
else:
text = text.replace(old, new, 1)
print("patched douyin-covers -> 127.0.0.1:28333")
else:
print("douyin-covers already local 28333")
# 2) insert ops-neko routes before douyin-covers if missing
OPS = """
\t# Ops-Neko tab fleet (douyinweb-ops-neko-*) via FRP-aux :1820x — independent of /neko/*
\t@opsneko_noslash path_regexp opsneko_id ^/ops-neko/([0-9]+)$
\tredir @opsneko_noslash /ops-neko/{re.opsneko_id.1}/ 308
"""
for i in range(1, 9):
port = 18199 + i # 18200..18207
OPS += f"""\thandle_path /ops-neko/{i}/* {{
\t\treverse_proxy 127.0.0.1:{port} {{
\t\t\theader_up Host {{host}}
\t\t\theader_up X-Forwarded-Proto {{scheme}}
\t\t\theader_up X-Forwarded-Host {{host}}
\t\t}}
\t}}
"""
if "handle_path /ops-neko/1/*" not in text:
needle = "\thandle /douyin-covers/* {"
if needle not in text:
raise SystemExit("cannot find insertion point before douyin-covers")
text = text.replace(needle, OPS + "\n" + needle, 1)
print("inserted ops-neko 1..8 routes")
else:
print("ops-neko routes already present")
p.write_text(text, encoding="utf-8")
print("wrote", p)
'''
def main():
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, PORT, USER, PW, timeout=60, banner_timeout=90, allow_agent=False, look_for_keys=False)
try:
# backup + write unit
run(c, f"cp -a {UNIT} {UNIT}.bak.{TS}")
sftp = c.open_sftp()
with sftp.open(UNIT, "w") as f:
f.write(NEW_UNIT)
with sftp.open(f"/tmp/patch_dy_caddy_{TS}.py", "w") as f:
f.write(PATCH_PY.replace("{ts}", TS))
sftp.close()
run(c, "systemctl daemon-reload; systemctl restart dy-frp-http-aux; sleep 3; systemctl is-active dy-frp-http-aux")
run(c, "ss -lntp | grep -E '28333|18200|18014|9200' || true")
run(c, f"python3 /tmp/patch_dy_caddy_{TS}.py")
code, out = run(c, "caddy validate --config /etc/caddy/Caddyfile 2>&1 | tail -20")
if code != 0 and "Valid configuration" not in out:
print("Caddy validate failed — restoring caddy from bak")
run(c, f"cp -a /etc/caddy/sites/dy.nomadro.com.caddy.bak.{TS} {CADDY}")
return 1
run(c, "systemctl reload caddy; sleep 2; systemctl is-active caddy")
run(
c,
r"""
set +e
curl -sS -m 12 -o /dev/null -w 'covers=%{http_code}\n' https://dy.nomadro.com/douyin-covers/
curl -sS -m 12 -o /dev/null -w 'opsneko1_noslash=%{http_code}\n' https://dy.nomadro.com/ops-neko/1
curl -sS -m 12 -o /dev/null -w 'opsneko1_slash=%{http_code}\n' https://dy.nomadro.com/ops-neko/1/
curl -sS -m 8 -o /dev/null -w 'local_28333=%{http_code}\n' http://127.0.0.1:28333/
curl -sS -m 8 -o /dev/null -w 'local_18200=%{http_code}\n' http://127.0.0.1:18200/
# sniff ops-neko is not Next login
curl -sS -m 10 -D- -o /tmp/ops1.html https://dy.nomadro.com/ops-neko/1/ | head -20
head -c 200 /tmp/ops1.html; echo
curl -sS -m 12 -o /dev/null -w 'dylive=%{http_code}\n' https://dylive.nomadro.com/api/health
curl -sS -m 12 -o /dev/null -w 'dy_api=%{http_code}\n' https://dy.nomadro.com/api/health
""",
)
return 0
finally:
c.close()
if __name__ == "__main__":
raise SystemExit(main())