recovered from aaronjaworski710
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
# typesafe provider — copy env, do not commit secrets
|
||||
#
|
||||
# Education / personal automation only. Default COUNT=1.
|
||||
# Do not copy the original farm defaults (ACCOUNT_COUNT=512 / CONCURRENCY=256).
|
||||
|
||||
# TYPESAFE_EMAIL_SOURCE=tinyhost
|
||||
# TYPESAFE_OTP_TIMEOUT=180
|
||||
# TYPESAFE_API_KEY_NAME=register-core
|
||||
|
||||
# Optional fixed URL (mainly with REGISTER_EGRESS=clash or direct override)
|
||||
# TYPESAFE_PROXY=http://127.0.0.1:7897
|
||||
|
||||
# HTTP/SOCKS pool (REGISTER_EGRESS=list|auto)
|
||||
# TYPESAFE_PROXY_LIST=http://user:pass@1.2.3.4:8080
|
||||
# REGISTER_NODES_FILE=./nodes.json
|
||||
@@ -0,0 +1,80 @@
|
||||
# typesafe.ai / jev console provider
|
||||
|
||||
In-process protocol register for **typesafe.ai console** accounts (Stytch
|
||||
magic-link + `/api/auth/callback` + `/api/api-keys`). Produces a this-run
|
||||
`api_key`. Ported from [Futureppo/typesafe_register](https://github.com/Futureppo/typesafe_register)
|
||||
(Apache-2.0) — **not** a drop-in of that script's 512-account / 256-worker farm.
|
||||
|
||||
## Stack
|
||||
|
||||
| Layer | Choice |
|
||||
|-------|--------|
|
||||
| Style | **In-process** (consumes `EmailSource`) |
|
||||
| HTTP | `requests` + ECDH `prime256v1` TLS pin |
|
||||
| Mail | `tinyhost` default (full body required for magic-link) |
|
||||
| Auth | Stytch magic-link, no browser / Turnstile |
|
||||
| CPA | **none** — never auto-inject production |
|
||||
|
||||
## Hub
|
||||
|
||||
```bash
|
||||
./register.sh typesafe [count]
|
||||
./register.sh core run -p typesafe -n 1 --email-source tinyhost
|
||||
# aliases: jev | typesafe-ai
|
||||
```
|
||||
|
||||
`TYPESAFE_LEGACY=1` falls back to `providers/typesafe/run-register.sh`.
|
||||
|
||||
```bash
|
||||
./register.sh smoke typesafe # tinyhost + console probe, no mailbox
|
||||
TYPESAFE_SMOKE_LIVE=1 ./register.sh smoke typesafe # n=1 this-run api_key
|
||||
```
|
||||
|
||||
## Env
|
||||
|
||||
| Var | Default | Meaning |
|
||||
|-----|---------|---------|
|
||||
| `TYPESAFE_EMAIL_SOURCE` | `tinyhost` | Must return full mail body (not OTP-only) |
|
||||
| `TYPESAFE_EMAIL_DOMAIN` | empty | Optional tinyhost domain pin |
|
||||
| `TYPESAFE_OTP_TIMEOUT` | `180` | Magic-link poll window (seconds) |
|
||||
| `TYPESAFE_API_KEY_NAME` | `register-core` | Console key label |
|
||||
| `TYPESAFE_PROXY` | empty | Fixed register egress |
|
||||
| `TYPESAFE_SKIP_CONSOLE_PROBE` | empty | Skip login GET+parse before mailbox allocate |
|
||||
| `TYPESAFE_SMOKE_LIVE` | `0` | `1` = n=1 live register from `./register.sh smoke typesafe` |
|
||||
| `REGISTER_EGRESS` | `auto` | `list` \| `core` \| `clash` \| `direct` \| `auto` |
|
||||
|
||||
Do **not** set `ACCOUNT_COUNT=512` / `CONCURRENCY=256`. Pipeline `count` /
|
||||
threads own scale. Default is `1`.
|
||||
|
||||
## Artifacts
|
||||
|
||||
- `providers/typesafe/output/` (0700)
|
||||
- `providers/typesafe/output/typesafe-<email>-<ts>.json` (0600) — this-run deliverable
|
||||
- Pipeline sink: `providers/typesafe/output/pipeline.jsonl`
|
||||
|
||||
Success = this-run `secret` / `secret_kind=api_key` plus a new 0600 json.
|
||||
Do **not** append full keys to `accounts.jsonl`; historical tails do not count.
|
||||
|
||||
## Mail
|
||||
|
||||
Cloudflare Worker / Gmail IMAP wrappers in this repo only extract 6-digit
|
||||
OTPs. typesafe login mail carries a Stytch URL, so the default source is
|
||||
tinyhost (subject + body + html). `sender_hint=typesafe`.
|
||||
|
||||
## Latest case (pxed, 2026-09-22)
|
||||
|
||||
`./register.sh typesafe 1000`, 8 workers, `--no-fail-fast`, egress
|
||||
`rotate=off` (fixed Clash mixed-port, this run did **not** rotate IP).
|
||||
|
||||
`CONTRACT_EXIT:0`, ok=976, fail=24 (`mail_miss`×17 no-mail timeouts,
|
||||
`session`×5 HTTP 500 on send, `oauth_callback`×2 HTTP 401). 976 new `0600`
|
||||
json files, dir `0700`, no `accounts.jsonl` append, no CPA inject.
|
||||
|
||||
Same day, smaller runs: n=100 → 97/3 `mail_miss`; after the magic-link parse
|
||||
fix, n=100 → 98/2 HTTP 500.
|
||||
|
||||
## Compliance
|
||||
|
||||
Education / personal automation only. May violate third-party ToS. No
|
||||
mass-farm defaults, no plus-alias, no CPA inject. This project recognizes
|
||||
the [LINUX DO](https://linux.do/) community.
|
||||
@@ -0,0 +1 @@
|
||||
# typesafe.ai / jev product package (in-process protocol).
|
||||
@@ -0,0 +1,5 @@
|
||||
"""typesafe.ai / jev console magic-link registration (in-process)."""
|
||||
|
||||
from .flow import TypesafeRegisterError, TypesafeResult, probe_console, register_one
|
||||
|
||||
__all__ = ["TypesafeRegisterError", "TypesafeResult", "probe_console", "register_one"]
|
||||
@@ -0,0 +1,24 @@
|
||||
"""typesafe.ai console constants (Stytch magic-link + Next.js Server Action)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
CONSOLE_BASE_URL = "https://console.typesafe.ai"
|
||||
LOGIN_PAGE_URL = f"{CONSOLE_BASE_URL}/login"
|
||||
# Fallback Next.js deployment id from the original public login snapshot.
|
||||
# Live HTML is authoritative (pxed 2026-09-22: fb80dfc7f55f4bf7a496bc9579755816f26fae3c);
|
||||
# mismatch is logged; the page $ACTION blob is always preferred.
|
||||
CONSOLE_DEPLOYMENT_ID = "cc6f6dca06537cc04123caaaf50ca5a76d506a92"
|
||||
|
||||
DEFAULT_API_KEY_NAME = "register-core"
|
||||
DEFAULT_TIMEOUT = 30
|
||||
TLS_ECDH_CURVE = "prime256v1"
|
||||
|
||||
USER_AGENT = (
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
||||
"(KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0"
|
||||
)
|
||||
|
||||
COMMON_HEADERS: dict[str, str] = {
|
||||
"User-Agent": USER_AGENT,
|
||||
"accept-language": "zh-CN,zh;q=0.9,en;q=0.7",
|
||||
}
|
||||
@@ -0,0 +1,564 @@
|
||||
"""typesafe.ai Stytch magic-link + console API-key protocol.
|
||||
|
||||
Ported from Futureppo/typesafe_register (Apache-2.0) as an in-process flow.
|
||||
Mailbox comes from register_core EmailSource — do not use the original
|
||||
512/256 farm defaults or TEMPMAIL placeholders.
|
||||
|
||||
Success = this-run API key. Never treat historical accounts.json as ok.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import html
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
|
||||
from register_core.decode.extract import extract_typesafe_magic_link
|
||||
from register_core.errors import MailMissError
|
||||
|
||||
from .constants import (
|
||||
CONSOLE_BASE_URL,
|
||||
CONSOLE_DEPLOYMENT_ID,
|
||||
DEFAULT_API_KEY_NAME,
|
||||
DEFAULT_TIMEOUT,
|
||||
LOGIN_PAGE_URL,
|
||||
)
|
||||
|
||||
LogFn = Callable[[str], None]
|
||||
_deployment_drift_logged = False
|
||||
|
||||
|
||||
class TypesafeRegisterError(RuntimeError):
|
||||
"""Single-attempt registration failure (caller maps to RegisterResult)."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
message: str,
|
||||
*,
|
||||
kind: str = "provider",
|
||||
step: str = "",
|
||||
steps: dict[str, Any] | None = None,
|
||||
) -> None:
|
||||
super().__init__(message)
|
||||
self.kind = kind
|
||||
self.step = step or ""
|
||||
self.steps: dict[str, Any] = dict(steps or {})
|
||||
|
||||
|
||||
@dataclass
|
||||
class TypesafeResult:
|
||||
ok: bool
|
||||
email: str = ""
|
||||
api_key: str = ""
|
||||
api_key_id: str = ""
|
||||
user_id: str = ""
|
||||
organization_id: str = ""
|
||||
organization_name: str = ""
|
||||
magic_link: str = ""
|
||||
error: str = ""
|
||||
error_kind: str = ""
|
||||
fail_step: str = ""
|
||||
steps: dict[str, Any] = field(default_factory=dict)
|
||||
deployment_id: str = ""
|
||||
deployment_mismatch: bool = False
|
||||
|
||||
def to_public_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"ok": self.ok,
|
||||
"email": self.email,
|
||||
"api_key": _preview(self.api_key),
|
||||
"api_key_id": self.api_key_id,
|
||||
"user_id": self.user_id,
|
||||
"organization_id": self.organization_id,
|
||||
"error": self.error,
|
||||
"error_kind": self.error_kind,
|
||||
"fail_step": self.fail_step,
|
||||
"step_keys": sorted(self.steps.keys()),
|
||||
"deployment_mismatch": self.deployment_mismatch,
|
||||
}
|
||||
|
||||
|
||||
def _preview(value: str) -> str:
|
||||
s = value or ""
|
||||
if not s:
|
||||
return ""
|
||||
if len(s) <= 8:
|
||||
return "***"
|
||||
return f"{s[:4]}…{s[-4:]}(len={len(s)})"
|
||||
|
||||
|
||||
def _action_snippet(page: str, limit: int = 400) -> str:
|
||||
"""Short $ACTION window for session failures — not the full login HTML."""
|
||||
raw = str(page or "")
|
||||
if not raw:
|
||||
return ""
|
||||
idx = raw.find("$ACTION_")
|
||||
if idx < 0:
|
||||
idx = raw.find('type="email"')
|
||||
if idx < 0:
|
||||
return raw[:limit]
|
||||
start = max(0, idx - 80)
|
||||
return raw[start : start + limit]
|
||||
|
||||
|
||||
def parse_login_action(page: str) -> dict[str, str]:
|
||||
"""Parse Next.js $ACTION_* blob + action id from the console login HTML."""
|
||||
raw = str(page or "")
|
||||
if not raw.strip():
|
||||
raise TypesafeRegisterError("login page empty", kind="session", step="login_action")
|
||||
pos = raw.find('type="email"')
|
||||
window = raw[:pos] if pos >= 0 else raw
|
||||
indexes = re.findall(r"\\?\$ACTION_(\d+):0", window)
|
||||
if not indexes:
|
||||
indexes = re.findall(r"\$ACTION_(\d+):0", window)
|
||||
if not indexes:
|
||||
raise TypesafeRegisterError(
|
||||
"login page missing $ACTION index",
|
||||
kind="session",
|
||||
step="login_action",
|
||||
steps={"login_action": {"ok": False, "html_snippet": _action_snippet(raw)}},
|
||||
)
|
||||
index = indexes[-1]
|
||||
ref_m = re.search(rf'\\?\$ACTION_{re.escape(index)}:0" value="([^"]+)"', raw)
|
||||
if not ref_m:
|
||||
ref_m = re.search(rf'\$ACTION_{re.escape(index)}:0" value="([^"]+)"', raw)
|
||||
if not ref_m:
|
||||
raise TypesafeRegisterError(
|
||||
"login page missing $ACTION ref",
|
||||
kind="session",
|
||||
step="login_action",
|
||||
steps={"login_action": {"ok": False, "html_snippet": _action_snippet(raw)}},
|
||||
)
|
||||
try:
|
||||
ref = json.loads(html.unescape(ref_m.group(1)))
|
||||
except Exception as exc:
|
||||
raise TypesafeRegisterError(
|
||||
f"login action ref json: {exc}",
|
||||
kind="session",
|
||||
step="login_action",
|
||||
) from exc
|
||||
action_id = str((ref or {}).get("id") or "").strip()
|
||||
if not action_id:
|
||||
raise TypesafeRegisterError(
|
||||
"login action id empty",
|
||||
kind="session",
|
||||
step="login_action",
|
||||
)
|
||||
blob_m = re.search(rf'\\?\$ACTION_{re.escape(index)}:2" value="([^"]+)"', raw)
|
||||
if not blob_m:
|
||||
blob_m = re.search(rf'\$ACTION_{re.escape(index)}:2" value="([^"]+)"', raw)
|
||||
if not blob_m:
|
||||
raise TypesafeRegisterError(
|
||||
"login page missing encrypted action blob",
|
||||
kind="session",
|
||||
step="login_action",
|
||||
steps={"login_action": {"ok": False, "html_snippet": _action_snippet(raw)}},
|
||||
)
|
||||
return {
|
||||
"action_id": action_id,
|
||||
"index": index,
|
||||
"blob": html.unescape(blob_m.group(1)),
|
||||
}
|
||||
|
||||
|
||||
def parse_deployment_id(page: str) -> str:
|
||||
m = re.search(r"dpl=([0-9a-f]{20,})", str(page or ""))
|
||||
return m.group(1) if m else ""
|
||||
|
||||
|
||||
def extract_magic_link(blob: str, subject: str = "") -> dict[str, str]:
|
||||
"""Return {public_token, token, magic_link} or empty dict."""
|
||||
hay = "\n".join([str(subject or ""), str(blob or "")])
|
||||
parsed = extract_typesafe_magic_link(hay)
|
||||
if not parsed:
|
||||
return {}
|
||||
return parsed
|
||||
|
||||
|
||||
def _usable_login_action(action: dict[str, Any] | None) -> dict[str, Any] | None:
|
||||
"""Return the action dict only when send_magic_link can POST it."""
|
||||
if not isinstance(action, dict):
|
||||
return None
|
||||
if not str(action.get("action_id") or "").strip():
|
||||
return None
|
||||
if not str(action.get("index") or "").strip():
|
||||
return None
|
||||
if not str(action.get("blob") or "").strip():
|
||||
return None
|
||||
return action
|
||||
|
||||
|
||||
def _classify_login_http(status: int, *, step: str, label: str) -> None:
|
||||
"""5xx / no status = network; 4xx = session. Do not parse error bodies as login HTML."""
|
||||
if status <= 0:
|
||||
raise TypesafeRegisterError(f"{label} no http status", kind="network", step=step)
|
||||
if status >= 500:
|
||||
raise TypesafeRegisterError(f"{label} HTTP {status}", kind="network", step=step)
|
||||
if status >= 400:
|
||||
raise TypesafeRegisterError(f"{label} HTTP {status}", kind="session", step=step)
|
||||
|
||||
|
||||
def _fetch_login_page_action(
|
||||
session: Any,
|
||||
*,
|
||||
log: LogFn | None = None,
|
||||
step: str = "login_action",
|
||||
label: str = "login GET",
|
||||
emit_status: bool = False,
|
||||
) -> tuple[dict[str, Any], int]:
|
||||
emit = log or (lambda _m: None)
|
||||
resp, err = _get(session, LOGIN_PAGE_URL, headers={"accept": "text/html,application/xhtml+xml"})
|
||||
if resp is None:
|
||||
raise TypesafeRegisterError(f"{label} failed: {err}", kind="network", step=step)
|
||||
status = int(getattr(resp, "status_code", 0) or 0)
|
||||
if emit_status:
|
||||
emit(f"{step} status={status}")
|
||||
_classify_login_http(status, step=step, label=label)
|
||||
page = getattr(resp, "text", "") or ""
|
||||
dpl = parse_deployment_id(page)
|
||||
if dpl and dpl != CONSOLE_DEPLOYMENT_ID:
|
||||
# Once per process. n=100 otherwise logs the same drift on every GET.
|
||||
global _deployment_drift_logged
|
||||
if not _deployment_drift_logged:
|
||||
_deployment_drift_logged = True
|
||||
emit(f"console deployment changed {CONSOLE_DEPLOYMENT_ID} -> {dpl}")
|
||||
action = parse_login_action(page)
|
||||
action["deployment_id"] = dpl
|
||||
action["deployment_mismatch"] = bool(dpl and dpl != CONSOLE_DEPLOYMENT_ID)
|
||||
return action, status
|
||||
|
||||
|
||||
def probe_console(
|
||||
session: Any | None = None,
|
||||
*,
|
||||
proxy: str = "",
|
||||
log: LogFn | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""GET + parse the login page *before* mailbox allocate.
|
||||
|
||||
Clash/list preflight is skipped for ``egress=clash``. A dead mixed port
|
||||
would otherwise burn a tinyhost address on the first login GET. HTTP 5xx
|
||||
is ``network``; 4xx is ``session``; parse failure is ``session``. The
|
||||
parsed ``action`` is returned so ``register_one`` can POST without a
|
||||
second login GET after allocate.
|
||||
"""
|
||||
from .session import create_session
|
||||
|
||||
sess = session if session is not None else create_session(proxy)
|
||||
action, status = _fetch_login_page_action(
|
||||
sess,
|
||||
log=log,
|
||||
step="console_probe",
|
||||
label="console probe",
|
||||
emit_status=True,
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"status": status,
|
||||
"index": action.get("index"),
|
||||
"deployment_id": action.get("deployment_id") or "",
|
||||
"url": LOGIN_PAGE_URL,
|
||||
"action": {
|
||||
"action_id": action["action_id"],
|
||||
"index": action["index"],
|
||||
"blob": action["blob"],
|
||||
"deployment_id": str(action.get("deployment_id") or ""),
|
||||
"deployment_mismatch": bool(action.get("deployment_mismatch")),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def login_action(session: Any, *, log: LogFn | None = None) -> dict[str, Any]:
|
||||
action, _status = _fetch_login_page_action(
|
||||
session,
|
||||
log=log,
|
||||
step="login_action",
|
||||
label="login GET",
|
||||
emit_status=False,
|
||||
)
|
||||
return action
|
||||
|
||||
|
||||
def send_magic_link(
|
||||
session: Any,
|
||||
email: str,
|
||||
*,
|
||||
action: dict[str, Any] | None = None,
|
||||
log: LogFn | None = None,
|
||||
) -> dict[str, str]:
|
||||
emit = log or (lambda _m: None)
|
||||
last_status = 0
|
||||
last_redirect = ""
|
||||
for refresh in (False, True):
|
||||
current = action if (action and not refresh) else login_action(session, log=emit)
|
||||
action = current
|
||||
boundary = "----WebKitFormBoundary" + uuid.uuid4().hex[:16]
|
||||
idx = current["index"]
|
||||
body = (
|
||||
f"--{boundary}\r\nContent-Disposition: form-data; name=\"1\"\r\n\r\n{current['blob']}\r\n"
|
||||
f"--{boundary}\r\nContent-Disposition: form-data; name=\"_{idx}_email\"\r\n\r\n{email}\r\n"
|
||||
f"--{boundary}\r\nContent-Disposition: form-data; name=\"0\"\r\n\r\n[\"$@1\",\"$K{idx}\"]\r\n"
|
||||
f"--{boundary}--\r\n"
|
||||
).encode()
|
||||
resp, err = _post(
|
||||
session,
|
||||
LOGIN_PAGE_URL,
|
||||
data=body,
|
||||
headers={
|
||||
"content-type": f"multipart/form-data; boundary={boundary}",
|
||||
"next-action": current["action_id"],
|
||||
"accept": "text/x-component",
|
||||
"origin": CONSOLE_BASE_URL,
|
||||
"referer": LOGIN_PAGE_URL,
|
||||
"sec-fetch-site": "same-origin",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-dest": "empty",
|
||||
},
|
||||
)
|
||||
if resp is None:
|
||||
raise TypesafeRegisterError(
|
||||
f"send magic link failed: {err}",
|
||||
kind="network",
|
||||
step="send_magic_link",
|
||||
)
|
||||
last_status = int(getattr(resp, "status_code", 0) or 0)
|
||||
last_redirect = str(resp.headers.get("x-action-redirect", "") or "")
|
||||
if last_status == 200 and "sent=true" in last_redirect:
|
||||
return {
|
||||
"action_id": current["action_id"],
|
||||
"form_index": idx,
|
||||
"x_action_redirect": last_redirect,
|
||||
"deployment_id": str(current.get("deployment_id") or ""),
|
||||
"deployment_mismatch": bool(current.get("deployment_mismatch")),
|
||||
}
|
||||
emit(f"send_magic_link retry refresh={refresh} status={last_status}")
|
||||
raise TypesafeRegisterError(
|
||||
f"send magic link failed: HTTP {last_status} redirect={last_redirect!r}",
|
||||
kind="session",
|
||||
step="send_magic_link",
|
||||
)
|
||||
|
||||
|
||||
def auth_callback(session: Any, token: str) -> dict[str, Any]:
|
||||
token = (token or "").strip()
|
||||
if not token:
|
||||
raise TypesafeRegisterError("empty magic-link token", kind="oauth_callback", step="callback")
|
||||
resp, err = _post(
|
||||
session,
|
||||
f"{CONSOLE_BASE_URL}/api/auth/callback",
|
||||
json={"token": token, "tokenType": "magic_links", "preferredOrgId": None},
|
||||
headers={
|
||||
"content-type": "application/json",
|
||||
"accept": "*/*",
|
||||
"origin": CONSOLE_BASE_URL,
|
||||
"referer": f"{CONSOLE_BASE_URL}/auth/callback?stytch_token_type=magic_links",
|
||||
"sec-fetch-site": "same-origin",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-dest": "empty",
|
||||
},
|
||||
)
|
||||
if resp is None:
|
||||
raise TypesafeRegisterError(f"auth callback failed: {err}", kind="network", step="callback")
|
||||
status = int(getattr(resp, "status_code", 0) or 0)
|
||||
if status >= 400:
|
||||
raise TypesafeRegisterError(
|
||||
f"auth callback HTTP {status}",
|
||||
kind="oauth_callback",
|
||||
step="callback",
|
||||
)
|
||||
from .session import response_json
|
||||
|
||||
data = response_json(resp)
|
||||
if not data:
|
||||
raise TypesafeRegisterError("auth callback empty json", kind="oauth_callback", step="callback")
|
||||
return data
|
||||
|
||||
|
||||
def create_api_key(session: Any, name: str | None = None) -> dict[str, Any]:
|
||||
key_name = (name or os.environ.get("TYPESAFE_API_KEY_NAME") or DEFAULT_API_KEY_NAME).strip()
|
||||
resp, err = _post(
|
||||
session,
|
||||
f"{CONSOLE_BASE_URL}/api/api-keys",
|
||||
json={"name": key_name},
|
||||
headers={
|
||||
"content-type": "application/json",
|
||||
"accept": "*/*",
|
||||
"origin": CONSOLE_BASE_URL,
|
||||
"referer": f"{CONSOLE_BASE_URL}/keys",
|
||||
"sec-fetch-site": "same-origin",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-dest": "empty",
|
||||
},
|
||||
)
|
||||
if resp is None:
|
||||
raise TypesafeRegisterError(f"create api key failed: {err}", kind="network", step="api_key")
|
||||
status = int(getattr(resp, "status_code", 0) or 0)
|
||||
if status >= 400:
|
||||
raise TypesafeRegisterError(
|
||||
f"create api key HTTP {status}",
|
||||
kind="token",
|
||||
step="api_key",
|
||||
)
|
||||
from .session import response_json
|
||||
|
||||
data = response_json(resp)
|
||||
if not str(data.get("api_key") or "").strip():
|
||||
raise TypesafeRegisterError(
|
||||
"create api key missing api_key field",
|
||||
kind="token",
|
||||
step="api_key",
|
||||
)
|
||||
return data
|
||||
|
||||
|
||||
def save_result(result: TypesafeResult, path: Path) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
try:
|
||||
path.parent.chmod(0o700)
|
||||
except Exception:
|
||||
pass
|
||||
payload = {
|
||||
"ok": result.ok,
|
||||
"email": result.email,
|
||||
"api_key": result.api_key,
|
||||
"api_key_id": result.api_key_id,
|
||||
"user_id": result.user_id,
|
||||
"organization_id": result.organization_id,
|
||||
"organization_name": result.organization_name,
|
||||
"provider": "typesafe",
|
||||
}
|
||||
path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
||||
try:
|
||||
path.chmod(0o600)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def register_one(
|
||||
*,
|
||||
email: str,
|
||||
proxy: str = "",
|
||||
magic_link_provider: Callable[[], str],
|
||||
log: LogFn | None = None,
|
||||
api_key_name: str | None = None,
|
||||
session: Any | None = None,
|
||||
action: dict[str, Any] | None = None,
|
||||
) -> TypesafeResult:
|
||||
"""Run login → send link → wait mail → callback → mint API key.
|
||||
|
||||
``action`` is the console-probe parse (same session). When usable, skip
|
||||
the post-allocate login GET so a transient 5xx cannot burn the mailbox.
|
||||
``send_magic_link`` still re-GETs on POST failure.
|
||||
"""
|
||||
from .session import create_session
|
||||
|
||||
emit = log or (lambda _m: None)
|
||||
steps: dict[str, Any] = {}
|
||||
sess = session if session is not None else create_session(proxy)
|
||||
result = TypesafeResult(ok=False, email=email)
|
||||
|
||||
try:
|
||||
emit(f"login_action email={email}")
|
||||
reused = _usable_login_action(action)
|
||||
if reused is not None:
|
||||
action = reused
|
||||
emit("login_action reused from console_probe")
|
||||
else:
|
||||
action = login_action(sess, log=emit)
|
||||
result.deployment_id = str(action.get("deployment_id") or "")
|
||||
result.deployment_mismatch = bool(action.get("deployment_mismatch"))
|
||||
steps["login_action"] = {
|
||||
"ok": True,
|
||||
"index": action.get("index"),
|
||||
"deployment_mismatch": result.deployment_mismatch,
|
||||
"reused_probe": reused is not None,
|
||||
}
|
||||
|
||||
emit("send_magic_link")
|
||||
sent = send_magic_link(sess, email, action=action, log=emit)
|
||||
steps["send_magic_link"] = {
|
||||
"ok": True,
|
||||
"redirect": sent.get("x_action_redirect", ""),
|
||||
}
|
||||
|
||||
emit("wait_magic_link")
|
||||
raw_link = str(magic_link_provider() or "").strip()
|
||||
parsed = extract_magic_link(raw_link)
|
||||
if not parsed:
|
||||
raise TypesafeRegisterError(
|
||||
"magic link missing or malformed",
|
||||
kind="mail_miss",
|
||||
step="wait_magic_link",
|
||||
steps=steps,
|
||||
)
|
||||
result.magic_link = parsed["magic_link"]
|
||||
steps["wait_magic_link"] = {"ok": True, "has_token": True}
|
||||
|
||||
emit("auth_callback")
|
||||
cb = auth_callback(sess, parsed["token"])
|
||||
result.user_id = str(cb.get("userId") or "")
|
||||
org = ((cb.get("org_memberships") or [{}])[0].get("org") or {})
|
||||
result.organization_id = str(org.get("id") or cb.get("selectedOrgId") or "")
|
||||
result.organization_name = str(org.get("name") or "")
|
||||
steps["callback"] = {
|
||||
"ok": True,
|
||||
"user_id": result.user_id,
|
||||
"organization_id": result.organization_id,
|
||||
}
|
||||
|
||||
emit("create_api_key")
|
||||
key = create_api_key(sess, api_key_name)
|
||||
result.api_key = str(key.get("api_key") or "").strip()
|
||||
result.api_key_id = str(key.get("id") or "")
|
||||
steps["api_key"] = {"ok": True, "api_key_id": result.api_key_id}
|
||||
if not result.api_key:
|
||||
raise TypesafeRegisterError(
|
||||
"missing api_key",
|
||||
kind="token",
|
||||
step="api_key",
|
||||
steps=steps,
|
||||
)
|
||||
result.ok = True
|
||||
result.steps = steps
|
||||
return result
|
||||
except TypesafeRegisterError as exc:
|
||||
if not exc.steps:
|
||||
exc.steps = steps
|
||||
result.error = str(exc)
|
||||
result.error_kind = exc.kind
|
||||
result.fail_step = exc.step
|
||||
result.steps = exc.steps or steps
|
||||
raise
|
||||
except MailMissError as exc:
|
||||
wrapped = TypesafeRegisterError(
|
||||
str(exc) or "magic link timeout",
|
||||
kind="mail_miss",
|
||||
step="wait_magic_link",
|
||||
steps=steps,
|
||||
)
|
||||
wrapped.__cause__ = exc
|
||||
raise wrapped from exc
|
||||
except Exception as exc:
|
||||
raise TypesafeRegisterError(
|
||||
f"unexpected: {exc}",
|
||||
kind="provider",
|
||||
step=result.fail_step or "register_one",
|
||||
steps=steps,
|
||||
) from exc
|
||||
|
||||
|
||||
def _get(session: Any, url: str, **kwargs: Any) -> tuple[Any | None, str]:
|
||||
from .session import request_with_retry
|
||||
|
||||
return request_with_retry(session, "GET", url, timeout=DEFAULT_TIMEOUT, **kwargs)
|
||||
|
||||
|
||||
def _post(session: Any, url: str, **kwargs: Any) -> tuple[Any | None, str]:
|
||||
from .session import request_with_retry
|
||||
|
||||
return request_with_retry(session, "POST", url, timeout=DEFAULT_TIMEOUT, **kwargs)
|
||||
@@ -0,0 +1,74 @@
|
||||
"""HTTP session for typesafe.ai console (ECDH prime256v1 + requests)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ssl
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
import requests
|
||||
from requests.adapters import HTTPAdapter, Retry
|
||||
|
||||
from .constants import COMMON_HEADERS, DEFAULT_TIMEOUT, TLS_ECDH_CURVE
|
||||
|
||||
|
||||
class TlsAdapter(HTTPAdapter):
|
||||
"""Pin an SSLContext so ECDH stays on prime256v1 (original script)."""
|
||||
|
||||
def __init__(self, ctx: ssl.SSLContext, **kw: Any) -> None:
|
||||
self._ctx = ctx
|
||||
super().__init__(**kw)
|
||||
|
||||
def init_poolmanager(self, *a: Any, **kw: Any) -> Any:
|
||||
kw["ssl_context"] = self._ctx
|
||||
return super().init_poolmanager(*a, **kw)
|
||||
|
||||
def proxy_manager_for(self, proxy: str, **kw: Any) -> Any:
|
||||
kw["ssl_context"] = self._ctx
|
||||
return super().proxy_manager_for(proxy, **kw)
|
||||
|
||||
|
||||
def create_session(proxy: str = "") -> requests.Session:
|
||||
"""Build a requests Session with the original script's ECDH pin."""
|
||||
ctx = ssl.create_default_context()
|
||||
try:
|
||||
ctx.set_ecdh_curve(TLS_ECDH_CURVE)
|
||||
except Exception:
|
||||
pass
|
||||
session = requests.Session()
|
||||
retry = Retry(total=2, backoff_factor=1, status_forcelist=(502, 503, 504))
|
||||
session.mount("https://", TlsAdapter(ctx, max_retries=retry))
|
||||
session.headers.update(COMMON_HEADERS)
|
||||
proxy = (proxy or "").strip()
|
||||
if proxy:
|
||||
session.proxies.update({"http": proxy, "https": proxy})
|
||||
return session
|
||||
|
||||
|
||||
def request_with_retry(
|
||||
session: Any,
|
||||
method: str,
|
||||
url: str,
|
||||
*,
|
||||
retry_attempts: int = 3,
|
||||
timeout: float = DEFAULT_TIMEOUT,
|
||||
**kwargs: Any,
|
||||
) -> tuple[Any | None, str]:
|
||||
last_error = ""
|
||||
for _ in range(max(1, retry_attempts)):
|
||||
try:
|
||||
return session.request(method.upper(), url, timeout=timeout, **kwargs), ""
|
||||
except Exception as exc:
|
||||
last_error = str(exc)
|
||||
time.sleep(1)
|
||||
return None, last_error
|
||||
|
||||
|
||||
def response_json(resp: Any) -> dict:
|
||||
if resp is None:
|
||||
return {}
|
||||
try:
|
||||
data = resp.json()
|
||||
return data if isinstance(data, dict) else {}
|
||||
except Exception:
|
||||
return {}
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env bash
|
||||
# typesafe.ai / jev console register runner (in-process via register_core).
|
||||
# Usage:
|
||||
# ./providers/typesafe/run-register.sh [count]
|
||||
set -euo pipefail
|
||||
|
||||
COUNT="${COUNT:-${1:-1}}"
|
||||
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
if [[ -x "$ROOT/.venv/bin/python" ]]; then
|
||||
PY="$ROOT/.venv/bin/python"
|
||||
else
|
||||
PY="${PYTHON:-python3}"
|
||||
fi
|
||||
|
||||
export REGISTER_EGRESS="${REGISTER_EGRESS:-${EGRESS_BACKEND:-${TYPESAFE_EGRESS:-}}}"
|
||||
export TYPESAFE_PROXY="${TYPESAFE_PROXY:-${CHATGPT_PROXY:-${MIMO_PROXY:-}}}"
|
||||
# Magic-link needs a full mail body. tinyhost is the default; OTP-only
|
||||
# wrappers (cloudflare/gmail) cannot extract the Stytch URL.
|
||||
export TYPESAFE_EMAIL_SOURCE="${TYPESAFE_EMAIL_SOURCE:-tinyhost}"
|
||||
export TYPESAFE_EMAIL_DOMAIN="${TYPESAFE_EMAIL_DOMAIN:-${EMAIL_DOMAIN:-}}"
|
||||
export TYPESAFE_PROXY_LIST="${TYPESAFE_PROXY_LIST:-${PROXY_LIST:-}}"
|
||||
export TYPESAFE_PROXY_ROTATE_MODE="${TYPESAFE_PROXY_ROTATE_MODE:-${PROXY_ROTATE_MODE:-}}"
|
||||
export TYPESAFE_PROXY_ROTATE_EVERY="${TYPESAFE_PROXY_ROTATE_EVERY:-${PROXY_ROTATE_EVERY:-1}}"
|
||||
export REGISTER_NODES_FILE="${REGISTER_NODES_FILE:-${NODES_FILE:-$ROOT/nodes.json}}"
|
||||
export CLASH_PROXY="${CLASH_PROXY:-http://127.0.0.1:7897}"
|
||||
|
||||
SINK="${TYPESAFE_SINK:-$ROOT/providers/typesafe/output/pipeline.jsonl}"
|
||||
mkdir -p "$(dirname "$SINK")"
|
||||
|
||||
echo "[typesafe] COUNT=$COUNT egress=${REGISTER_EGRESS:-auto} proxy=${TYPESAFE_PROXY:-'(from egress)'} email_source=$TYPESAFE_EMAIL_SOURCE" >&2
|
||||
|
||||
ARGS=(
|
||||
-m register_core run
|
||||
-p typesafe
|
||||
-n "$COUNT"
|
||||
--email-source "${TYPESAFE_EMAIL_SOURCE}"
|
||||
--sink "$SINK"
|
||||
--timeout "${TYPESAFE_TIMEOUT:-900}"
|
||||
)
|
||||
if [[ -n "${REGISTER_EGRESS}" ]]; then
|
||||
ARGS+=(--egress "${REGISTER_EGRESS}")
|
||||
fi
|
||||
if [[ -n "${TYPESAFE_PROXY}" ]]; then
|
||||
ARGS+=(--proxy "${TYPESAFE_PROXY}")
|
||||
fi
|
||||
if [[ -n "${TYPESAFE_PROXY_LIST}" ]]; then
|
||||
ARGS+=(--proxy-list "${TYPESAFE_PROXY_LIST}")
|
||||
fi
|
||||
if [[ -n "${TYPESAFE_PROXY_ROTATE_MODE}" ]]; then
|
||||
ARGS+=(--proxy-rotate "${TYPESAFE_PROXY_ROTATE_MODE}")
|
||||
fi
|
||||
if [[ -n "${TYPESAFE_PROXY_ROTATE_EVERY}" ]]; then
|
||||
ARGS+=(--proxy-rotate-every "${TYPESAFE_PROXY_ROTATE_EVERY}")
|
||||
fi
|
||||
|
||||
exec "$PY" "${ARGS[@]}"
|
||||
@@ -0,0 +1,140 @@
|
||||
#!/usr/bin/env bash
|
||||
# typesafe.ai smoke.
|
||||
#
|
||||
# Default: connectivity only (tinyhost + console login parse). No mailbox.
|
||||
# Live n=1: TYPESAFE_SMOKE_LIVE=1 — COUNT=1, no CPA inject, this-run json only.
|
||||
# Never treat accounts.jsonl tail as success.
|
||||
set -euo pipefail
|
||||
|
||||
PROVIDER_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
ROOT="$(cd "$PROVIDER_DIR/../.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY ALL_PROXY all_proxy || true
|
||||
export CPA_EXPORT_ENABLED=false
|
||||
export CPA_REMOTE_INJECT=false
|
||||
export CPA_PROBE_CHAT=false
|
||||
export COUNT=1
|
||||
|
||||
if [[ -x "$ROOT/.venv/bin/python" ]]; then
|
||||
PY="$ROOT/.venv/bin/python"
|
||||
else
|
||||
PY="${PYTHON:-python3}"
|
||||
fi
|
||||
|
||||
echo "=== tinyhost ==="
|
||||
curl -sS --max-time 20 -o /dev/null -w "tinyhost=%{http_code} time=%{time_total}\n" \
|
||||
https://tinyhost.shop || {
|
||||
echo "tinyhost unreachable" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
echo "=== console probe (no mailbox) ==="
|
||||
"$PY" - <<'PY'
|
||||
from providers.typesafe.protocol.flow import probe_console
|
||||
|
||||
info = probe_console(proxy="http://127.0.0.1:7897")
|
||||
print(
|
||||
"console_probe",
|
||||
"status=" + str(info.get("status")),
|
||||
"index=" + str(info.get("index")),
|
||||
"dpl=" + str(info.get("deployment_id") or "")[:12],
|
||||
)
|
||||
PY
|
||||
|
||||
if [[ "${TYPESAFE_SMOKE_LIVE:-0}" != "1" ]]; then
|
||||
echo "SMOKE_OK (probe only; set TYPESAFE_SMOKE_LIVE=1 for n=1 register)"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "=== live n=1 (this-run api_key only) ==="
|
||||
mkdir -p logs providers/typesafe/output
|
||||
TS=$(date +%Y%m%d_%H%M%S)
|
||||
LOG="logs/typesafe_smoke_${TS}.log"
|
||||
BEFORE=$("$PY" - <<'PY'
|
||||
from pathlib import Path
|
||||
p = Path("providers/typesafe/output")
|
||||
p.mkdir(parents=True, exist_ok=True)
|
||||
print(max((f.stat().st_mtime for f in p.glob("typesafe-*.json")), default=0))
|
||||
PY
|
||||
)
|
||||
|
||||
set +e
|
||||
./register.sh typesafe 1 >"$LOG" 2>&1
|
||||
RC=$?
|
||||
set -e
|
||||
echo "register_rc=$RC log=$LOG"
|
||||
|
||||
"$PY" - "$RC" "$LOG" "$BEFORE" <<'PY'
|
||||
import json, sys
|
||||
from pathlib import Path
|
||||
|
||||
rc = int(sys.argv[1])
|
||||
log = Path(sys.argv[2]).read_text(errors="replace")
|
||||
before = float(sys.argv[3])
|
||||
out = Path("providers/typesafe/output")
|
||||
fresh = [
|
||||
p for p in out.glob("typesafe-*.json")
|
||||
if p.stat().st_mtime >= before - 1
|
||||
]
|
||||
contract = None
|
||||
head, sep, tail = log.rpartition("CONTRACT_EXIT:")
|
||||
if sep:
|
||||
first = tail.strip().splitlines()[0].strip() if tail.strip() else ""
|
||||
try:
|
||||
contract = int(first)
|
||||
except ValueError:
|
||||
contract = None
|
||||
summary = None
|
||||
decoder = json.JSONDecoder()
|
||||
scan = head
|
||||
pos = 0
|
||||
while pos < len(scan):
|
||||
brace = scan.find("{", pos)
|
||||
if brace < 0:
|
||||
break
|
||||
try:
|
||||
obj, end = decoder.raw_decode(scan, brace)
|
||||
except json.JSONDecodeError:
|
||||
pos = brace + 1
|
||||
continue
|
||||
pos = end
|
||||
if isinstance(obj, dict) and "results" in obj and "ok" in obj:
|
||||
summary = obj
|
||||
|
||||
ok = bool(summary and int(summary.get("ok") or 0) == 1)
|
||||
kind = ""
|
||||
email = ""
|
||||
auth = None
|
||||
if summary and summary.get("results"):
|
||||
r0 = summary["results"][0]
|
||||
kind = str(r0.get("secret_kind") or "")
|
||||
email = str(r0.get("email") or "")
|
||||
if fresh:
|
||||
auth = json.loads(fresh[-1].read_text(encoding="utf-8"))
|
||||
|
||||
# Success = this-run secret_kind + new 0600 json. accounts.jsonl is ignored.
|
||||
live_ok = (
|
||||
rc == 0
|
||||
and contract == 0
|
||||
and ok
|
||||
and kind == "api_key"
|
||||
and auth is not None
|
||||
and bool(auth.get("ok"))
|
||||
and bool(str(auth.get("api_key") or "").strip())
|
||||
and (not email or auth.get("email") == email)
|
||||
)
|
||||
print(json.dumps({
|
||||
"live_ok": live_ok,
|
||||
"rc": rc,
|
||||
"contract_exit": contract,
|
||||
"secret_kind": kind,
|
||||
"email": email,
|
||||
"fresh_json": str(fresh[-1]) if fresh else "",
|
||||
"accounts_jsonl_ignored": True,
|
||||
}, ensure_ascii=False))
|
||||
if not live_ok:
|
||||
sys.exit(1)
|
||||
PY
|
||||
|
||||
echo SMOKE_OK
|
||||
Reference in New Issue
Block a user