diff --git a/platforms/chatgpt/chatgpt_client.py b/platforms/chatgpt/chatgpt_client.py index 42397c7..18af414 100644 --- a/platforms/chatgpt/chatgpt_client.py +++ b/platforms/chatgpt/chatgpt_client.py @@ -847,6 +847,8 @@ class ChatGPTClient: birthdate, skymail_client, stop_before_about_you_submission=False, + otp_wait_timeout=600, + otp_resend_wait_timeout=300, ): """ 完整的注册流程(基于原版 run_register 方法) @@ -866,9 +868,19 @@ class ChatGPTClient: self._log( "注册状态机参数: " - f"stop_before_about_you_submission={'on' if stop_before_about_you_submission else 'off'}" + f"stop_before_about_you_submission={'on' if stop_before_about_you_submission else 'off'}, " + f"otp_wait_timeout={otp_wait_timeout}s, otp_resend_wait_timeout={otp_resend_wait_timeout}s" ) + try: + otp_wait_timeout = max(30, int(otp_wait_timeout or 600)) + except Exception: + otp_wait_timeout = 600 + try: + otp_resend_wait_timeout = max(30, int(otp_resend_wait_timeout or 300)) + except Exception: + otp_resend_wait_timeout = 300 + max_auth_attempts = 3 final_url = "" final_path = "" @@ -965,9 +977,14 @@ class ChatGPTClient: if self._state_is_email_otp(state): self._log("等待邮箱验证码...") - otp_code = skymail_client.wait_for_verification_code(email, timeout=90) + otp_code = skymail_client.wait_for_verification_code( + email, timeout=otp_wait_timeout + ) if not otp_code: - self._log("首次等待未收到验证码,尝试重发一次 email-otp/send 后再等待 60s") + self._log( + "首次等待未收到验证码,尝试重发一次 email-otp/send " + f"后再等待 {otp_resend_wait_timeout}s" + ) otp_send_attempts += 1 resend_ok = self.send_email_otp( referer=state.current_url or state.continue_url or f"{self.AUTH}/email-verification" @@ -976,7 +993,9 @@ class ChatGPTClient: self._log(f"重发验证码成功: attempt={otp_send_attempts}") else: self._log(f"重发验证码失败: attempt={otp_send_attempts}") - otp_code = skymail_client.wait_for_verification_code(email, timeout=60) + otp_code = skymail_client.wait_for_verification_code( + email, timeout=otp_resend_wait_timeout + ) if not otp_code: return False, "未收到验证码" diff --git a/platforms/chatgpt/oauth_client.py b/platforms/chatgpt/oauth_client.py index 022a6f4..18641a9 100644 --- a/platforms/chatgpt/oauth_client.py +++ b/platforms/chatgpt/oauth_client.py @@ -1911,8 +1911,23 @@ class OAuthClient: skymail_client._used_codes = set() tried_codes = set(getattr(skymail_client, "_used_codes", set())) - otp_deadline = time.time() + 60 + try: + otp_wait_seconds = int( + self.config.get( + "chatgpt_oauth_otp_wait_seconds", + self.config.get("chatgpt_otp_wait_seconds", 600), + ) + or 600 + ) + except Exception: + otp_wait_seconds = 600 + otp_wait_seconds = max(30, min(otp_wait_seconds, 3600)) + otp_poll_window = min(30, max(10, otp_wait_seconds)) + otp_deadline = time.time() + otp_wait_seconds otp_sent_at = time.time() + self._log( + f"OAuth OTP 等待窗口: total={otp_wait_seconds}s, poll_window={otp_poll_window}s" + ) def validate_otp(code): tried_codes.add(code) @@ -1958,7 +1973,7 @@ class OAuthClient: self._log("使用 wait_for_verification_code 进行阻塞式获取新验证码...") while time.time() < otp_deadline: remaining = max(1, int(otp_deadline - time.time())) - wait_time = min(10, remaining) + wait_time = min(otp_poll_window, remaining) try: code = skymail_client.wait_for_verification_code( email, @@ -1997,8 +2012,8 @@ class OAuthClient: candidate_codes.append(code) if not candidate_codes: - elapsed = int(60 - max(0, otp_deadline - time.time())) - self._log(f"等待新的 OTP... ({elapsed}s/60s)") + elapsed = int(otp_wait_seconds - max(0, otp_deadline - time.time())) + self._log(f"等待新的 OTP... ({elapsed}s/{otp_wait_seconds}s)") time.sleep(2) continue @@ -2013,6 +2028,6 @@ class OAuthClient: if not self.last_error: self._set_error( - f"OAuth 阶段 OTP 验证失败,已尝试 {len(tried_codes)} 个验证码" + f"OAuth 阶段 OTP 验证失败,已尝试 {len(tried_codes)} 个验证码,等待窗口 {otp_wait_seconds}s" ) return None diff --git a/platforms/chatgpt/refresh_token_registration_engine.py b/platforms/chatgpt/refresh_token_registration_engine.py index 414de79..ec802cc 100644 --- a/platforms/chatgpt/refresh_token_registration_engine.py +++ b/platforms/chatgpt/refresh_token_registration_engine.py @@ -214,6 +214,27 @@ class RefreshTokenRegistrationEngine: ) return any(marker in text for marker in markers) + def _read_int_config( + self, + primary_key: str, + *, + fallback_keys: tuple[str, ...] = (), + default: int, + minimum: int, + maximum: int, + ) -> int: + keys = (primary_key, *tuple(fallback_keys or ())) + for key in keys: + if key not in self.extra_config: + continue + value = self.extra_config.get(key) + try: + parsed = int(value) + except Exception: + continue + return max(minimum, min(parsed, maximum)) + return max(minimum, min(int(default), maximum)) + def _build_chatgpt_client(self) -> ChatGPTClient: client = ChatGPTClient( proxy=self.proxy_url, @@ -314,6 +335,20 @@ class RefreshTokenRegistrationEngine: result = RegistrationResult(success=False, logs=self.logs) last_error = "" fixed_email = str(self.email or "").strip() + register_otp_wait_seconds = self._read_int_config( + "chatgpt_register_otp_wait_seconds", + fallback_keys=("chatgpt_otp_wait_seconds",), + default=600, + minimum=30, + maximum=3600, + ) + register_otp_resend_wait_seconds = self._read_int_config( + "chatgpt_register_otp_resend_wait_seconds", + fallback_keys=("chatgpt_register_otp_wait_seconds", "chatgpt_otp_wait_seconds"), + default=300, + minimum=30, + maximum=3600, + ) try: for attempt in range(self.max_retries): @@ -355,6 +390,12 @@ class RefreshTokenRegistrationEngine: self._log(f"密码: {self.password}") self._log(f"注册信息: {first_name} {last_name}, 生日: {birthdate}") self._log("流程策略: 注册阶段到 about_you 即停,改由 OAuth 新会话补全资料") + self._log( + "验证码等待策略: " + f"register_wait={register_otp_wait_seconds}s, " + f"register_resend_wait={register_otp_resend_wait_seconds}s, " + "oauth_wait=读取 OAuthClient 配置(默认600s)" + ) email_adapter = EmailServiceAdapter( self.email_service, @@ -372,6 +413,8 @@ class RefreshTokenRegistrationEngine: birthdate, email_adapter, stop_before_about_you_submission=True, + otp_wait_timeout=register_otp_wait_seconds, + otp_resend_wait_timeout=register_otp_resend_wait_seconds, ) if not registered: