mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-07-02 12:52:20 +08:00
Merge branch 'master' of https://github.com/cnlimiter/codex-manager into feat/newapi-upload-target-support
This commit is contained in:
@@ -5,12 +5,13 @@
|
||||
|
||||
import abc
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Dict, Any, List
|
||||
from enum import Enum
|
||||
|
||||
from ..config.constants import EmailServiceType
|
||||
from ..config.constants import EmailServiceType, OTP_CODE_PATTERN, OTP_CODE_SEMANTIC_PATTERN
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -146,6 +147,8 @@ class BaseEmailService(abc.ABC):
|
||||
self._last_error = None
|
||||
self._provider_backoff = reset_adaptive_backoff()
|
||||
|
||||
_EMAIL_ADDRESS_PATTERN = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}")
|
||||
|
||||
@property
|
||||
def status(self) -> EmailServiceStatus:
|
||||
"""获取服务状态"""
|
||||
@@ -272,6 +275,30 @@ class BaseEmailService(abc.ABC):
|
||||
return email_info
|
||||
return None
|
||||
|
||||
def _strip_email_addresses(self, text: str) -> str:
|
||||
"""移除文本中的邮箱地址,避免域名数字被误识别为验证码。"""
|
||||
return self._EMAIL_ADDRESS_PATTERN.sub(" ", text or "")
|
||||
|
||||
def _extract_otp_from_text(self, text: str, pattern: Optional[str] = None) -> Optional[str]:
|
||||
"""
|
||||
从文本中提取验证码。
|
||||
|
||||
优先语义匹配,再在移除邮箱地址后的文本上做 6 位数字兜底。
|
||||
"""
|
||||
if not text:
|
||||
return None
|
||||
|
||||
semantic_match = re.search(OTP_CODE_SEMANTIC_PATTERN, text, re.IGNORECASE)
|
||||
if semantic_match:
|
||||
return semantic_match.group(1)
|
||||
|
||||
fallback_pattern = pattern or OTP_CODE_PATTERN
|
||||
simple_match = re.search(fallback_pattern, self._strip_email_addresses(text))
|
||||
if simple_match:
|
||||
return simple_match.group(1)
|
||||
|
||||
return None
|
||||
|
||||
def wait_for_email(
|
||||
self,
|
||||
email: str,
|
||||
|
||||
@@ -237,34 +237,31 @@ class FreemailService(BaseEmailService):
|
||||
if "openai" not in content.lower():
|
||||
continue
|
||||
|
||||
# 尝试直接使用 Freemail 提取的验证码
|
||||
v_code = mail.get("verification_code")
|
||||
if v_code:
|
||||
logger.info(f"从 Freemail 邮箱 {email} 找到验证码: {v_code}")
|
||||
self.update_status(True)
|
||||
return v_code
|
||||
|
||||
# 如果没有直接提供,通过正则匹配 preview
|
||||
match = re.search(pattern, content)
|
||||
if match:
|
||||
code = match.group(1)
|
||||
code = self._extract_otp_from_text(content, pattern)
|
||||
if code:
|
||||
logger.info(f"从 Freemail 邮箱 {email} 找到验证码: {code}")
|
||||
self.update_status(True)
|
||||
return code
|
||||
|
||||
v_code = str(mail.get("verification_code") or "").strip()
|
||||
|
||||
# 如果依然未找到,获取邮件详情进行匹配
|
||||
try:
|
||||
detail = self._make_request("GET", f"/api/email/{mail_id}")
|
||||
full_content = str(detail.get("content", "")) + "\n" + str(detail.get("html_content", ""))
|
||||
match = re.search(pattern, full_content)
|
||||
if match:
|
||||
code = match.group(1)
|
||||
code = self._extract_otp_from_text(full_content, pattern)
|
||||
if code:
|
||||
logger.info(f"从 Freemail 邮箱 {email} 找到验证码: {code}")
|
||||
self.update_status(True)
|
||||
return code
|
||||
except Exception as e:
|
||||
logger.debug(f"获取 Freemail 邮件详情失败: {e}")
|
||||
|
||||
if re.fullmatch(r"\d{6}", v_code):
|
||||
logger.info(f"从 Freemail 邮箱 {email} 找到验证码: {v_code}")
|
||||
self.update_status(True)
|
||||
return v_code
|
||||
|
||||
except Exception as e:
|
||||
logger.debug(f"检查 Freemail 邮件时出错: {e}")
|
||||
|
||||
|
||||
@@ -353,9 +353,8 @@ class TempMailService(BaseEmailService):
|
||||
if "openai" not in sender and "openai" not in content.lower():
|
||||
continue
|
||||
|
||||
match = re.search(pattern, content)
|
||||
if match:
|
||||
code = match.group(1)
|
||||
code = self._extract_otp_from_text(content, pattern)
|
||||
if code:
|
||||
logger.info(f"从 TempMail 邮箱 {email} 找到验证码: {code}")
|
||||
self.update_status(True)
|
||||
return code
|
||||
|
||||
Reference in New Issue
Block a user