# -*- coding: utf-8 -*- """tokvi: ждём открытия суточной квоты SMTP (550) -> волна -> банк ключей.""" import fcntl import json import os import socket import subprocess import sys import time from curl_cffi import requests as creq 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") D = "/root/tokvi" PXURL = "http://127.0.0.1:18899" PX = {"http": PXURL, "https": PXURL} UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/131.0.0.0" PROBE = [PROBE_EMAIL, PROBE_EMAIL2] STATE = D + "/auto_state.json" IPV6 = ",".join("http://%s:%s@%s:8888" % (PX_USER, PX_PASS, h) for h in (PX_HOST1, PX_HOST2, PX_HOST3, PX_HOST4)) def log(m): line = time.strftime("%F %T ") + str(m) print(line, flush=True) with open(D + "/auto.log", "a") as f: f.write(line + "\n") def st(): try: return json.load(open(STATE)) except Exception: return {} def save(d): json.dump(d, open(STATE, "w")) def probe(): for addr in PROBE: try: r = creq.get("https://tokvi.ai/api/verification", params={"email": addr}, headers={"User-Agent": UA}, impersonate="chrome124", timeout=25, proxies=PX) j = r.json() if j.get("success") is True: return True, "quota_open", addr return False, str(j.get("message") or "no_success")[:60], addr except Exception: log("probe exc " + type(addr).__name__) time.sleep(3) return False, "probe_fail", "" def tunnel_alive(): try: socket.create_connection(("127.0.0.1", 18899), 5).close() return True except Exception: return False def wave(tag): px = PXURL if tunnel_alive() else IPV6 log("WAVE start %s px=%s" % (tag, "zte" if px == PXURL else "ipv6pool")) env = dict(os.environ, PX=px, N="70", W="5", MAILF=D + "/mails_all.txt", USEDF=D + "/used.txt", CREDF=D + "/creds.jsonl", OUTF=D + "/wave_%s.jsonl" % tag) try: subprocess.run([sys.executable, D + "/tokvi_farm.py"], env=env, cwd=D, timeout=2400, capture_output=True) except subprocess.TimeoutExpired: log("WAVE timeout") p = D + "/wave_%s.jsonl" % tag keys = [] if os.path.exists(p): for l in open(p): try: r = json.loads(l) except Exception: continue if r.get("key"): keys.append(r) log("WAVE done keys=%d" % len(keys)) return keys def merge(): bank = {} if os.path.exists(D + "/bank.jsonl"): for l in open(D + "/bank.jsonl"): try: r = json.loads(l) if r.get("key"): bank[r["key"]] = r except Exception: pass if os.path.exists(D + "/creds.jsonl"): for l in open(D + "/creds.jsonl"): try: r = json.loads(l) if r.get("key") and r["key"] not in bank: bank[r["key"]] = {"key": r["key"], "mail": r.get("mail", ""), "ts": int(time.time())} except Exception: pass with open(D + "/bank.jsonl", "w") as f: for v in bank.values(): f.write(json.dumps(v, ensure_ascii=False) + "\n") return len(bank) def main(): lk = open(D + "/.cron.lock", "w") try: fcntl.flock(lk, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: return state = st() ok, msg, _ = probe() for _try in range(4): if ok or "频繁" not in msg: break time.sleep(6) ok, msg, _ = probe() log("probe: %s | %s | tunnel=%s" % (ok, msg, tunnel_alive())) if not ok: return t_end = time.time() + 2400 empty = 0 while time.time() < t_end and empty < 2: tag = time.strftime("%Y%m%d_%H%M") keys = wave(tag) if keys: empty = 0 state["last_wave"] = tag state["total_waves"] = state.get("total_waves", 0) + 1 else: empty += 1 ok2, msg2, _ = probe() for _try in range(3): if ok2 or "频繁" not in msg2: break time.sleep(5) ok2, msg2, _ = probe() if not ok2: log("quota closed: " + msg2) break time.sleep(20) state["bank"] = merge() state["last_probe_ok"] = time.strftime("%F %T") save(state) log("merge bank=%d" % state["bank"]) if __name__ == "__main__": main()