214 lines
7.8 KiB
Python
214 lines
7.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Minara reg core: baseline UID -> send code -> wait NEW mail -> verify.
|
|
|
|
usage: python core.py <email> <pass> [invite|none]
|
|
"""
|
|
import imaplib, json, os, poplib, random, re, sys, time
|
|
|
|
if os.environ.get("MINARA_FORCE_V4"):
|
|
import socket as _sock
|
|
|
|
_orig_gai = _sock.getaddrinfo
|
|
|
|
def _v4_only(host, port, family=0, type=0, proto=0, flags=0):
|
|
return _orig_gai(host, port, _sock.AF_INET, type, proto, flags)
|
|
|
|
_sock.getaddrinfo = _v4_only
|
|
import email as emaillib
|
|
import requests
|
|
|
|
HERE = os.environ.get("MINARA_HOME") or os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, HERE)
|
|
from flow import solve_captcha
|
|
|
|
API = "https://api.minara.ai"
|
|
INVITE = os.environ.get("MINARA_INVITE", "UM9Y8P")
|
|
PROXIES = [p.strip() for p in os.environ.get("MINARA_PROXY_POOL", "").split(",") if p.strip()]
|
|
IMAPMAP_PATH = os.environ.get("MINARA_IMAPMAP") or os.path.join(HERE, "discovered_imap.json")
|
|
IMAPMAP = json.load(open(IMAPMAP_PATH)) if os.path.exists(IMAPMAP_PATH) else {}
|
|
POP_HOSTS = {"rediffmail.com": ("pop.rediffmail.com", 995)}
|
|
BANK = os.environ.get("MINARA_BANK", HERE + "/bank.jsonl")
|
|
HDRS = {
|
|
"Content-Type": "application/json",
|
|
"Origin": "https://minara.ai",
|
|
"Referer": "https://minara.ai/",
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36",
|
|
}
|
|
|
|
def new_session():
|
|
p = random.choice(PROXIES)
|
|
if os.environ.get("MINARA_IMPERSONATE"):
|
|
from curl_cffi import requests as cffi
|
|
s = cffi.Session(impersonate=os.environ.get("MINARA_IMPERSONATE") or "chrome")
|
|
if p:
|
|
s.proxies = {"http": p, "https": p}
|
|
s.headers.update(HDRS)
|
|
return s
|
|
s = requests.Session()
|
|
s.proxies = {"http": p, "https": p}
|
|
s.headers.update(HDRS)
|
|
return s
|
|
|
|
def imap_conn(dom, user, pw):
|
|
host, port = IMAPMAP.get(dom, ("imap." + dom, 993))
|
|
port = int(port)
|
|
c = imaplib.IMAP4_SSL(host, port, timeout=25) if port == 993 else imaplib.IMAP4(host, port, timeout=25)
|
|
c.login(user, pw)
|
|
c.select("INBOX")
|
|
return c
|
|
|
|
def max_uid(c):
|
|
t, d = c.uid("SEARCH", None, "ALL")
|
|
ids = d[0].split() if d and d[0] else []
|
|
return int(ids[-1]) if ids else 0
|
|
|
|
def find_code(c, since_uid):
|
|
"""Быстрый поиск: только письма от minara.ai новее since_uid (без полного скана ящика)."""
|
|
d_since = time.strftime("%d-%b-%Y", time.localtime(time.time() - 86400))
|
|
try:
|
|
t, d = c.uid("SEARCH", None, "ALL")
|
|
raw = d[0].split() if d and d[0] else []
|
|
except Exception:
|
|
raise
|
|
uids = sorted(int(u) for u in raw if int(u) > since_uid)[-3:]
|
|
if not uids:
|
|
return None
|
|
for u in reversed(uids):
|
|
t, m = c.uid("FETCH", str(u).encode(), "(BODY.PEEK[])")
|
|
if not m or not m[0]:
|
|
continue
|
|
msg = emaillib.message_from_bytes(m[0][1])
|
|
if "minara.ai" not in (msg.get("From") or "").lower():
|
|
continue
|
|
body = ""
|
|
for p in msg.walk():
|
|
if p.get_content_type() in ("text/plain", "text/html"):
|
|
body += (p.get_payload(decode=True) or b"").decode("utf8", "ignore")
|
|
m1 = re.search(r"font-size:\s*48px[\s\S]{0,500}?>\s*([A-Za-z0-9]{6})\s*<", body)
|
|
if m1:
|
|
return m1.group(1)
|
|
dc = re.findall(r"(?<!\d)(\d{6})(?!\d)", re.sub(r"<[^>]+>", " ", body))
|
|
if dc:
|
|
return dc[0]
|
|
return None
|
|
|
|
def pop_conn(dom, user, pw):
|
|
host = POP_HOSTS.get(dom) or ("pop." + dom, 995)
|
|
c = poplib.POP3_SSL(host[0], host[1], timeout=25)
|
|
c.user(user)
|
|
c.pass_(pw)
|
|
return c
|
|
|
|
|
|
def pop_find_code(dom, user, pw, since_ts, timeout=240):
|
|
"""POP3 fallback (rediffmail и подобные): свежее письмо от minara.ai."""
|
|
end = time.time() + timeout
|
|
while time.time() < end:
|
|
try:
|
|
c = pop_conn(dom, user, pw)
|
|
n = len(c.list()[1])
|
|
for i in range(n, max(1, n - 12), -1):
|
|
try:
|
|
resp, lines, _ = c.retr(i)
|
|
except Exception:
|
|
continue
|
|
raw = b"\n".join(lines)
|
|
m = emaillib.message_from_bytes(raw)
|
|
if "minara.ai" not in (m.get("From") or "").lower():
|
|
continue
|
|
body = ""
|
|
for p in m.walk():
|
|
if p.get_content_type() in ("text/plain", "text/html"):
|
|
body += (p.get_payload(decode=True) or b"").decode("utf8", "ignore")
|
|
m1 = re.search(r"font-size:\s*48px[\s\S]{0,500}?>\s*([A-Za-z0-9]{6})\s*<", body)
|
|
code = m1.group(1) if m1 else None
|
|
if not code:
|
|
dc = re.findall(r"(?<!\d)(\d{6})(?!\d)", re.sub(r"<[^>]+>", " ", body))
|
|
code = dc[0] if dc else None
|
|
if code:
|
|
try:
|
|
c.quit()
|
|
except Exception:
|
|
pass
|
|
return code
|
|
try:
|
|
c.quit()
|
|
except Exception:
|
|
pass
|
|
except Exception:
|
|
pass
|
|
time.sleep(8)
|
|
return None
|
|
|
|
|
|
def register(em, pw, dom, invite=INVITE, timeout=None):
|
|
if timeout is None:
|
|
timeout = int(os.environ.get("MINARA_MAIL_TIMEOUT", "240"))
|
|
s = new_session()
|
|
use_pop = False
|
|
try:
|
|
c = imap_conn(dom, em, pw)
|
|
base = max_uid(c)
|
|
except Exception as e:
|
|
c = None
|
|
use_pop = True
|
|
base = 0
|
|
print(" imap-fallback->pop", em, str(e)[:60], flush=True)
|
|
tok = solve_captcha()
|
|
if not tok:
|
|
return {"email": em, "ok": False, "stage": "captcha"}
|
|
r = s.post(API + "/auth/email/code", json={"email": em, "captchaToken": tok}, timeout=40)
|
|
if '"success":true' not in r.text:
|
|
return {"email": em, "ok": False, "stage": "send", "http": r.status_code, "resp": r.text[:200]}
|
|
code = None
|
|
end = time.time() + timeout
|
|
while time.time() < end:
|
|
try:
|
|
if use_pop:
|
|
code = pop_find_code(dom, em, pw, time.time(), timeout=30)
|
|
else:
|
|
try:
|
|
c.noop()
|
|
except Exception:
|
|
c = imap_conn(dom, em, pw)
|
|
c.select("INBOX")
|
|
code = find_code(c, base)
|
|
except Exception:
|
|
try:
|
|
c = imap_conn(dom, em, pw)
|
|
c.select("INBOX")
|
|
except Exception:
|
|
pass
|
|
if code:
|
|
break
|
|
time.sleep(6)
|
|
try:
|
|
if c:
|
|
c.logout()
|
|
except Exception:
|
|
pass
|
|
if not code:
|
|
return {"email": em, "ok": False, "stage": "mail"}
|
|
body = {"email": em, "code": code, "channel": None,
|
|
"deviceId": "".join(random.choices("0123456789abcdef", k=32)), "deviceType": "web"}
|
|
if invite and invite.lower() != "none":
|
|
body["inviteCode"] = invite
|
|
r = s.post(API + "/auth/email/verify", json=body, timeout=40)
|
|
try:
|
|
j = json.loads(r.text)
|
|
except Exception:
|
|
return {"email": em, "ok": False, "stage": "verify", "http": r.status_code, "resp": r.text[:200]}
|
|
at = j.get("access_token")
|
|
if not at:
|
|
return {"email": em, "ok": False, "stage": "verify", "code_used": code, "http": r.status_code, "resp": j}
|
|
me = s.get(API + "/auth/me", headers={"Authorization": "Bearer " + at}, timeout=40)
|
|
rec = {"email": em, "pw": pw, "dom": dom, "ok": True, "token": at, "me_http": me.status_code,
|
|
"me": me.text[:2000], "ts": int(time.time())}
|
|
with open(BANK, "a", encoding="utf8") as fh:
|
|
fh.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
|
return rec
|
|
|
|
if __name__ == "__main__":
|
|
em, pw = sys.argv[1], sys.argv[2]
|
|
inv = sys.argv[3] if len(sys.argv) > 3 else INVITE
|
|
print(json.dumps(register(em, pw, em.split("@")[-1].lower(), inv), ensure_ascii=False)[:1500]) |