# -*- coding: utf-8 -*- """ar_auto.py — сторож api-route: проба почтового канала -> волна рег -> health -> инжект в iishko. Запуск по крону (раз в 30 мин). Молчит, если канал закрыт. """ import json import os import subprocess import sys import time import os as _os PROD_HOST = _os.environ.get("PROD_HOST", "root@127.0.0.1") FARM_HOST = _os.environ.get("FARM_HOST", "root@127.0.0.1") PX_USER = _os.environ.get("PX_USER", "") PX_PASS = _os.environ.get("PX_PASS", "") PX_HOST1 = _os.environ.get("PX_HOST1", "127.0.0.1") PX_HOST2 = _os.environ.get("PX_HOST2", "127.0.0.1") PX_HOST3 = _os.environ.get("PX_HOST3", "127.0.0.1") PX_HOST4 = _os.environ.get("PX_HOST4", "127.0.0.1") MAIL_LOCAL = _os.environ.get("MAIL_LOCAL", "user") PROBE_EMAIL = _os.environ.get("PROBE_EMAIL", "probe@example.com") PROBE_EMAIL2 = _os.environ.get("PROBE_EMAIL2", "probe2@example.com") PROBE_EMAIL3 = _os.environ.get("PROBE_EMAIL3", "probe3@example.com") TMP = r"C:\Users\User\tmp" PY = r"C:\Users\User\AppData\Local\Programs\Python\Python311\python.exe" LOG = os.path.join(TMP, "ar_auto.log") STATE = os.path.join(TMP, "ar_auto_state.json") PROBE = PROBE_EMAIL3 LANES = ("8891", "8892", "8893", "8894") def log(msg): line = "%s %s" % (time.strftime("%Y-%m-%d %H:%M:%S"), msg) print(line, flush=True) with open(LOG, "a", encoding="utf-8") as f: f.write(line + "\n") def state(): try: return json.load(open(STATE, encoding="utf-8")) except Exception: return {} def probe(): """True только если API реально принял адрес к отправке.""" import urllib.error import urllib.request body = json.dumps({"email": PROBE}).encode() req = urllib.request.Request( "https://www.api-route.com/api/dist/user/email-verification", data=body, headers={"Content-Type": "application/json"}) try: r = urllib.request.urlopen(req, timeout=30) j = json.loads(r.read().decode("utf-8", "replace")) return bool(j.get("success")) is True, str(j.get("message") or "")[:60] except Exception as e: return False, "%s %s" % (type(e).__name__, str(e)[:50]) def browsers_running(): import psutil n = 0 for p in psutil.process_iter(["cmdline"]): cl = p.info.get("cmdline") or [] if cl and "browser_reg_ar2.py" in " ".join(cl): n += 1 return n def run(cmd, timeout=None): p = subprocess.run(cmd, shell=True, capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=timeout) return p.returncode, ((p.stdout or "") + (p.stderr or "")).strip() def main(): ok, msg = probe() st = state() st["last_probe"] = time.strftime("%F %T") st["probe_ok"] = ok json.dump(st, open(STATE, "w")) log("probe: %s | %s" % (ok, msg)) if not ok: return before = sum(1 for _ in open(os.path.join(TMP, "panel_bank.jsonl"), encoding="utf-8", errors="replace")) log("channel open, bank lines=%d -> wave" % before) env = dict(os.environ) env.pop("PYTHONPATH", None) env["USERPROFILE"] = r"C:\Users\User" subprocess.run([PY, os.path.join(TMP, "spawn_ar.py"), "10"], cwd=TMP, env=env, capture_output=True, text=True, timeout=180) t_end = time.time() + 2700 while time.time() < t_end and browsers_running() > 0: time.sleep(60) after = sum(1 for _ in open(os.path.join(TMP, "panel_bank.jsonl"), encoding="utf-8", errors="replace")) log("wave done, bank lines=%d (+%d)" % (after, after - before)) for script, args in (("ar_health.py", " --apply"),): rc, out = run('scp -o StrictHostKeyChecking=no %s " + PROD_HOST + ":/root/%s' % (os.path.join(TMP, script), script), timeout=180) rc, out = run('ssh -o StrictHostKeyChecking=no " + PROD_HOST + " "cd /root && python3 %s%s"' % (script, args), timeout=900) log("health rc=%s %s" % (rc, out.replace("\n", " ")[-220:])) rc, out = run('cd %s && %s ar_sync.py' % (TMP, PY), timeout=1200) log("sync rc=%s %s" % (rc, out.replace("\n", " ")[-260:])) if __name__ == "__main__": main()