# -*- coding: utf-8 -*- """Bring API up; confirm login works publicly.""" from __future__ import annotations import json import sys import time import http.cookiejar import urllib.request import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") REMOTE = r''' import subprocess, time, urllib.request, json, os, sys os.chdir("/opt/douyinWEB/backend") def health(): try: with urllib.request.urlopen("http://127.0.0.1:8014/api/health", timeout=3) as r: return r.status except Exception: return 0 print("before", health(), flush=True) if health()!=200: subprocess.run("systemctl kill -s SIGKILL douyinweb-api || true", shell=True) subprocess.run("pkill -9 -f 'uvicorn app.main:app' || true", shell=True) time.sleep(2) subprocess.run("systemctl reset-failed douyinweb-api || true", shell=True) subprocess.run("systemctl start douyinweb-api", shell=True) for i in range(90): time.sleep(2) h=health() if i%5==0: print("wait", i, h, flush=True) if h==200: print("UP", i, flush=True); break else: print(subprocess.getoutput("journalctl -u douyinweb-api --no-pager -n 30")) # ensure hash still correct under live secret pid=subprocess.getoutput("systemctl show douyinweb-api -p MainPID --value").strip() print("pid", pid, flush=True) env={} if pid.isdigit() and int(pid)>1: for x in open(f"/proc/{pid}/environ","rb").read().decode("utf-8","replace").split("\0"): if "=" in x: k,v=x.split("=",1); env[k]=v for k,v in env.items(): if k.startswith("DY_"): os.environ[k]=v sys.path.insert(0,".") from importlib import reload import app.config as cfg; reload(cfg) from app.config import settings from app.session_auth import hash_password, verify_admin, ensure_default_admin_credentials from app import db ensure_default_admin_credentials() exp=hash_password(str(settings.admin_password or "")) db.update_settings_map({"admin_username":"hackrobot","admin_password_hash":exp}) print("secret_len", len(str(settings.session_secret or "")), "verify", verify_admin("hackrobot","Xiao4669805@"), flush=True) req=urllib.request.Request("http://127.0.0.1:8014/api/auth/login", data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(), headers={"Content-Type":"application/json"}, method="POST") with urllib.request.urlopen(req, timeout=15) as r: print("login", r.status, r.read()[:100], flush=True) print("set-cookie", (r.headers.get("Set-Cookie") or "")[:120], flush=True) ''' c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() with sftp.file("/tmp/_auth_up.py", "w") as f: f.write(REMOTE) sftp.close() chan = c.get_transport().open_session() chan.settimeout(15) chan.exec_command("echo '4669805' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python -u /tmp/_auth_up.py") deadline = time.time() + 220 while time.time() < deadline: if chan.recv_ready(): print(chan.recv(4096).decode("utf-8", "replace"), end="", flush=True) if chan.recv_stderr_ready(): print(chan.recv_stderr(4096).decode("utf-8", "replace"), end="", flush=True) if chan.exit_status_ready(): while chan.recv_ready(): print(chan.recv(4096).decode("utf-8", "replace"), end="", flush=True) break time.sleep(0.3) print("exit", chan.recv_exit_status(), flush=True) c.close() cj = http.cookiejar.CookieJar() op = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) req = urllib.request.Request( "https://dy.nomadro.com/api/auth/login", data=json.dumps({"username": "hackrobot", "password": "Xiao4669805@"}).encode(), headers={"Content-Type": "application/json"}, method="POST", ) try: with op.open(req, timeout=25) as r: print("public_login", r.status, r.read()[:150], flush=True) except Exception as ex: print("public_login_err", ex, flush=True) try: print(ex.read()[:200]) # type: ignore except Exception: pass print("cookies", [(x.name, x.domain, x.secure, x.path) for x in cj], flush=True) try: with op.open("https://dy.nomadro.com/api/auth/status", timeout=20) as r: print("status", r.read().decode(), flush=True) except Exception as ex: print("status_err", ex, flush=True) try: with op.open("https://dy.nomadro.com/api/rooms?limit=1", timeout=20) as r: print("rooms", r.status, r.read()[:120], flush=True) except Exception as ex: print("rooms_err", getattr(ex, "code", None), ex, flush=True)