feat: 维护台 maintain(巡检/补号/日志)+ 配置与 .gitignore
- Flask 控制台:Cliproxy 巡检、批量补号、流水线、运行日志 - codex_stats_oauth_dir 支持统计 Cliproxy auth 目录 - 忽略 accounts/oauth、.venv、本地 DB/日志与 new.py Made-with: Cursor
This commit is contained in:
1 parent
ebef9acd7c
commit
a089da31cd
23 files changed
+3719
-631
No files matched your search
+281
-101
@@ -1,101 +1,281 @@
|
||||
"""临时邮箱服务 - mail.chatgpt.org.uk API"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import time
|
||||
from typing import Optional, Tuple
|
||||
|
||||
import requests
|
||||
import urllib3
|
||||
|
||||
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
||||
|
||||
BASE_URL = "https://mail.chatgpt.org.uk/api"
|
||||
HEADERS = {
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||
"Origin": "https://mail.chatgpt.org.uk",
|
||||
"Referer": "https://mail.chatgpt.org.uk/",
|
||||
}
|
||||
|
||||
|
||||
def create_temp_email() -> Optional[str]:
|
||||
"""创建临时邮箱,返回邮箱地址"""
|
||||
try:
|
||||
r = requests.get(
|
||||
f"{BASE_URL}/generate-email",
|
||||
headers={**HEADERS, "Content-Type": "application/json"},
|
||||
timeout=10,
|
||||
verify=False,
|
||||
)
|
||||
if r.status_code == 200:
|
||||
data = r.json()
|
||||
if data.get("success") and data.get("data", {}).get("email"):
|
||||
return data["data"]["email"]
|
||||
except Exception as e:
|
||||
print(f"[email] 创建失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def fetch_emails(email_address: str) -> list[dict]:
|
||||
"""获取邮件列表"""
|
||||
try:
|
||||
ts = int(time.time() * 1000)
|
||||
r = requests.get(
|
||||
f"{BASE_URL}/emails?email={email_address}&_t={ts}",
|
||||
headers={**HEADERS, "Cache-Control": "no-cache"},
|
||||
timeout=30,
|
||||
verify=False,
|
||||
)
|
||||
if r.status_code == 200:
|
||||
data = r.json()
|
||||
if data.get("success") and data.get("data", {}).get("emails"):
|
||||
return data["data"]["emails"]
|
||||
except Exception as e:
|
||||
print(f"[email] 获取邮件错误: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def extract_code(email_data: dict) -> Optional[str]:
|
||||
"""从邮件中提取验证码"""
|
||||
for field in ["subject", "html_content", "content", "text_content", "body"]:
|
||||
content = email_data.get(field, "")
|
||||
if not content:
|
||||
continue
|
||||
# HTML <div class="code">
|
||||
m = re.search(r'<div[^>]*class="code"[^>]*>(\d{6})</div>', content, re.I)
|
||||
if m:
|
||||
return m.group(1)
|
||||
# verification code: XXXXXX
|
||||
m = re.search(r'[Vv]erification\s+code:?\s*(\d{6})', content)
|
||||
if m:
|
||||
return m.group(1)
|
||||
# code: XXXXXX
|
||||
m = re.search(r'code:?\s*(\d{6})', content, re.I)
|
||||
if m:
|
||||
return m.group(1)
|
||||
# 独立6位数字
|
||||
clean = re.sub(r'<[^>]+>', ' ', content)
|
||||
m = re.search(r'\b(\d{6})\b', clean)
|
||||
if m and m.group(1) != "177010":
|
||||
return m.group(1)
|
||||
return None
|
||||
|
||||
|
||||
def wait_for_code(email_address: str, timeout: int = 120) -> Optional[str]:
|
||||
"""轮询等待验证码"""
|
||||
start = time.time()
|
||||
seen = set()
|
||||
while time.time() - start < timeout:
|
||||
for mail in fetch_emails(email_address):
|
||||
mid = mail.get("id") or mail.get("date")
|
||||
if mid in seen:
|
||||
continue
|
||||
seen.add(mid)
|
||||
sender = str(mail.get("from", "") or mail.get("sender", "")).lower()
|
||||
subject = str(mail.get("subject", "")).lower()
|
||||
if any(k in sender or k in subject for k in ("openai", "chatgpt", "verification")):
|
||||
code = extract_code(mail)
|
||||
if code:
|
||||
return code
|
||||
time.sleep(5)
|
||||
return None
|
||||
"""临时邮箱 — [GPTMail](https://mail.chatgpt.org.uk) 网页模拟
|
||||
|
||||
对外接口:
|
||||
- create_temp_email() -> Optional[str]
|
||||
- fetch_emails(email_address) -> list[dict]
|
||||
- wait_for_code(email_address, timeout=120) -> Optional[str]
|
||||
|
||||
通过网页同款鉴权(/api/inbox-token + X-Inbox-Token)访问收件接口,
|
||||
避免 API key 配额限制。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
|
||||
GPTMAIL_BASE = "https://mail.chatgpt.org.uk"
|
||||
|
||||
# 当前进程内:本脚本创建的邮箱 -> 会话信息
|
||||
_MAIL_STATE: dict[str, dict] = {}
|
||||
|
||||
UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136 Safari/537.36"
|
||||
|
||||
def _gptmail_headers() -> dict:
|
||||
return {
|
||||
"User-Agent": UA,
|
||||
"Accept": "application/json",
|
||||
}
|
||||
|
||||
|
||||
def _normalize_gptmail_message(raw: dict) -> dict:
|
||||
"""统一成 extract_code / wait_for_code 可用的字段"""
|
||||
sender = (raw.get("from_address") or raw.get("from") or "").strip()
|
||||
mid = raw.get("id")
|
||||
text = raw.get("content") or raw.get("text") or ""
|
||||
html = raw.get("html_content") or raw.get("html") or ""
|
||||
return {
|
||||
"id": mid,
|
||||
"from": sender,
|
||||
"sender": sender,
|
||||
"subject": raw.get("subject", "") or "",
|
||||
"text_content": text,
|
||||
"html_content": html,
|
||||
"content": text,
|
||||
"body": text,
|
||||
"date": raw.get("created_at") or raw.get("timestamp"),
|
||||
}
|
||||
|
||||
|
||||
def _new_session() -> requests.Session:
|
||||
s = requests.Session()
|
||||
s.headers.update(_gptmail_headers())
|
||||
return s
|
||||
|
||||
|
||||
def _bootstrap_session(session: requests.Session) -> bool:
|
||||
"""先访问首页拿 gm_sid,后续 inbox-token 才会通过"""
|
||||
try:
|
||||
r = session.get(
|
||||
f"{GPTMAIL_BASE}/",
|
||||
headers={
|
||||
"Referer": f"{GPTMAIL_BASE}/",
|
||||
"Origin": GPTMAIL_BASE,
|
||||
**_gptmail_headers(),
|
||||
},
|
||||
timeout=20,
|
||||
)
|
||||
return r.status_code == 200
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
|
||||
def _refresh_inbox_token(session: requests.Session, email: str = "") -> Optional[dict]:
|
||||
r = session.post(
|
||||
f"{GPTMAIL_BASE}/api/inbox-token",
|
||||
json={"email": (email or "").strip().lower()},
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"Referer": f"{GPTMAIL_BASE}/",
|
||||
"Origin": GPTMAIL_BASE,
|
||||
**_gptmail_headers(),
|
||||
},
|
||||
timeout=20,
|
||||
)
|
||||
if r.status_code != 200:
|
||||
return None
|
||||
try:
|
||||
body = r.json()
|
||||
except Exception:
|
||||
return None
|
||||
if not body.get("success"):
|
||||
return None
|
||||
auth = body.get("auth") or {}
|
||||
if not isinstance(auth, dict):
|
||||
return None
|
||||
token = (auth.get("token") or "").strip()
|
||||
if not token:
|
||||
return None
|
||||
return {
|
||||
"token": token,
|
||||
"email": (auth.get("email") or "").strip().lower(),
|
||||
"expires_at": int(auth.get("expires_at") or 0),
|
||||
}
|
||||
|
||||
|
||||
def _auth_fetch(
|
||||
session: requests.Session,
|
||||
method: str,
|
||||
path: str,
|
||||
*,
|
||||
auth_email: str,
|
||||
params: dict | None = None,
|
||||
json_data: dict | None = None,
|
||||
) -> requests.Response:
|
||||
auth = _refresh_inbox_token(session, auth_email)
|
||||
headers = {}
|
||||
if auth and auth.get("token"):
|
||||
headers["X-Inbox-Token"] = auth["token"]
|
||||
return session.request(
|
||||
method,
|
||||
f"{GPTMAIL_BASE}{path}",
|
||||
params=params,
|
||||
json=json_data,
|
||||
headers=headers,
|
||||
timeout=20,
|
||||
)
|
||||
|
||||
|
||||
def _gptmail_fetch_detail(session: requests.Session, email_address: str, email_id: str) -> Optional[dict]:
|
||||
r = _auth_fetch(session, "GET", f"/api/email/{email_id}", auth_email=email_address)
|
||||
if r.status_code != 200:
|
||||
return None
|
||||
try:
|
||||
body = r.json()
|
||||
except Exception:
|
||||
return None
|
||||
if not body.get("success"):
|
||||
return None
|
||||
data = body.get("data") or {}
|
||||
return data if isinstance(data, dict) else None
|
||||
|
||||
|
||||
def _gptmail_create_email() -> Optional[str]:
|
||||
session = _new_session()
|
||||
if not _bootstrap_session(session):
|
||||
print("[email] GPTMail 会话初始化失败")
|
||||
return None
|
||||
r = _auth_fetch(session, "GET", "/api/generate-email", auth_email="")
|
||||
try:
|
||||
body = r.json()
|
||||
except Exception:
|
||||
print(f"[email] GPTMail 响应非 JSON: HTTP {r.status_code}")
|
||||
return None
|
||||
|
||||
if not body.get("success"):
|
||||
err = body.get("error") or body.get("message") or r.text[:200]
|
||||
print(f"[email] GPTMail 创建失败: {err}")
|
||||
return None
|
||||
|
||||
data = body.get("data") or {}
|
||||
email = (data.get("email") or "").strip()
|
||||
if not email or "@" not in email:
|
||||
print("[email] GPTMail 返回缺少 email")
|
||||
return None
|
||||
|
||||
_MAIL_STATE[email] = {
|
||||
"provider": "gptmail",
|
||||
"session": session,
|
||||
}
|
||||
return email
|
||||
|
||||
|
||||
def create_temp_email() -> Optional[str]:
|
||||
"""创建临时邮箱,返回邮箱地址"""
|
||||
try:
|
||||
return _gptmail_create_email()
|
||||
except Exception as e:
|
||||
print(f"[email] 创建失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def fetch_emails(email_address: str) -> list[dict]:
|
||||
"""获取邮件列表(统一输出字典列表)"""
|
||||
state = _MAIL_STATE.get(email_address)
|
||||
if not state or state.get("provider") != "gptmail":
|
||||
return []
|
||||
|
||||
session = state.get("session")
|
||||
if not isinstance(session, requests.Session):
|
||||
session = _new_session()
|
||||
_bootstrap_session(session)
|
||||
state["session"] = session
|
||||
|
||||
try:
|
||||
r = _auth_fetch(
|
||||
session,
|
||||
"GET",
|
||||
"/api/emails",
|
||||
auth_email=email_address,
|
||||
params={"email": email_address},
|
||||
)
|
||||
if r.status_code != 200:
|
||||
return []
|
||||
|
||||
body = r.json()
|
||||
if not body.get("success"):
|
||||
return []
|
||||
|
||||
data = body.get("data") or {}
|
||||
members = data.get("emails") or []
|
||||
if not isinstance(members, list):
|
||||
return []
|
||||
|
||||
out: list[dict] = []
|
||||
for msg in members:
|
||||
if not isinstance(msg, dict):
|
||||
continue
|
||||
mid = msg.get("id")
|
||||
detail = msg
|
||||
if mid and not (msg.get("content") or msg.get("html_content")):
|
||||
d = _gptmail_fetch_detail(session, email_address, mid)
|
||||
if d:
|
||||
detail = {**msg, **d}
|
||||
|
||||
out.append(_normalize_gptmail_message(detail))
|
||||
|
||||
return out
|
||||
except Exception as e:
|
||||
print(f"[email] 获取邮件错误: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def extract_code(email_data: dict) -> Optional[str]:
|
||||
"""从邮件中提取验证码"""
|
||||
for field in ["subject", "html_content", "content", "text_content", "body"]:
|
||||
content = email_data.get(field, "")
|
||||
if not content:
|
||||
continue
|
||||
# HTML <div class="code">
|
||||
m = re.search(r'<div[^>]*class="code"[^>]*>(\d{6})</div>', content, re.I)
|
||||
if m:
|
||||
return m.group(1)
|
||||
# verification code: XXXXXX
|
||||
m = re.search(r'[Vv]erification\s+code:?\s*(\d{6})', content)
|
||||
if m:
|
||||
return m.group(1)
|
||||
# code: XXXXXX
|
||||
m = re.search(r'code:?\s*(\d{6})', content, re.I)
|
||||
if m:
|
||||
return m.group(1)
|
||||
# 独立6位数字
|
||||
clean = re.sub(r"<[^>]+>", " ", content)
|
||||
m = re.search(r"\b(\d{6})\b", clean)
|
||||
if m and m.group(1) != "177010":
|
||||
return m.group(1)
|
||||
return None
|
||||
|
||||
|
||||
def wait_for_code(email_address: str, timeout: int = 120) -> Optional[str]:
|
||||
"""轮询等待验证码"""
|
||||
start = time.time()
|
||||
seen = set()
|
||||
while time.time() - start < timeout:
|
||||
for mail in fetch_emails(email_address):
|
||||
mid = mail.get("id") or mail.get("date")
|
||||
if mid in seen:
|
||||
continue
|
||||
seen.add(mid)
|
||||
sender = str(mail.get("from", "") or mail.get("sender", "")).lower()
|
||||
subject = str(mail.get("subject", "")).lower()
|
||||
if any(
|
||||
k in sender or k in subject
|
||||
for k in ("openai", "chatgpt", "verification", "noreply")
|
||||
):
|
||||
code = extract_code(mail)
|
||||
if code:
|
||||
return code
|
||||
time.sleep(5)
|
||||
return None
|
||||
Reference in new issue
Block a user