recovered from aaronjaworski710
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
"""OpenAI / ChatGPT platform OAuth registration protocol (in-process)."""
|
||||
|
||||
from .flow import ChatGPTRegisterError, RegistrationResult, register_one
|
||||
|
||||
__all__ = ["ChatGPTRegisterError", "RegistrationResult", "register_one"]
|
||||
@@ -0,0 +1,73 @@
|
||||
"""OpenAI auth / platform OAuth constants (protocol register path)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
AUTH_BASE = "https://auth.openai.com"
|
||||
PLATFORM_BASE = "https://platform.openai.com"
|
||||
SENTINEL_BASE = "https://sentinel.openai.com"
|
||||
|
||||
# Platform OAuth client used by open-reg-auto / zhuce6-style protocol path.
|
||||
PLATFORM_OAUTH_CLIENT_ID = "app_2SKx67EdpoN0G6j64rFvigXD"
|
||||
PLATFORM_OAUTH_REDIRECT_URI = f"{PLATFORM_BASE}/auth/callback"
|
||||
PLATFORM_OAUTH_AUDIENCE = "https://api.openai.com/v1"
|
||||
|
||||
# curl_cffi 0.15 chrome* fingerprints from chrome119+ are Macintosh TLS profiles.
|
||||
# Headers MUST match the impersonate platform (Mac Chrome 145). Windows headers
|
||||
# with Mac JA3 previously produced a fingerprint mismatch risk signal.
|
||||
DEFAULT_IMPERSONATE = "chrome145"
|
||||
|
||||
USER_AGENT = (
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
||||
"Chrome/145.0.0.0 Safari/537.36"
|
||||
)
|
||||
SEC_CH_UA = '"Google Chrome";v="145", "Not?A_Brand";v="8", "Chromium";v="145"'
|
||||
SEC_CH_UA_FULL = (
|
||||
'"Chromium";v="145.0.0.0", "Not:A-Brand";v="99.0.0.0", '
|
||||
'"Google Chrome";v="145.0.0.0"'
|
||||
)
|
||||
SEC_CH_UA_PLATFORM = '"macOS"'
|
||||
SEC_CH_UA_PLATFORM_VERSION = '"15.0.0"'
|
||||
SEC_CH_UA_ARCH = '"arm64"'
|
||||
|
||||
DEFAULT_TIMEOUT = 30
|
||||
SENTINEL_SDK = "https://sentinel.openai.com/sentinel/20260124ceb8/sdk.js"
|
||||
|
||||
COMMON_HEADERS: dict[str, str] = {
|
||||
"accept": "application/json",
|
||||
"accept-language": "en-US,en;q=0.9",
|
||||
"content-type": "application/json",
|
||||
"origin": AUTH_BASE,
|
||||
"priority": "u=1, i",
|
||||
"user-agent": USER_AGENT,
|
||||
"sec-ch-ua": SEC_CH_UA,
|
||||
"sec-ch-ua-arch": SEC_CH_UA_ARCH,
|
||||
"sec-ch-ua-bitness": '"64"',
|
||||
"sec-ch-ua-full-version-list": SEC_CH_UA_FULL,
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-model": '""',
|
||||
"sec-ch-ua-platform": SEC_CH_UA_PLATFORM,
|
||||
"sec-ch-ua-platform-version": SEC_CH_UA_PLATFORM_VERSION,
|
||||
"sec-fetch-dest": "empty",
|
||||
"sec-fetch-mode": "cors",
|
||||
"sec-fetch-site": "same-origin",
|
||||
}
|
||||
|
||||
NAVIGATE_HEADERS: dict[str, str] = {
|
||||
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
||||
"accept-language": "en-US,en;q=0.9",
|
||||
"user-agent": USER_AGENT,
|
||||
"sec-ch-ua": SEC_CH_UA,
|
||||
"sec-ch-ua-arch": SEC_CH_UA_ARCH,
|
||||
"sec-ch-ua-bitness": '"64"',
|
||||
"sec-ch-ua-full-version-list": SEC_CH_UA_FULL,
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-model": '""',
|
||||
"sec-ch-ua-platform": SEC_CH_UA_PLATFORM,
|
||||
"sec-ch-ua-platform-version": SEC_CH_UA_PLATFORM_VERSION,
|
||||
"sec-fetch-dest": "document",
|
||||
"sec-fetch-mode": "navigate",
|
||||
"sec-fetch-site": "same-origin",
|
||||
"sec-fetch-user": "?1",
|
||||
"upgrade-insecure-requests": "1",
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,148 @@
|
||||
"""OpenAI Sentinel proof-of-work token builder."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import random
|
||||
import time
|
||||
import uuid
|
||||
from typing import Any
|
||||
|
||||
from .constants import (
|
||||
SEC_CH_UA,
|
||||
SEC_CH_UA_PLATFORM,
|
||||
SENTINEL_BASE,
|
||||
SENTINEL_SDK,
|
||||
USER_AGENT,
|
||||
)
|
||||
|
||||
|
||||
class SentinelTokenGenerator:
|
||||
MAX_ATTEMPTS = 500_000
|
||||
ERROR_PREFIX = "wQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D"
|
||||
|
||||
def __init__(self, device_id: str, ua: str = USER_AGENT) -> None:
|
||||
self.device_id = device_id
|
||||
self.user_agent = ua
|
||||
self.sid = str(uuid.uuid4())
|
||||
|
||||
@staticmethod
|
||||
def _fnv1a_32(text: str) -> str:
|
||||
h = 2166136261
|
||||
for ch in text:
|
||||
h ^= ord(ch)
|
||||
h = (h * 16777619) & 0xFFFFFFFF
|
||||
h ^= h >> 16
|
||||
h = (h * 2246822507) & 0xFFFFFFFF
|
||||
h ^= h >> 13
|
||||
h = (h * 3266489909) & 0xFFFFFFFF
|
||||
h ^= h >> 16
|
||||
return format(h & 0xFFFFFFFF, "08x")
|
||||
|
||||
def _get_config(self) -> list[Any]:
|
||||
perf_now = random.uniform(1000, 50000)
|
||||
return [
|
||||
"1920x1080",
|
||||
time.strftime(
|
||||
"%a %b %d %Y %H:%M:%S GMT+0000 (Coordinated Universal Time)",
|
||||
time.gmtime(),
|
||||
),
|
||||
4294705152,
|
||||
random.random(),
|
||||
self.user_agent,
|
||||
SENTINEL_SDK,
|
||||
None,
|
||||
None,
|
||||
"en-US",
|
||||
random.random(),
|
||||
random.choice(
|
||||
[
|
||||
"vendorSub-undefined",
|
||||
"plugins-undefined",
|
||||
"mimeTypes-undefined",
|
||||
"hardwareConcurrency-undefined",
|
||||
]
|
||||
),
|
||||
random.choice(
|
||||
["location", "implementation", "URL", "documentURI", "compatMode"]
|
||||
),
|
||||
random.choice(
|
||||
["Object", "Function", "Array", "Number", "parseFloat", "undefined"]
|
||||
),
|
||||
perf_now,
|
||||
self.sid,
|
||||
"",
|
||||
random.choice([4, 8, 12, 16]),
|
||||
time.time() * 1000 - perf_now,
|
||||
]
|
||||
|
||||
@staticmethod
|
||||
def _b64(data: Any) -> str:
|
||||
raw = json.dumps(data, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
|
||||
return base64.b64encode(raw).decode("ascii")
|
||||
|
||||
def generate_requirements_token(self) -> str:
|
||||
data = self._get_config()
|
||||
data[3] = 1
|
||||
data[9] = round(random.uniform(5, 50))
|
||||
return "gAAAAAC" + self._b64(data)
|
||||
|
||||
def generate_token(self, seed: str, difficulty: str) -> str:
|
||||
start = time.time()
|
||||
data = self._get_config()
|
||||
difficulty = str(difficulty or "0")
|
||||
for i in range(self.MAX_ATTEMPTS):
|
||||
data[3] = i
|
||||
data[9] = round((time.time() - start) * 1000)
|
||||
payload = self._b64(data)
|
||||
if self._fnv1a_32(seed + payload)[: len(difficulty)] <= difficulty:
|
||||
return "gAAAAAB" + payload + "~S"
|
||||
return "gAAAAAB" + self.ERROR_PREFIX + self._b64(str(None))
|
||||
|
||||
|
||||
def build_sentinel_token(session: Any, device_id: str, flow: str) -> str:
|
||||
"""POST sentinel/req and return JSON string for openai-sentinel-token header."""
|
||||
generator = SentinelTokenGenerator(device_id, USER_AGENT)
|
||||
resp = session.post(
|
||||
f"{SENTINEL_BASE}/backend-api/sentinel/req",
|
||||
data=json.dumps(
|
||||
{
|
||||
"p": generator.generate_requirements_token(),
|
||||
"id": device_id,
|
||||
"flow": flow,
|
||||
}
|
||||
),
|
||||
headers={
|
||||
"Content-Type": "text/plain;charset=UTF-8",
|
||||
"Referer": f"{SENTINEL_BASE}/backend-api/sentinel/frame.html",
|
||||
"Origin": SENTINEL_BASE,
|
||||
"User-Agent": USER_AGENT,
|
||||
"sec-ch-ua": SEC_CH_UA,
|
||||
"sec-ch-ua-mobile": "?0",
|
||||
"sec-ch-ua-platform": SEC_CH_UA_PLATFORM,
|
||||
},
|
||||
timeout=20,
|
||||
)
|
||||
try:
|
||||
data = resp.json() if hasattr(resp, "json") else {}
|
||||
except Exception:
|
||||
data = {}
|
||||
if not isinstance(data, dict):
|
||||
data = {}
|
||||
token = str(data.get("token") or "").strip()
|
||||
status = int(getattr(resp, "status_code", 0) or 0)
|
||||
if status != 200 or not token:
|
||||
raise RuntimeError(f"sentinel_req_failed_{status}")
|
||||
pow_data = data.get("proofofwork") or {}
|
||||
if isinstance(pow_data, dict) and pow_data.get("required") and pow_data.get("seed"):
|
||||
p_value = generator.generate_token(
|
||||
str(pow_data.get("seed") or ""),
|
||||
str(pow_data.get("difficulty") or "0"),
|
||||
)
|
||||
else:
|
||||
p_value = generator.generate_requirements_token()
|
||||
return json.dumps(
|
||||
{"p": p_value, "t": "", "c": token, "id": device_id, "flow": flow},
|
||||
separators=(",", ":"),
|
||||
)
|
||||
@@ -0,0 +1,101 @@
|
||||
"""HTTP session helpers for ChatGPT protocol register."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from typing import Any
|
||||
from urllib.parse import quote
|
||||
|
||||
from .constants import DEFAULT_IMPERSONATE, DEFAULT_TIMEOUT
|
||||
|
||||
|
||||
def create_session(
|
||||
proxy: str = "",
|
||||
*,
|
||||
impersonate: str = DEFAULT_IMPERSONATE,
|
||||
) -> Any:
|
||||
"""Prefer curl_cffi (TLS fingerprint); fall back to requests.
|
||||
|
||||
Default impersonate is aligned with constants.USER_AGENT / sec-ch-ua
|
||||
(Mac Chrome 145). Passing a mismatched platform is a risk-engine signal.
|
||||
"""
|
||||
proxy = (proxy or "").strip()
|
||||
try:
|
||||
from curl_cffi import requests as curl_requests
|
||||
|
||||
kwargs: dict[str, Any] = {"impersonate": impersonate, "verify": False}
|
||||
if proxy:
|
||||
kwargs["proxy"] = proxy
|
||||
return curl_requests.Session(**kwargs)
|
||||
except Exception:
|
||||
import requests
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib3.util.retry import Retry
|
||||
|
||||
session = requests.Session()
|
||||
retry = Retry(
|
||||
total=2,
|
||||
connect=2,
|
||||
read=2,
|
||||
backoff_factor=0.5,
|
||||
status_forcelist=(429, 500, 502, 503, 504),
|
||||
)
|
||||
adapter = HTTPAdapter(max_retries=retry, pool_connections=20, pool_maxsize=20)
|
||||
session.mount("http://", adapter)
|
||||
session.mount("https://", adapter)
|
||||
session.verify = False
|
||||
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 quote_param(value: str) -> str:
|
||||
return quote(str(value), safe="")
|
||||
|
||||
|
||||
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 {}
|
||||
|
||||
|
||||
def cookie_get(session: Any, name: str) -> str:
|
||||
jar = getattr(session, "cookies", None)
|
||||
if jar is None:
|
||||
return ""
|
||||
try:
|
||||
val = jar.get(name)
|
||||
if val:
|
||||
return str(val)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
for cookie in jar:
|
||||
if getattr(cookie, "name", "") == name:
|
||||
return str(getattr(cookie, "value", "") or "")
|
||||
except Exception:
|
||||
pass
|
||||
return ""
|
||||
Reference in New Issue
Block a user