This commit is contained in:
2026-01-25 05:40:08 +08:00
parent af161cca4f
commit 32e926c4af
9 changed files with 1495 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
# ==================== 浏览器自动化模块 ====================
# 处理 OpenAI 注册、Codex 授权等浏览器自动化操作
# 使用 DrissionPage 替代 Selenium
# 支持协议模式 (API) 和浏览器模式
import time
import random
@@ -34,6 +35,17 @@ from s2a_service import (
)
from logger import log
# 导入协议模式模块
try:
from api_register import (
api_register_account_only,
is_api_mode_available as _is_api_mode_available,
)
API_MODE_AVAILABLE = _is_api_mode_available()
except ImportError:
API_MODE_AVAILABLE = False
api_register_account_only = None
# 进度更新 (Telegram Bot 使用)
try:
from bot_notifier import progress_update
@@ -945,6 +957,91 @@ def is_logged_in(page, timeout: int = 5) -> bool:
return False
def register_openai_account_api(email: str, password: str, proxy: str = None) -> bool:
"""使用协议模式 (API) 注册 OpenAI 账号
Args:
email: 邮箱地址
password: 密码
proxy: 代理地址 (可选)
Returns:
bool: 是否成功
"""
if not API_MODE_AVAILABLE:
log.warning("协议模式不可用,回退到浏览器模式")
return None # 返回 None 表示需要回退
log.info(f"[API模式] 开始注册 OpenAI 账号: {email}", icon="account")
# 生成随机姓名和生日
random_name = get_random_name()
birthday = get_random_birthday()
birthdate = f"{birthday['year']}-{birthday['month']}-{birthday['day']}"
log.step(f"姓名: {random_name}, 生日: {birthdate}")
# 定义获取验证码的函数
def get_code(target_email):
progress_update(phase="注册", step="等待验证码...")
log.step("等待验证码邮件...")
code, error, email_time = unified_get_verification_code(target_email)
if code:
log.success(f"获取到验证码: {code}")
return code
# 执行 API 注册
try:
result = api_register_account_only(
email=email,
password=password,
real_name=random_name,
birthdate=birthdate,
get_verification_code_func=get_code,
proxy=proxy,
progress_callback=lambda msg: log.step(msg)
)
if result:
log.success(f"[API模式] 注册完成: {email}")
return True
else:
log.warning("[API模式] 注册失败,可能需要回退到浏览器模式")
return False
except Exception as e:
log.error(f"[API模式] 注册异常: {e}")
return False
def register_openai_account_auto(page, email: str, password: str, use_api: bool = True, proxy: str = None) -> bool:
"""自动选择模式注册 OpenAI 账号
优先使用 API 模式,失败则回退到浏览器模式
Args:
page: 浏览器实例 (用于浏览器模式回退)
email: 邮箱地址
password: 密码
use_api: 是否优先使用 API 模式
proxy: 代理地址 (API 模式使用)
Returns:
bool: 是否成功
"""
# 如果启用 API 模式且可用
if use_api and API_MODE_AVAILABLE:
result = register_openai_account_api(email, password, proxy)
if result is True:
return True
elif result is False:
log.warning("API 模式注册失败,回退到浏览器模式...")
# result is None 表示 API 模式不可用,直接使用浏览器模式
# 使用浏览器模式
return register_openai_account(page, email, password)
def register_openai_account(page, email: str, password: str) -> bool:
"""使用浏览器注册 OpenAI 账号
@@ -1976,12 +2073,13 @@ def login_and_authorize_with_otp(email: str) -> tuple[bool, dict]:
return False, None
def register_and_authorize(email: str, password: str) -> tuple:
def register_and_authorize(email: str, password: str, use_api_register: bool = True) -> tuple:
"""完整流程: 注册 OpenAI + Codex 授权 (带重试机制)
Args:
email: 邮箱地址
password: 密码
use_api_register: 是否优先使用 API 模式注册 (默认 True)
Returns:
tuple: (register_success, codex_data)
@@ -1992,8 +2090,11 @@ def register_and_authorize(email: str, password: str) -> tuple:
with browser_context_with_retry(max_browser_retries=2) as ctx:
for attempt in ctx.attempts():
try:
# 注册 OpenAI
register_result = register_openai_account(ctx.page, email, password)
# 注册 OpenAI (优先使用 API 模式)
register_result = register_openai_account_auto(
ctx.page, email, password,
use_api=use_api_register
)
# 检查是否是域名黑名单错误
if register_result == "domain_blacklisted":