# -*- coding: utf-8 -*- """Compare DB admin hash vs expected; reset to env password if mismatched.""" from __future__ import annotations import sys import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") REMOTE = r''' import os, sys, json, urllib.request os.chdir("/opt/douyinWEB/backend") sys.path.insert(0, ".") from app.config import settings from app import db from app.session_auth import ( hash_password, get_admin_credentials, verify_admin, ensure_default_admin_credentials, _secret, ) m = db.get_settings_map() print("db_admin_user", repr(m.get("admin_username"))) db_hash = str(m.get("admin_password_hash") or "") print("db_hash_len", len(db_hash), "pref", db_hash[:10]) exp = hash_password(str(settings.admin_password or "")) print("expected_len", len(exp), "pref", exp[:10]) print("match", db_hash == exp) print("secret_len", len(_secret()), "secret_pref", _secret()[:8]) u,h = get_admin_credentials() print("get_admin", repr(u), "hash_pref", h[:10], "eq_expected", h==exp) print("verify_before", verify_admin("hackrobot", settings.admin_password)) # Was ensure ever applied? print("calling ensure_default_admin_credentials()") ensure_default_admin_credentials() m2 = db.get_settings_map() db_hash2 = str(m2.get("admin_password_hash") or "") print("after_ensure match", db_hash2 == exp, "changed", db_hash2 != db_hash) print("verify_after_ensure", verify_admin("hackrobot", settings.admin_password)) # Force reset to current env password (operator recovery) db.update_settings_map({ "admin_username": str(settings.admin_username or "hackrobot").strip(), "admin_password_hash": exp, }) print("forced_reset verify", verify_admin("hackrobot", "Xiao4669805@")) # HTTP login 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", ) try: with urllib.request.urlopen(req, timeout=10) as r: print("login", r.status, r.read()[:120], "cookie", (r.headers.get("Set-Cookie") or "")[:100]) except Exception as e: body=b"" try: body=e.read() except Exception: pass print("login_fail", getattr(e,"code",None), body[:160]) ''' 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_fix.py", "w") as f: f.write(REMOTE) sftp.close() _, o, e = c.exec_command( "echo '4669805' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_auth_fix.py", timeout=60, ) print((o.read() + e.read()).decode("utf-8", "replace")) c.close()