#!/usr/bin/env python3 """Replace dylive key registry with dy export (dedupe); re-verify both.""" from __future__ import annotations import json import ssl import sys import time import urllib.request from http.cookiejar import CookieJar import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") CTX = ssl.create_default_context() APP_PASS = "Xiao4669805@" SSH_PASS = "Xiao4669805" def ssh(host: str): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for i in range(8): try: c.connect( host, 22, "root", SSH_PASS, timeout=50, allow_agent=False, look_for_keys=False, banner_timeout=180, ) return c except Exception as e: print("retry", host, i, e, flush=True) time.sleep(2 + i) raise SystemExit("ssh fail") def run(c, cmd: str, t: int = 120) -> str: _i, o, e = c.exec_command(cmd, timeout=t) return (o.read() + e.read()).decode("utf-8", "replace") def http_check(label: str, base: str): cj = CookieJar() op = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(cj), urllib.request.HTTPSHandler(context=CTX), ) req = urllib.request.Request( f"{base}/api/auth/login", data=json.dumps({"username": "hackrobot", "password": APP_PASS}).encode(), headers={"Content-Type": "application/json"}, method="POST", ) op.open(req, timeout=45).read() with op.open(f"{base}/api/youtube/stream-keys", timeout=45) as r: sk = json.loads(r.read().decode()) with op.open(f"{base}/api/youtube/assign/groups", timeout=45) as r: ag = json.loads(r.read().decode()) groups = ag.get("groups") or [] full = sum(1 for g in groups if len(str(g.get("stream_key") or "")) >= 12) names = [(g.get("name") or g.get("id")) for g in groups] print( f"{label}: registry={sk.get('total')} assign={len(groups)} full_keys={full}", flush=True, ) missing = [g.get("name") for g in groups if len(str(g.get("stream_key") or "")) < 12] if missing: print(f" missing keys: {missing}") return set(str(n) for n in names if n) print("export dy…", flush=True) dy = ssh("108.174.50.68") out = run( dy, r""" cd /opt/douyinWEB/backend ./.venv/bin/python - <<'PY' import json from app import yt_stream_key_db print('REG_START') print(json.dumps(yt_stream_key_db.export_all(), ensure_ascii=False)) print('REG_END') PY """, ) dy.close() a, b = out.find("REG_START"), out.find("REG_END") reg = json.loads(out[a + len("REG_START") : b].strip()) items = reg.get("items") or [] print(f"dy registry items={len(items)}", flush=True) dl = ssh("107.173.30.245") sftp = dl.open_sftp() with sftp.file("/tmp/dy_stream_key_export.json", "w") as f: f.write(json.dumps(reg, ensure_ascii=False)) sftp.close() print( run( dl, r""" cd /opt/douyinWEB/backend ./.venv/bin/python - <<'PY' import json from app import yt_stream_key_db with open('/tmp/dy_stream_key_export.json', encoding='utf-8') as f: reg = json.load(f) items = reg.get('items') or [] print(yt_stream_key_db.import_payload(items, replace=True)) print(yt_stream_key_db.list_entries(include_secret=False)['total']) PY """, ), flush=True, ) dl.close() print("\nverify HTTP:", flush=True) dy_names = http_check("dy", "https://dy.nomadro.live") dl_names = http_check("dylive", "https://dylive.nomadro.com") print("only_on_dy:", sorted(dy_names - dl_names)) print("only_on_dylive:", sorted(dl_names - dy_names)) print("DONE")