# -*- coding: utf-8 -*- """Extend edge proxies for neko 5–8; start neko-3 if stopped.""" from __future__ import annotations import re import sys import time from io import BytesIO import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") MAX_NEKO = 8 UP_COM = "10.8.0.2" UP_CN = "10.9.0.2" def ssh(host, port, user, pw): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host, port, user, pw, timeout=60, allow_agent=False, look_for_keys=False) return c def run(c, cmd, t=120): _, o, e = c.exec_command(cmd, timeout=t) return (o.read() + e.read()).decode("utf-8", "replace") def caddy_blocks(missing: list[int]) -> str: parts = [] for n in missing: port = 9199 + n parts.append( f"\thandle /neko/{n} {{\n" f"\t\tredir /neko/{n}/ 308\n" f"\t}}\n" f"\thandle_path /neko/{n}/* {{\n" f"\t\treverse_proxy {UP_COM}:{port} {{\n" f"\t\t\theader_up Host {{host}}\n" f"\t\t\theader_up X-Forwarded-Proto {{scheme}}\n" f"\t\t\theader_up X-Forwarded-Host {{host}}\n" f"\t\t}}\n" f"\t}}\n" ) return "".join(parts) def nginx_blocks(missing: list[int]) -> str: parts = [] for n in missing: port = 9199 + n parts.append( f"\n location = /neko/{n} {{ return 308 /neko/{n}/; }}\n" f" location /neko/{n}/ {{\n" f" proxy_pass http://{UP_CN}:{port}/;\n" f" proxy_http_version 1.1;\n" f" proxy_set_header Upgrade $http_upgrade;\n" f" proxy_set_header Connection $connection_upgrade;\n" f" proxy_set_header Host $host;\n" f" proxy_set_header X-Forwarded-Host $host;\n" f" proxy_set_header X-Real-IP $remote_addr;\n" f" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n" f" proxy_set_header X-Forwarded-Proto $scheme;\n" f" proxy_read_timeout 3600s;\n" f" proxy_send_timeout 3600s;\n" f" proxy_buffering off;\n" f" }}\n" ) return "".join(parts) def main(): # --- overseas Caddy --- v = ssh("107.173.30.245", 22, "root", "Xiao4669805") path = "/etc/caddy/sites/dy.nomadro.com.caddy" text = run(v, f"cat {path}") missing = [n for n in range(1, MAX_NEKO + 1) if f"handle_path /neko/{n}/*" not in text] print("caddy missing:", missing, flush=True) if missing: blocks = caddy_blocks(missing) matches = list(re.finditer(r"handle_path /neko/\d+/\* \{[\s\S]*?\n\t\}\n", text)) if matches: pos = matches[-1].end() new_text = text[:pos] + blocks + text[pos:] else: new_text = text.replace("{\n", "{\n" + blocks, 1) run(v, f"cp -a {path} {path}.bak-neko-$(date +%s)") sftp = v.open_sftp() with sftp.file(path, "w") as f: f.write(new_text) sftp.close() print( run( v, "caddy validate --config /etc/caddy/Caddyfile 2>&1 | tail -8; " "systemctl reload caddy; sleep 1; systemctl is-active caddy", ), flush=True, ) # --- physical start neko3 --- g = ssh("107.173.30.245", 4014, "eric", "4669805") print( run( g, "echo '4669805' | sudo -S -p '' bash -lc '" "docker start douyinweb-neko-3 2>&1; sleep 3; " "docker ps -a --filter name=douyinweb-neko- --format \"{{.Names}} {{.Status}}\" | sort; " "for i in 1 2 3 4 5 6; do " "c=$(curl -sS -m 3 -o /dev/null -w \"%{http_code}\" http://127.0.0.1:$((9199+i))/ || echo x); " "echo local$i=$c; done" "'", 90, ), flush=True, ) g.close() # --- tencent nginx --- 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}") nmiss = [n for n in range(1, MAX_NEKO + 1) if f"location /neko/{n}/" not in ntext] print("nginx missing:", nmiss, flush=True) if nmiss: blocks = nginx_blocks(nmiss) marker = " location / {\n proxy_pass http://dy_cn_web;" if marker not in ntext: raise SystemExit("nginx web location marker missing") new_n = ntext.replace(marker, blocks + "\n" + marker, 1) run(d, f"sudo cp -a {npath} {npath}.bak-neko-$(date +%s)") sftp = d.open_sftp() with sftp.file("/tmp/dy.nomadro.cn.conf.new", "w") as f: f.write(new_n) sftp.close() print( run( d, f"sudo mv /tmp/dy.nomadro.cn.conf.new {npath}; " "sudo nginx -t 2>&1 | tail -10; sudo nginx -s reload; echo nginx_ok", ), flush=True, ) d.close() time.sleep(2) print("=== verify ===", flush=True) print( run( v, r""" for i in 1 2 3 4 5 6; do code=$(curl -sS -m 12 -o /dev/null -w "%{http_code}" "https://dy.nomadro.com/neko/$i/" || echo err) echo "com/$i=$code" done curl -sS -m 12 -o /dev/null -w "dylive=%{http_code}\n" https://dylive.nomadro.com/api/health grep -n 'neko/[56]' /etc/caddy/sites/dy.nomadro.com.caddy | head """, ), flush=True, ) v.close() print("DONE — hard refresh neko/5 and neko/6", flush=True) if __name__ == "__main__": main()