ctxdev autoreg v2: t-online flow, grant banner form, VPS IMAP read, proxy pool loop, dash login
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
import json, os, random, re, subprocess, sys, time
|
||||
import requests
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
BASE = os.path.dirname(os.path.abspath(__file__))
|
||||
POOL = os.environ.get("CTX_MAIL_POOL", os.path.join(BASE, "mail_pool.txt"))
|
||||
USED = os.path.join(BASE, "t_used.json")
|
||||
PORT = os.environ.get("CTX_CDP_PORT", "9311")
|
||||
CDP = "http://127.0.0.1:%s" % PORT
|
||||
PROF = os.environ.get("CTX_CHROME_PROF", BASE + r"\chrome_z1")
|
||||
PROXY = os.environ.get("CTX_CHROME_PROXY", "")
|
||||
IDPXY = {"http": os.environ.get("CTX_ID_PROXY", "http://127.0.0.1:8899"), "https": os.environ.get("CTX_ID_PROXY", "http://127.0.0.1:8899")}
|
||||
VPS = os.environ.get("CTX_VPS", "")
|
||||
IMAP_HOST = os.environ.get("CTX_IMAP_HOST", "secureimap.t-online.de")
|
||||
|
||||
VPS_SCRIPT = r'''
|
||||
import imaplib, email, re, sys
|
||||
addr, pw = sys.argv[1], sys.argv[2]
|
||||
host = sys.argv[3] if len(sys.argv) > 3 else "secureimap.t-online.de"
|
||||
m = imaplib.IMAP4_SSL(host, 993)
|
||||
m.login(addr, pw)
|
||||
m.select("INBOX")
|
||||
typ, data = m.search(None, "FROM", "context.dev")
|
||||
ids = data[0].split()
|
||||
for i in reversed(ids[-3:]):
|
||||
typ, d = m.fetch(i, "(RFC822)")
|
||||
msg = email.message_from_bytes(d[0][1])
|
||||
body = ""
|
||||
if msg.is_multipart():
|
||||
for pt in msg.walk():
|
||||
if pt.get_content_type() in ("text/html", "text/plain"):
|
||||
try: body += pt.get_payload(decode=True).decode("utf-8", "replace")
|
||||
except Exception: pass
|
||||
else:
|
||||
body = msg.get_payload(decode=True).decode("utf-8", "replace")
|
||||
ls = re.findall(r"https://www\.context\.dev/verify-email[^\s\"'<>]+", body)
|
||||
if ls:
|
||||
print(ls[0].replace("&", "&")); sys.exit(0)
|
||||
print("NONE")
|
||||
'''
|
||||
|
||||
def log(*a, **kw):
|
||||
kw.setdefault("flush", True)
|
||||
print(*a, **kw)
|
||||
|
||||
def txt(pg, n=300):
|
||||
try: return pg.inner_text("body")[:n]
|
||||
except Exception: return ""
|
||||
|
||||
def click(pg, rx):
|
||||
for kind in ("button", "link"):
|
||||
try:
|
||||
el = pg.get_by_role(kind, name=re.compile(rx, re.I)).first
|
||||
if el.count() and el.is_visible():
|
||||
el.click(); return True
|
||||
except Exception: pass
|
||||
return False
|
||||
|
||||
def consent(pg):
|
||||
if "cookie" in txt(pg, 200).lower():
|
||||
click(pg, "^accept"); time.sleep(2)
|
||||
|
||||
def vps_link(addr, pw, timeout=200):
|
||||
t0 = time.time()
|
||||
last = ""
|
||||
while time.time() - t0 < timeout:
|
||||
try:
|
||||
r = subprocess.run(["ssh", "-o", "StrictHostKeyChecking=no", "-o", "ConnectTimeout=15", VPS,
|
||||
"python3 - " + addr + " " + pw + " " + IMAP_HOST],
|
||||
input=VPS_SCRIPT.encode(), capture_output=True, timeout=90)
|
||||
out = r.stdout.decode("utf-8", "replace").strip()
|
||||
last = out[:80]
|
||||
if out.startswith("https://"):
|
||||
return out.splitlines()[0]
|
||||
except Exception as e:
|
||||
last = "err " + type(e).__name__
|
||||
log(" mail wait:", last)
|
||||
time.sleep(20)
|
||||
return None
|
||||
|
||||
def identity(mail):
|
||||
for att in range(3):
|
||||
for px in (IDPXY, {"http": os.environ.get("CTX_ID_PROXY2", "http://127.0.0.1:8899"), "https": os.environ.get("CTX_ID_PROXY2", "http://127.0.0.1:8899")}):
|
||||
try:
|
||||
r = requests.post("https://www.context.dev/agent/identity",
|
||||
json={"type": "service_auth", "login_hint": mail,
|
||||
"api_key_name": "hermes-z", "client_name": "Hermes"},
|
||||
timeout=45, proxies=px)
|
||||
if r.status_code == 200:
|
||||
return r.json()
|
||||
log(" identity", r.status_code, r.text[:60])
|
||||
except Exception as e:
|
||||
log(" identity err", type(e).__name__)
|
||||
time.sleep(20)
|
||||
return None
|
||||
|
||||
# pick unused mail
|
||||
used = set(json.load(open(USED))) if os.path.exists(USED) else set()
|
||||
MAIL = MPW = None
|
||||
for line in open(POOL, encoding="utf-8", errors="replace"):
|
||||
line = line.strip()
|
||||
if not line or ":" not in line or line.startswith("#"): continue
|
||||
a, p = line.split(":", 1)
|
||||
if a in used: continue
|
||||
MAIL, MPW = a, p
|
||||
break
|
||||
log("account", MAIL[:4] + "***@" + MAIL.split("@")[-1], "| proxy", PROXY)
|
||||
|
||||
ident = identity(MAIL)
|
||||
if not ident:
|
||||
log("NO IDENTITY"); sys.exit(1)
|
||||
vuri, ucode, ctok = ident["claim"]["verification_uri"], ident["claim"]["user_code"], ident["claim_token"]
|
||||
PW = "Zx9" + "".join(random.choice("abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789") for _ in range(11)) + "!7"
|
||||
rec = {"email": MAIL, "pw": PW, "mailpw": MPW, "claim_token": ctok, "vuri": vuri, "user_code": ucode}
|
||||
af = os.path.join(BASE, "z_acct_%s.json" % re.sub(r"\W", "_", MAIL))
|
||||
json.dump(rec, open(af, "w"), indent=1)
|
||||
log("identity ok | code", ucode)
|
||||
|
||||
env2 = dict(os.environ); env2.update({"CTX_CDP_PORT": PORT, "CTX_CHROME_PROF": PROF, "CTX_CHROME_PROXY": PROXY})
|
||||
subprocess.run([sys.executable, os.path.join(BASE, "chrome_up.py")], check=False, env=env2)
|
||||
time.sleep(3)
|
||||
|
||||
ok = False
|
||||
with sync_playwright() as p:
|
||||
b = p.chromium.connect_over_cdp(CDP, timeout=90000)
|
||||
ctx = b.contexts[0]
|
||||
pg = ctx.new_page(); pg.set_default_timeout(50000)
|
||||
pg.goto(vuri, timeout=90000, wait_until="domcontentloaded")
|
||||
time.sleep(8); consent(pg)
|
||||
t = txt(pg, 240)
|
||||
log("page:", t[:100].replace("\n", " "))
|
||||
if "extra credits" in t or "Finish setup" in t:
|
||||
log(" gift banner present")
|
||||
has_form = pg.locator("#password").count() > 0
|
||||
if has_form:
|
||||
em = pg.locator("input[type=email]").first
|
||||
try: em.evaluate("e=>{e.readOnly=false}")
|
||||
except Exception: pass
|
||||
try: em.fill(MAIL)
|
||||
except Exception: pg.keyboard.type(MAIL, delay=35)
|
||||
nm = pg.locator("#name")
|
||||
if nm.count(): nm.first.fill(random.choice(["Nikita Refor", "Alex Grobman", "Max Koval"]))
|
||||
pg.locator("#password").first.fill(PW)
|
||||
try: pg.locator("input[type=checkbox]").first.click()
|
||||
except Exception: pass
|
||||
time.sleep(2)
|
||||
try: pg.locator("button[type=submit]").first.click(force=True)
|
||||
except Exception: pass
|
||||
for _ in range(25):
|
||||
time.sleep(4)
|
||||
t = txt(pg, 260)
|
||||
if "Confirm agent setup" in t or "Check your inbox" in t: break
|
||||
if "blocked" in t or "Security check" in t or "flagged" in t: break
|
||||
log("submit:", t[:110].replace("\n", " "))
|
||||
if "Confirm agent setup" in txt(pg, 300):
|
||||
ins = pg.locator("input[type=text], input[inputmode=numeric], input[autocomplete=one-time-code]")
|
||||
if ins.count() >= 6:
|
||||
for i in range(6): ins.nth(i).fill(ucode[i])
|
||||
elif ins.count():
|
||||
ins.first.fill(ucode)
|
||||
time.sleep(1)
|
||||
click(pg, "confirm setup|^confirm$|authorize|continue")
|
||||
time.sleep(9)
|
||||
log("after code:", txt(pg, 200).replace("\n", " "), "|", pg.url[:70])
|
||||
if "Check your inbox" in txt(pg, 320):
|
||||
link = vps_link(MAIL, MPW)
|
||||
log("mail:", (link or "NONE")[:80])
|
||||
if link:
|
||||
pg.goto(link, timeout=90000, wait_until="domcontentloaded"); time.sleep(7)
|
||||
log("verify page:", txt(pg, 120).replace("\n", " "))
|
||||
click(pg, "verify email address|verify email"); time.sleep(10)
|
||||
log("after verify:", txt(pg, 140).replace("\n", " "))
|
||||
pg.goto(vuri, timeout=90000, wait_until="domcontentloaded"); time.sleep(8)
|
||||
t2 = txt(pg, 260); log("back:", t2[:110].replace("\n", " "))
|
||||
if "verified my email" in t2:
|
||||
click(pg, "verified my email"); time.sleep(9)
|
||||
t2 = txt(pg, 200); log("after ack:", t2[:110].replace("\n", " "))
|
||||
ins = pg.locator("input[type=text], input[inputmode=numeric], input[autocomplete=one-time-code]")
|
||||
if ins.count() >= 6:
|
||||
for i in range(6): ins.nth(i).fill(ucode[i])
|
||||
time.sleep(1)
|
||||
click(pg, "confirm setup|^confirm$|authorize|continue"); time.sleep(10)
|
||||
log("final page:", txt(pg, 200).replace("\n", " "), "|", pg.url[:80])
|
||||
if "blocked" not in txt(pg, 320):
|
||||
ok = True
|
||||
pg.screenshot(path=os.path.join(BASE, "z_final.png"))
|
||||
try:
|
||||
ks = pg.evaluate("() => (document.body.innerText.match(/ctxt_secret_[A-Za-z0-9]+/g) || null)")
|
||||
log("keys on page:", ks)
|
||||
full = pg.inner_text("body")
|
||||
open(os.path.join(BASE, "z_page_%s.txt" % re.sub(r"\W", "_", MAIL)), "w", encoding="utf-8").write(
|
||||
pg.url + "\n\n" + full)
|
||||
except Exception as e:
|
||||
log("dump err", type(e).__name__)
|
||||
pg.close()
|
||||
|
||||
used.add(MAIL); json.dump(sorted(used), open(USED, "w"))
|
||||
j = {}
|
||||
for att in range(4):
|
||||
px = IDPXY if att % 2 == 0 else {"http": os.environ.get("CTX_ID_PROXY2", "http://127.0.0.1:8899"), "https": os.environ.get("CTX_ID_PROXY2", "http://127.0.0.1:8899")}
|
||||
try:
|
||||
r = requests.post("https://www.context.dev/oauth2/token",
|
||||
data={"grant_type": "urn:workos:agent-auth:grant-type:claim", "claim_token": ctok},
|
||||
timeout=60, proxies=px)
|
||||
log("token:", r.status_code, r.text[:180])
|
||||
if r.text.strip().startswith("{"):
|
||||
j = r.json()
|
||||
if "access_token" in j or "error" in j:
|
||||
break
|
||||
except Exception as e:
|
||||
log("token err", type(e).__name__)
|
||||
time.sleep(5)
|
||||
if "access_token" in j:
|
||||
k = j["access_token"]
|
||||
log("KEY len", len(k), "gift", j.get("signup_gift_credits"))
|
||||
u = requests.get("https://api.context.dev/v1/org/logs", headers={"Authorization": "Bearer " + k},
|
||||
timeout=30, proxies=IDPXY)
|
||||
log("logs:", u.status_code, u.text[:300])
|
||||
json.dump({**rec, "key": k}, open(af, "w"), indent=1)
|
||||
with open(os.path.join(BASE, "bank.jsonl"), "a") as f:
|
||||
f.write(json.dumps({"email": MAIL, "tag": "t-online", "key": k, "ts": time.time(),
|
||||
"gift": j.get("signup_gift_credits"), "credits": None, "dead": False}) + "\n")
|
||||
log("SAVED")
|
||||
Reference in New Issue
Block a user