mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-06-09 09:29:39 +08:00
feat(tempmail): 将邮箱 token 持久化到数据库,重启后可继续接收验证码
This commit is contained in:
@@ -6,9 +6,6 @@ import re
|
|||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
from typing import Optional, Dict, Any, List
|
from typing import Optional, Dict, Any, List
|
||||||
import json
|
|
||||||
|
|
||||||
from curl_cffi import requests as cffi_requests
|
|
||||||
|
|
||||||
from .base import BaseEmailService, EmailServiceError, EmailServiceType
|
from .base import BaseEmailService, EmailServiceError, EmailServiceType
|
||||||
from ..core.http_client import HTTPClient, RequestConfig
|
from ..core.http_client import HTTPClient, RequestConfig
|
||||||
@@ -58,10 +55,32 @@ class TempmailService(BaseEmailService):
|
|||||||
config=http_config
|
config=http_config
|
||||||
)
|
)
|
||||||
|
|
||||||
# 状态变量
|
# 状态变量(内存缓存,重启后从 DB 按需查询)
|
||||||
self._email_cache: Dict[str, Dict[str, Any]] = {}
|
self._email_cache: Dict[str, Dict[str, Any]] = {}
|
||||||
self._last_check_time: float = 0
|
self._last_check_time: float = 0
|
||||||
|
|
||||||
|
def _save_token_to_db(self, email: str, token: str) -> None:
|
||||||
|
"""将邮箱 token 持久化到 Setting 表,key=tempmail_token:{email}"""
|
||||||
|
try:
|
||||||
|
from ..database.session import get_db
|
||||||
|
from ..database.crud import set_setting
|
||||||
|
with get_db() as db:
|
||||||
|
set_setting(db, f"tempmail_token:{email}", token, category="tempmail")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"保存 Tempmail token 到数据库失败: {e}")
|
||||||
|
|
||||||
|
def _load_token_from_db(self, email: str) -> Optional[str]:
|
||||||
|
"""从 Setting 表读取邮箱 token"""
|
||||||
|
try:
|
||||||
|
from ..database.session import get_db
|
||||||
|
from ..database.crud import get_setting
|
||||||
|
with get_db() as db:
|
||||||
|
setting = get_setting(db, f"tempmail_token:{email}")
|
||||||
|
return setting.value if setting else None
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"从数据库读取 Tempmail token 失败: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
def create_email(self, config: Dict[str, Any] = None) -> Dict[str, Any]:
|
def create_email(self, config: Dict[str, Any] = None) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
创建新的临时邮箱
|
创建新的临时邮箱
|
||||||
@@ -107,6 +126,7 @@ class TempmailService(BaseEmailService):
|
|||||||
"created_at": time.time(),
|
"created_at": time.time(),
|
||||||
}
|
}
|
||||||
self._email_cache[email] = email_info
|
self._email_cache[email] = email_info
|
||||||
|
self._save_token_to_db(email, token)
|
||||||
|
|
||||||
logger.info(f"成功创建 Tempmail.lol 邮箱: {email}")
|
logger.info(f"成功创建 Tempmail.lol 邮箱: {email}")
|
||||||
self.update_status(True)
|
self.update_status(True)
|
||||||
@@ -141,12 +161,14 @@ class TempmailService(BaseEmailService):
|
|||||||
"""
|
"""
|
||||||
token = email_id
|
token = email_id
|
||||||
if not token:
|
if not token:
|
||||||
# 从缓存中查找 token
|
# 先从内存缓存查找,再从数据库查找
|
||||||
if email in self._email_cache:
|
if email in self._email_cache:
|
||||||
token = self._email_cache[email].get("token")
|
token = self._email_cache[email].get("token")
|
||||||
else:
|
if not token:
|
||||||
logger.warning(f"未找到邮箱 {email} 的 token,无法获取验证码")
|
token = self._load_token_from_db(email)
|
||||||
return None
|
if not token:
|
||||||
|
logger.warning(f"未找到邮箱 {email} 的 token,无法获取验证码")
|
||||||
|
return None
|
||||||
|
|
||||||
if not token:
|
if not token:
|
||||||
logger.warning(f"邮箱 {email} 没有 token,无法获取验证码")
|
logger.warning(f"邮箱 {email} 没有 token,无法获取验证码")
|
||||||
|
|||||||
Reference in New Issue
Block a user