mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-05-06 20:02:51 +08:00
feat: 新增 freemail 邮箱服务渠道支持
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -25,6 +25,7 @@ wheels/
|
|||||||
venv/
|
venv/
|
||||||
ENV/
|
ENV/
|
||||||
env/
|
env/
|
||||||
|
uv.lock
|
||||||
|
|
||||||
# IDE
|
# IDE
|
||||||
.idea/
|
.idea/
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ class EmailServiceType(str, Enum):
|
|||||||
CUSTOM_DOMAIN = "custom_domain"
|
CUSTOM_DOMAIN = "custom_domain"
|
||||||
TEMP_MAIL = "temp_mail"
|
TEMP_MAIL = "temp_mail"
|
||||||
DUCK_MAIL = "duck_mail"
|
DUCK_MAIL = "duck_mail"
|
||||||
|
FREEMAIL = "freemail"
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
@@ -120,6 +121,13 @@ EMAIL_SERVICE_DEFAULTS = {
|
|||||||
"password_length": 12,
|
"password_length": 12,
|
||||||
"timeout": 30,
|
"timeout": 30,
|
||||||
"max_retries": 3,
|
"max_retries": 3,
|
||||||
|
},
|
||||||
|
"freemail": {
|
||||||
|
"base_url": "",
|
||||||
|
"admin_token": "",
|
||||||
|
"domain": "",
|
||||||
|
"timeout": 30,
|
||||||
|
"max_retries": 3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ from .outlook import OutlookService
|
|||||||
from .moe_mail import MeoMailEmailService
|
from .moe_mail import MeoMailEmailService
|
||||||
from .temp_mail import TempMailService
|
from .temp_mail import TempMailService
|
||||||
from .duck_mail import DuckMailService
|
from .duck_mail import DuckMailService
|
||||||
|
from .freemail import FreemailService
|
||||||
|
|
||||||
# 注册服务
|
# 注册服务
|
||||||
EmailServiceFactory.register(EmailServiceType.TEMPMAIL, TempmailService)
|
EmailServiceFactory.register(EmailServiceType.TEMPMAIL, TempmailService)
|
||||||
@@ -22,6 +23,7 @@ EmailServiceFactory.register(EmailServiceType.OUTLOOK, OutlookService)
|
|||||||
EmailServiceFactory.register(EmailServiceType.CUSTOM_DOMAIN, MeoMailEmailService)
|
EmailServiceFactory.register(EmailServiceType.CUSTOM_DOMAIN, MeoMailEmailService)
|
||||||
EmailServiceFactory.register(EmailServiceType.TEMP_MAIL, TempMailService)
|
EmailServiceFactory.register(EmailServiceType.TEMP_MAIL, TempMailService)
|
||||||
EmailServiceFactory.register(EmailServiceType.DUCK_MAIL, DuckMailService)
|
EmailServiceFactory.register(EmailServiceType.DUCK_MAIL, DuckMailService)
|
||||||
|
EmailServiceFactory.register(EmailServiceType.FREEMAIL, FreemailService)
|
||||||
|
|
||||||
# 导出 Outlook 模块的额外内容
|
# 导出 Outlook 模块的额外内容
|
||||||
from .outlook.base import (
|
from .outlook.base import (
|
||||||
@@ -53,6 +55,7 @@ __all__ = [
|
|||||||
'MeoMailEmailService',
|
'MeoMailEmailService',
|
||||||
'TempMailService',
|
'TempMailService',
|
||||||
'DuckMailService',
|
'DuckMailService',
|
||||||
|
'FreemailService',
|
||||||
# Outlook 模块
|
# Outlook 模块
|
||||||
'ProviderType',
|
'ProviderType',
|
||||||
'EmailMessage',
|
'EmailMessage',
|
||||||
|
|||||||
324
src/services/freemail.py
Normal file
324
src/services/freemail.py
Normal file
@@ -0,0 +1,324 @@
|
|||||||
|
"""
|
||||||
|
Freemail 邮箱服务实现
|
||||||
|
基于自部署 Cloudflare Worker 临时邮箱服务 (https://github.com/idinging/freemail)
|
||||||
|
"""
|
||||||
|
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
from typing import Optional, Dict, Any, List
|
||||||
|
|
||||||
|
from .base import BaseEmailService, EmailServiceError, EmailServiceType
|
||||||
|
from ..core.http_client import HTTPClient, RequestConfig
|
||||||
|
from ..config.constants import OTP_CODE_PATTERN
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class FreemailService(BaseEmailService):
|
||||||
|
"""
|
||||||
|
Freemail 邮箱服务
|
||||||
|
基于自部署 Cloudflare Worker 的临时邮箱
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, config: Dict[str, Any] = None, name: str = None):
|
||||||
|
"""
|
||||||
|
初始化 Freemail 服务
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config: 配置字典,支持以下键:
|
||||||
|
- base_url: Worker 域名地址 (必需)
|
||||||
|
- admin_token: Admin Token,对应 JWT_TOKEN (必需)
|
||||||
|
- domain: 邮箱域名,如 example.com
|
||||||
|
- timeout: 请求超时时间,默认 30
|
||||||
|
- max_retries: 最大重试次数,默认 3
|
||||||
|
name: 服务名称
|
||||||
|
"""
|
||||||
|
super().__init__(EmailServiceType.FREEMAIL, name)
|
||||||
|
|
||||||
|
required_keys = ["base_url", "admin_token"]
|
||||||
|
missing_keys = [key for key in required_keys if not (config or {}).get(key)]
|
||||||
|
if missing_keys:
|
||||||
|
raise ValueError(f"缺少必需配置: {missing_keys}")
|
||||||
|
|
||||||
|
default_config = {
|
||||||
|
"timeout": 30,
|
||||||
|
"max_retries": 3,
|
||||||
|
}
|
||||||
|
self.config = {**default_config, **(config or {})}
|
||||||
|
self.config["base_url"] = self.config["base_url"].rstrip("/")
|
||||||
|
|
||||||
|
http_config = RequestConfig(
|
||||||
|
timeout=self.config["timeout"],
|
||||||
|
max_retries=self.config["max_retries"],
|
||||||
|
)
|
||||||
|
self.http_client = HTTPClient(proxy_url=None, config=http_config)
|
||||||
|
|
||||||
|
# 缓存 domain 列表
|
||||||
|
self._domains = []
|
||||||
|
|
||||||
|
def _get_headers(self) -> Dict[str, str]:
|
||||||
|
"""构造 admin 请求头"""
|
||||||
|
return {
|
||||||
|
"Authorization": f"Bearer {self.config['admin_token']}",
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Accept": "application/json",
|
||||||
|
}
|
||||||
|
|
||||||
|
def _make_request(self, method: str, path: str, **kwargs) -> Any:
|
||||||
|
"""
|
||||||
|
发送请求并返回 JSON 数据
|
||||||
|
|
||||||
|
Args:
|
||||||
|
method: HTTP 方法
|
||||||
|
path: 请求路径(以 / 开头)
|
||||||
|
**kwargs: 传递给 http_client.request 的额外参数
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
响应 JSON 数据
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
EmailServiceError: 请求失败
|
||||||
|
"""
|
||||||
|
url = f"{self.config['base_url']}{path}"
|
||||||
|
kwargs.setdefault("headers", {})
|
||||||
|
kwargs["headers"].update(self._get_headers())
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = self.http_client.request(method, url, **kwargs)
|
||||||
|
|
||||||
|
if response.status_code >= 400:
|
||||||
|
error_msg = f"请求失败: {response.status_code}"
|
||||||
|
try:
|
||||||
|
error_data = response.json()
|
||||||
|
error_msg = f"{error_msg} - {error_data}"
|
||||||
|
except Exception:
|
||||||
|
error_msg = f"{error_msg} - {response.text[:200]}"
|
||||||
|
self.update_status(False, EmailServiceError(error_msg))
|
||||||
|
raise EmailServiceError(error_msg)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return response.json()
|
||||||
|
except Exception:
|
||||||
|
return {"raw_response": response.text}
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.update_status(False, e)
|
||||||
|
if isinstance(e, EmailServiceError):
|
||||||
|
raise
|
||||||
|
raise EmailServiceError(f"请求失败: {method} {path} - {e}")
|
||||||
|
|
||||||
|
def _ensure_domains(self):
|
||||||
|
"""获取并缓存可用域名列表"""
|
||||||
|
if not self._domains:
|
||||||
|
try:
|
||||||
|
domains = self._make_request("GET", "/api/domains")
|
||||||
|
if isinstance(domains, list):
|
||||||
|
self._domains = domains
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"获取 Freemail 域名列表失败: {e}")
|
||||||
|
|
||||||
|
def create_email(self, config: Dict[str, Any] = None) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
通过 API 创建临时邮箱
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
包含邮箱信息的字典:
|
||||||
|
- email: 邮箱地址
|
||||||
|
- service_id: 同 email(用作标识)
|
||||||
|
"""
|
||||||
|
self._ensure_domains()
|
||||||
|
|
||||||
|
req_config = config or {}
|
||||||
|
domain_index = 0
|
||||||
|
target_domain = req_config.get("domain") or self.config.get("domain")
|
||||||
|
|
||||||
|
if target_domain and self._domains:
|
||||||
|
for i, d in enumerate(self._domains):
|
||||||
|
if d == target_domain:
|
||||||
|
domain_index = i
|
||||||
|
break
|
||||||
|
|
||||||
|
prefix = req_config.get("name")
|
||||||
|
try:
|
||||||
|
if prefix:
|
||||||
|
body = {
|
||||||
|
"local": prefix,
|
||||||
|
"domainIndex": domain_index
|
||||||
|
}
|
||||||
|
resp = self._make_request("POST", "/api/create", json=body)
|
||||||
|
else:
|
||||||
|
params = {"domainIndex": domain_index}
|
||||||
|
length = req_config.get("length")
|
||||||
|
if length:
|
||||||
|
params["length"] = length
|
||||||
|
resp = self._make_request("GET", "/api/generate", params=params)
|
||||||
|
|
||||||
|
email = resp.get("email")
|
||||||
|
if not email:
|
||||||
|
raise EmailServiceError(f"创建邮箱失败,未返回邮箱地址: {resp}")
|
||||||
|
|
||||||
|
email_info = {
|
||||||
|
"email": email,
|
||||||
|
"service_id": email,
|
||||||
|
"id": email,
|
||||||
|
"created_at": time.time(),
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(f"成功创建 Freemail 邮箱: {email}")
|
||||||
|
self.update_status(True)
|
||||||
|
return email_info
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
self.update_status(False, e)
|
||||||
|
if isinstance(e, EmailServiceError):
|
||||||
|
raise
|
||||||
|
raise EmailServiceError(f"创建邮箱失败: {e}")
|
||||||
|
|
||||||
|
def get_verification_code(
|
||||||
|
self,
|
||||||
|
email: str,
|
||||||
|
email_id: str = None,
|
||||||
|
timeout: int = 120,
|
||||||
|
pattern: str = OTP_CODE_PATTERN,
|
||||||
|
otp_sent_at: Optional[float] = None,
|
||||||
|
) -> Optional[str]:
|
||||||
|
"""
|
||||||
|
从 Freemail 邮箱获取验证码
|
||||||
|
|
||||||
|
Args:
|
||||||
|
email: 邮箱地址
|
||||||
|
email_id: 未使用,保留接口兼容
|
||||||
|
timeout: 超时时间(秒)
|
||||||
|
pattern: 验证码正则
|
||||||
|
otp_sent_at: OTP 发送时间戳(暂未使用)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
验证码字符串,超时返回 None
|
||||||
|
"""
|
||||||
|
logger.info(f"正在从 Freemail 邮箱 {email} 获取验证码...")
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
seen_mail_ids: set = set()
|
||||||
|
|
||||||
|
while time.time() - start_time < timeout:
|
||||||
|
try:
|
||||||
|
mails = self._make_request("GET", "/api/emails", params={"mailbox": email, "limit": 20})
|
||||||
|
if not isinstance(mails, list):
|
||||||
|
time.sleep(3)
|
||||||
|
continue
|
||||||
|
|
||||||
|
for mail in mails:
|
||||||
|
mail_id = mail.get("id")
|
||||||
|
if not mail_id or mail_id in seen_mail_ids:
|
||||||
|
continue
|
||||||
|
|
||||||
|
seen_mail_ids.add(mail_id)
|
||||||
|
|
||||||
|
sender = str(mail.get("sender", "")).lower()
|
||||||
|
subject = str(mail.get("subject", ""))
|
||||||
|
preview = str(mail.get("preview", ""))
|
||||||
|
|
||||||
|
content = f"{sender}\n{subject}\n{preview}"
|
||||||
|
|
||||||
|
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)
|
||||||
|
logger.info(f"从 Freemail 邮箱 {email} 找到验证码: {code}")
|
||||||
|
self.update_status(True)
|
||||||
|
return code
|
||||||
|
|
||||||
|
# 如果依然未找到,获取邮件详情进行匹配
|
||||||
|
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)
|
||||||
|
logger.info(f"从 Freemail 邮箱 {email} 找到验证码: {code}")
|
||||||
|
self.update_status(True)
|
||||||
|
return code
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"获取 Freemail 邮件详情失败: {e}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"检查 Freemail 邮件时出错: {e}")
|
||||||
|
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
|
logger.warning(f"等待 Freemail 验证码超时: {email}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
def list_emails(self, **kwargs) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
列出邮箱
|
||||||
|
|
||||||
|
Args:
|
||||||
|
**kwargs: 额外查询参数
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
邮箱列表
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
params = {
|
||||||
|
"limit": kwargs.get("limit", 100),
|
||||||
|
"offset": kwargs.get("offset", 0)
|
||||||
|
}
|
||||||
|
resp = self._make_request("GET", "/api/mailboxes", params=params)
|
||||||
|
|
||||||
|
emails = []
|
||||||
|
if isinstance(resp, list):
|
||||||
|
for mail in resp:
|
||||||
|
address = mail.get("address")
|
||||||
|
if address:
|
||||||
|
emails.append({
|
||||||
|
"id": address,
|
||||||
|
"service_id": address,
|
||||||
|
"email": address,
|
||||||
|
"created_at": mail.get("created_at"),
|
||||||
|
"raw_data": mail
|
||||||
|
})
|
||||||
|
self.update_status(True)
|
||||||
|
return emails
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"列出 Freemail 邮箱失败: {e}")
|
||||||
|
self.update_status(False, e)
|
||||||
|
return []
|
||||||
|
|
||||||
|
def delete_email(self, email_id: str) -> bool:
|
||||||
|
"""
|
||||||
|
删除邮箱
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
self._make_request("DELETE", "/api/mailboxes", params={"address": email_id})
|
||||||
|
logger.info(f"已删除 Freemail 邮箱: {email_id}")
|
||||||
|
self.update_status(True)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"删除 Freemail 邮箱失败: {e}")
|
||||||
|
self.update_status(False, e)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def check_health(self) -> bool:
|
||||||
|
"""检查服务健康状态"""
|
||||||
|
try:
|
||||||
|
self._make_request("GET", "/api/domains")
|
||||||
|
self.update_status(True)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Freemail 健康检查失败: {e}")
|
||||||
|
self.update_status(False, e)
|
||||||
|
return False
|
||||||
@@ -84,7 +84,7 @@ class OutlookBatchImportResponse(BaseModel):
|
|||||||
# ============== Helper Functions ==============
|
# ============== Helper Functions ==============
|
||||||
|
|
||||||
# 敏感字段列表,返回响应时需要过滤
|
# 敏感字段列表,返回响应时需要过滤
|
||||||
SENSITIVE_FIELDS = {'password', 'api_key', 'refresh_token', 'access_token'}
|
SENSITIVE_FIELDS = {'password', 'api_key', 'refresh_token', 'access_token', 'admin_token'}
|
||||||
|
|
||||||
def filter_sensitive_config(config: Dict[str, Any]) -> Dict[str, Any]:
|
def filter_sensitive_config(config: Dict[str, Any]) -> Dict[str, Any]:
|
||||||
"""过滤敏感配置信息"""
|
"""过滤敏感配置信息"""
|
||||||
@@ -145,6 +145,7 @@ async def get_email_services_stats():
|
|||||||
'custom_count': 0,
|
'custom_count': 0,
|
||||||
'temp_mail_count': 0,
|
'temp_mail_count': 0,
|
||||||
'duck_mail_count': 0,
|
'duck_mail_count': 0,
|
||||||
|
'freemail_count': 0,
|
||||||
'tempmail_available': True, # 临时邮箱始终可用
|
'tempmail_available': True, # 临时邮箱始终可用
|
||||||
'enabled_count': enabled_count
|
'enabled_count': enabled_count
|
||||||
}
|
}
|
||||||
@@ -158,6 +159,8 @@ async def get_email_services_stats():
|
|||||||
stats['temp_mail_count'] = count
|
stats['temp_mail_count'] = count
|
||||||
elif service_type == 'duck_mail':
|
elif service_type == 'duck_mail':
|
||||||
stats['duck_mail_count'] = count
|
stats['duck_mail_count'] = count
|
||||||
|
elif service_type == 'freemail':
|
||||||
|
stats['freemail_count'] = count
|
||||||
|
|
||||||
return stats
|
return stats
|
||||||
|
|
||||||
@@ -218,6 +221,16 @@ async def get_service_types():
|
|||||||
{"name": "api_key", "label": "API Key", "required": False, "secret": True},
|
{"name": "api_key", "label": "API Key", "required": False, "secret": True},
|
||||||
{"name": "password_length", "label": "随机密码长度", "required": False, "default": 12},
|
{"name": "password_length", "label": "随机密码长度", "required": False, "default": 12},
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"value": "freemail",
|
||||||
|
"label": "Freemail",
|
||||||
|
"description": "Freemail 自部署 Cloudflare Worker 临时邮箱服务",
|
||||||
|
"config_fields": [
|
||||||
|
{"name": "base_url", "label": "API 地址", "required": True, "placeholder": "https://freemail.example.com"},
|
||||||
|
{"name": "admin_token", "label": "Admin Token", "required": True, "secret": True},
|
||||||
|
{"name": "domain", "label": "邮箱域名", "required": False, "placeholder": "example.com"},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ def _normalize_email_service_config(
|
|||||||
if service_type == EmailServiceType.CUSTOM_DOMAIN:
|
if service_type == EmailServiceType.CUSTOM_DOMAIN:
|
||||||
if 'domain' in normalized and 'default_domain' not in normalized:
|
if 'domain' in normalized and 'default_domain' not in normalized:
|
||||||
normalized['default_domain'] = normalized.pop('domain')
|
normalized['default_domain'] = normalized.pop('domain')
|
||||||
elif service_type == EmailServiceType.TEMP_MAIL:
|
elif service_type in (EmailServiceType.TEMP_MAIL, EmailServiceType.FREEMAIL):
|
||||||
if 'default_domain' in normalized and 'domain' not in normalized:
|
if 'default_domain' in normalized and 'domain' not in normalized:
|
||||||
normalized['domain'] = normalized.pop('default_domain')
|
normalized['domain'] = normalized.pop('default_domain')
|
||||||
elif service_type == EmailServiceType.DUCK_MAIL:
|
elif service_type == EmailServiceType.DUCK_MAIL:
|
||||||
@@ -358,6 +358,20 @@ def _run_sync_registration_task(task_uuid: str, email_service_type: str, proxy:
|
|||||||
logger.info(f"使用数据库 DuckMail 服务: {db_service.name}")
|
logger.info(f"使用数据库 DuckMail 服务: {db_service.name}")
|
||||||
else:
|
else:
|
||||||
raise ValueError("没有可用的 DuckMail 邮箱服务,请先在邮箱服务页面添加服务")
|
raise ValueError("没有可用的 DuckMail 邮箱服务,请先在邮箱服务页面添加服务")
|
||||||
|
elif service_type == EmailServiceType.FREEMAIL:
|
||||||
|
from ...database.models import EmailService as EmailServiceModel
|
||||||
|
|
||||||
|
db_service = db.query(EmailServiceModel).filter(
|
||||||
|
EmailServiceModel.service_type == "freemail",
|
||||||
|
EmailServiceModel.enabled == True
|
||||||
|
).order_by(EmailServiceModel.priority.asc()).first()
|
||||||
|
|
||||||
|
if db_service and db_service.config:
|
||||||
|
config = _normalize_email_service_config(service_type, db_service.config, actual_proxy_url)
|
||||||
|
crud.update_registration_task(db, task_uuid, email_service_id=db_service.id)
|
||||||
|
logger.info(f"使用数据库 Freemail 服务: {db_service.name}")
|
||||||
|
else:
|
||||||
|
raise ValueError("没有可用的 Freemail 邮箱服务,请先在邮箱服务页面添加服务")
|
||||||
else:
|
else:
|
||||||
config = email_service_config or {}
|
config = email_service_config or {}
|
||||||
|
|
||||||
@@ -1091,6 +1105,11 @@ async def get_available_email_services():
|
|||||||
"available": False,
|
"available": False,
|
||||||
"count": 0,
|
"count": 0,
|
||||||
"services": []
|
"services": []
|
||||||
|
},
|
||||||
|
"freemail": {
|
||||||
|
"available": False,
|
||||||
|
"count": 0,
|
||||||
|
"services": []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1182,6 +1201,24 @@ async def get_available_email_services():
|
|||||||
result["duck_mail"]["count"] = len(duck_mail_services)
|
result["duck_mail"]["count"] = len(duck_mail_services)
|
||||||
result["duck_mail"]["available"] = len(duck_mail_services) > 0
|
result["duck_mail"]["available"] = len(duck_mail_services) > 0
|
||||||
|
|
||||||
|
freemail_services = db.query(EmailServiceModel).filter(
|
||||||
|
EmailServiceModel.service_type == "freemail",
|
||||||
|
EmailServiceModel.enabled == True
|
||||||
|
).order_by(EmailServiceModel.priority.asc()).all()
|
||||||
|
|
||||||
|
for service in freemail_services:
|
||||||
|
config = service.config or {}
|
||||||
|
result["freemail"]["services"].append({
|
||||||
|
"id": service.id,
|
||||||
|
"name": service.name,
|
||||||
|
"type": "freemail",
|
||||||
|
"domain": config.get("domain"),
|
||||||
|
"priority": service.priority
|
||||||
|
})
|
||||||
|
|
||||||
|
result["freemail"]["count"] = len(freemail_services)
|
||||||
|
result["freemail"]["available"] = len(freemail_services) > 0
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ let availableServices = {
|
|||||||
outlook: { available: false, services: [] },
|
outlook: { available: false, services: [] },
|
||||||
custom_domain: { available: false, services: [] },
|
custom_domain: { available: false, services: [] },
|
||||||
temp_mail: { available: false, services: [] },
|
temp_mail: { available: false, services: [] },
|
||||||
duck_mail: { available: false, services: [] }
|
duck_mail: { available: false, services: [] },
|
||||||
|
freemail: { available: false, services: [] }
|
||||||
};
|
};
|
||||||
|
|
||||||
// WebSocket 相关变量
|
// WebSocket 相关变量
|
||||||
@@ -354,6 +355,23 @@ function updateEmailServiceOptions() {
|
|||||||
|
|
||||||
select.appendChild(optgroup);
|
select.appendChild(optgroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Freemail
|
||||||
|
if (availableServices.freemail && availableServices.freemail.available) {
|
||||||
|
const optgroup = document.createElement('optgroup');
|
||||||
|
optgroup.label = `📧 Freemail (${availableServices.freemail.count} 个服务)`;
|
||||||
|
|
||||||
|
availableServices.freemail.services.forEach(service => {
|
||||||
|
const option = document.createElement('option');
|
||||||
|
option.value = `freemail:${service.id}`;
|
||||||
|
option.textContent = service.name + (service.domain ? ` (@${service.domain})` : '');
|
||||||
|
option.dataset.type = 'freemail';
|
||||||
|
option.dataset.serviceId = service.id;
|
||||||
|
optgroup.appendChild(option);
|
||||||
|
});
|
||||||
|
|
||||||
|
select.appendChild(optgroup);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理邮箱服务切换
|
// 处理邮箱服务切换
|
||||||
@@ -399,6 +417,11 @@ function handleServiceChange(e) {
|
|||||||
if (service) {
|
if (service) {
|
||||||
addLog('info', `[系统] 已选择 DuckMail 服务: ${service.name}`);
|
addLog('info', `[系统] 已选择 DuckMail 服务: ${service.name}`);
|
||||||
}
|
}
|
||||||
|
} else if (type === 'freemail') {
|
||||||
|
const service = availableServices.freemail.services.find(s => s.id == id);
|
||||||
|
if (service) {
|
||||||
|
addLog('info', `[系统] 已选择 Freemail 服务: ${service.name}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ const elements = {
|
|||||||
addMoemailFields: document.getElementById('add-moemail-fields'),
|
addMoemailFields: document.getElementById('add-moemail-fields'),
|
||||||
addTempmailFields: document.getElementById('add-tempmail-fields'),
|
addTempmailFields: document.getElementById('add-tempmail-fields'),
|
||||||
addDuckmailFields: document.getElementById('add-duckmail-fields'),
|
addDuckmailFields: document.getElementById('add-duckmail-fields'),
|
||||||
|
addFreemailFields: document.getElementById('add-freemail-fields'),
|
||||||
|
|
||||||
// 编辑自定义域名模态框
|
// 编辑自定义域名模态框
|
||||||
editCustomModal: document.getElementById('edit-custom-modal'),
|
editCustomModal: document.getElementById('edit-custom-modal'),
|
||||||
@@ -60,6 +61,7 @@ const elements = {
|
|||||||
editMoemailFields: document.getElementById('edit-moemail-fields'),
|
editMoemailFields: document.getElementById('edit-moemail-fields'),
|
||||||
editTempmailFields: document.getElementById('edit-tempmail-fields'),
|
editTempmailFields: document.getElementById('edit-tempmail-fields'),
|
||||||
editDuckmailFields: document.getElementById('edit-duckmail-fields'),
|
editDuckmailFields: document.getElementById('edit-duckmail-fields'),
|
||||||
|
editFreemailFields: document.getElementById('edit-freemail-fields'),
|
||||||
editCustomTypeBadge: document.getElementById('edit-custom-type-badge'),
|
editCustomTypeBadge: document.getElementById('edit-custom-type-badge'),
|
||||||
editCustomSubTypeHidden: document.getElementById('edit-custom-sub-type-hidden'),
|
editCustomSubTypeHidden: document.getElementById('edit-custom-sub-type-hidden'),
|
||||||
|
|
||||||
@@ -73,7 +75,8 @@ const elements = {
|
|||||||
const CUSTOM_SUBTYPE_LABELS = {
|
const CUSTOM_SUBTYPE_LABELS = {
|
||||||
moemail: '🔗 MoeMail(自定义域名 API)',
|
moemail: '🔗 MoeMail(自定义域名 API)',
|
||||||
tempmail: '📮 TempMail(自部署 Cloudflare Worker)',
|
tempmail: '📮 TempMail(自部署 Cloudflare Worker)',
|
||||||
duckmail: '🦆 DuckMail(DuckMail API)'
|
duckmail: '🦆 DuckMail(DuckMail API)',
|
||||||
|
freemail: 'Freemail(自部署 Cloudflare Worker)'
|
||||||
};
|
};
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
@@ -178,6 +181,7 @@ function switchAddSubType(subType) {
|
|||||||
elements.addMoemailFields.style.display = subType === 'moemail' ? '' : 'none';
|
elements.addMoemailFields.style.display = subType === 'moemail' ? '' : 'none';
|
||||||
elements.addTempmailFields.style.display = subType === 'tempmail' ? '' : 'none';
|
elements.addTempmailFields.style.display = subType === 'tempmail' ? '' : 'none';
|
||||||
elements.addDuckmailFields.style.display = subType === 'duckmail' ? '' : 'none';
|
elements.addDuckmailFields.style.display = subType === 'duckmail' ? '' : 'none';
|
||||||
|
elements.addFreemailFields.style.display = subType === 'freemail' ? '' : 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 切换编辑表单子类型显示
|
// 切换编辑表单子类型显示
|
||||||
@@ -186,6 +190,7 @@ function switchEditSubType(subType) {
|
|||||||
elements.editMoemailFields.style.display = subType === 'moemail' ? '' : 'none';
|
elements.editMoemailFields.style.display = subType === 'moemail' ? '' : 'none';
|
||||||
elements.editTempmailFields.style.display = subType === 'tempmail' ? '' : 'none';
|
elements.editTempmailFields.style.display = subType === 'tempmail' ? '' : 'none';
|
||||||
elements.editDuckmailFields.style.display = subType === 'duckmail' ? '' : 'none';
|
elements.editDuckmailFields.style.display = subType === 'duckmail' ? '' : 'none';
|
||||||
|
elements.editFreemailFields.style.display = subType === 'freemail' ? '' : 'none';
|
||||||
elements.editCustomTypeBadge.textContent = CUSTOM_SUBTYPE_LABELS[subType] || CUSTOM_SUBTYPE_LABELS.moemail;
|
elements.editCustomTypeBadge.textContent = CUSTOM_SUBTYPE_LABELS[subType] || CUSTOM_SUBTYPE_LABELS.moemail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +199,7 @@ async function loadStats() {
|
|||||||
try {
|
try {
|
||||||
const data = await api.get('/email-services/stats');
|
const data = await api.get('/email-services/stats');
|
||||||
elements.outlookCount.textContent = data.outlook_count || 0;
|
elements.outlookCount.textContent = data.outlook_count || 0;
|
||||||
elements.customCount.textContent = (data.custom_count || 0) + (data.temp_mail_count || 0) + (data.duck_mail_count || 0);
|
elements.customCount.textContent = (data.custom_count || 0) + (data.temp_mail_count || 0) + (data.duck_mail_count || 0) + (data.freemail_count || 0);
|
||||||
elements.tempmailStatus.textContent = data.tempmail_available ? '可用' : '不可用';
|
elements.tempmailStatus.textContent = data.tempmail_available ? '可用' : '不可用';
|
||||||
elements.totalEnabled.textContent = data.enabled_count || 0;
|
elements.totalEnabled.textContent = data.enabled_count || 0;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -273,7 +278,10 @@ function getCustomServiceTypeBadge(subType) {
|
|||||||
if (subType === 'tempmail') {
|
if (subType === 'tempmail') {
|
||||||
return '<span class="status-badge warning">TempMail</span>';
|
return '<span class="status-badge warning">TempMail</span>';
|
||||||
}
|
}
|
||||||
return '<span class="status-badge success">DuckMail</span>';
|
if (subType === 'duckmail') {
|
||||||
|
return '<span class="status-badge success">DuckMail</span>';
|
||||||
|
}
|
||||||
|
return '<span class="status-badge" style="background-color:#9c27b0;color:white;">Freemail</span>';
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCustomServiceAddress(service) {
|
function getCustomServiceAddress(service) {
|
||||||
@@ -285,18 +293,20 @@ function getCustomServiceAddress(service) {
|
|||||||
return `${escapeHtml(baseUrl)}<div style="color: var(--text-muted); margin-top: 4px;">默认域名:@${escapeHtml(domain)}</div>`;
|
return `${escapeHtml(baseUrl)}<div style="color: var(--text-muted); margin-top: 4px;">默认域名:@${escapeHtml(domain)}</div>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加载自定义邮箱服务(custom_domain + temp_mail + duck_mail 合并)
|
// 加载自定义邮箱服务(custom_domain + temp_mail + duck_mail + freemail 合并)
|
||||||
async function loadCustomServices() {
|
async function loadCustomServices() {
|
||||||
try {
|
try {
|
||||||
const [r1, r2, r3] = await Promise.all([
|
const [r1, r2, r3, r4] = await Promise.all([
|
||||||
api.get('/email-services?service_type=custom_domain'),
|
api.get('/email-services?service_type=custom_domain'),
|
||||||
api.get('/email-services?service_type=temp_mail'),
|
api.get('/email-services?service_type=temp_mail'),
|
||||||
api.get('/email-services?service_type=duck_mail')
|
api.get('/email-services?service_type=duck_mail'),
|
||||||
|
api.get('/email-services?service_type=freemail')
|
||||||
]);
|
]);
|
||||||
customServices = [
|
customServices = [
|
||||||
...(r1.services || []).map(s => ({ ...s, _subType: 'moemail' })),
|
...(r1.services || []).map(s => ({ ...s, _subType: 'moemail' })),
|
||||||
...(r2.services || []).map(s => ({ ...s, _subType: 'tempmail' })),
|
...(r2.services || []).map(s => ({ ...s, _subType: 'tempmail' })),
|
||||||
...(r3.services || []).map(s => ({ ...s, _subType: 'duckmail' }))
|
...(r3.services || []).map(s => ({ ...s, _subType: 'duckmail' })),
|
||||||
|
...(r4.services || []).map(s => ({ ...s, _subType: 'freemail' }))
|
||||||
];
|
];
|
||||||
|
|
||||||
if (customServices.length === 0) {
|
if (customServices.length === 0) {
|
||||||
@@ -426,7 +436,7 @@ async function handleAddCustom(e) {
|
|||||||
domain: formData.get('tm_domain'),
|
domain: formData.get('tm_domain'),
|
||||||
enable_prefix: true
|
enable_prefix: true
|
||||||
};
|
};
|
||||||
} else {
|
} else if (subType === 'duckmail') {
|
||||||
serviceType = 'duck_mail';
|
serviceType = 'duck_mail';
|
||||||
config = {
|
config = {
|
||||||
base_url: formData.get('dm_base_url'),
|
base_url: formData.get('dm_base_url'),
|
||||||
@@ -434,6 +444,13 @@ async function handleAddCustom(e) {
|
|||||||
default_domain: formData.get('dm_domain'),
|
default_domain: formData.get('dm_domain'),
|
||||||
password_length: parseInt(formData.get('dm_password_length'), 10) || 12
|
password_length: parseInt(formData.get('dm_password_length'), 10) || 12
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
serviceType = 'freemail';
|
||||||
|
config = {
|
||||||
|
base_url: formData.get('fm_base_url'),
|
||||||
|
admin_token: formData.get('fm_admin_token'),
|
||||||
|
domain: formData.get('fm_domain')
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
@@ -574,7 +591,9 @@ async function editCustomService(id, subType) {
|
|||||||
? 'tempmail'
|
? 'tempmail'
|
||||||
: service.service_type === 'duck_mail'
|
: service.service_type === 'duck_mail'
|
||||||
? 'duckmail'
|
? 'duckmail'
|
||||||
: 'moemail'
|
: service.service_type === 'freemail'
|
||||||
|
? 'freemail'
|
||||||
|
: 'moemail'
|
||||||
);
|
);
|
||||||
|
|
||||||
document.getElementById('edit-custom-id').value = service.id;
|
document.getElementById('edit-custom-id').value = service.id;
|
||||||
@@ -594,12 +613,17 @@ async function editCustomService(id, subType) {
|
|||||||
document.getElementById('edit-tm-admin-password').value = '';
|
document.getElementById('edit-tm-admin-password').value = '';
|
||||||
document.getElementById('edit-tm-admin-password').placeholder = service.config?.admin_password ? '已设置,留空保持不变' : '请输入 Admin 密码';
|
document.getElementById('edit-tm-admin-password').placeholder = service.config?.admin_password ? '已设置,留空保持不变' : '请输入 Admin 密码';
|
||||||
document.getElementById('edit-tm-domain').value = service.config?.domain || '';
|
document.getElementById('edit-tm-domain').value = service.config?.domain || '';
|
||||||
} else {
|
} else if (resolvedSubType === 'duckmail') {
|
||||||
document.getElementById('edit-dm-base-url').value = service.config?.base_url || '';
|
document.getElementById('edit-dm-base-url').value = service.config?.base_url || '';
|
||||||
document.getElementById('edit-dm-api-key').value = '';
|
document.getElementById('edit-dm-api-key').value = '';
|
||||||
document.getElementById('edit-dm-api-key').placeholder = service.config?.api_key ? '已设置,留空保持不变' : '请输入 API Key(可选)';
|
document.getElementById('edit-dm-api-key').placeholder = service.config?.api_key ? '已设置,留空保持不变' : '请输入 API Key(可选)';
|
||||||
document.getElementById('edit-dm-domain').value = service.config?.default_domain || '';
|
document.getElementById('edit-dm-domain').value = service.config?.default_domain || '';
|
||||||
document.getElementById('edit-dm-password-length').value = service.config?.password_length || 12;
|
document.getElementById('edit-dm-password-length').value = service.config?.password_length || 12;
|
||||||
|
} else {
|
||||||
|
document.getElementById('edit-fm-base-url').value = service.config?.base_url || '';
|
||||||
|
document.getElementById('edit-fm-admin-token').value = '';
|
||||||
|
document.getElementById('edit-fm-admin-token').placeholder = service.config?.admin_token ? '已设置,留空保持不变' : '请输入 Admin Token';
|
||||||
|
document.getElementById('edit-fm-domain').value = service.config?.domain || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
elements.editCustomModal.classList.add('active');
|
elements.editCustomModal.classList.add('active');
|
||||||
@@ -631,7 +655,7 @@ async function handleEditCustom(e) {
|
|||||||
};
|
};
|
||||||
const pwd = formData.get('tm_admin_password');
|
const pwd = formData.get('tm_admin_password');
|
||||||
if (pwd && pwd.trim()) config.admin_password = pwd.trim();
|
if (pwd && pwd.trim()) config.admin_password = pwd.trim();
|
||||||
} else {
|
} else if (subType === 'duckmail') {
|
||||||
config = {
|
config = {
|
||||||
base_url: formData.get('dm_base_url'),
|
base_url: formData.get('dm_base_url'),
|
||||||
default_domain: formData.get('dm_domain'),
|
default_domain: formData.get('dm_domain'),
|
||||||
@@ -639,6 +663,13 @@ async function handleEditCustom(e) {
|
|||||||
};
|
};
|
||||||
const apiKey = formData.get('dm_api_key');
|
const apiKey = formData.get('dm_api_key');
|
||||||
if (apiKey && apiKey.trim()) config.api_key = apiKey.trim();
|
if (apiKey && apiKey.trim()) config.api_key = apiKey.trim();
|
||||||
|
} else {
|
||||||
|
config = {
|
||||||
|
base_url: formData.get('fm_base_url'),
|
||||||
|
domain: formData.get('fm_domain')
|
||||||
|
};
|
||||||
|
const token = formData.get('fm_admin_token');
|
||||||
|
if (token && token.trim()) config.admin_token = token.trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateData = {
|
const updateData = {
|
||||||
|
|||||||
@@ -352,7 +352,9 @@ const statusMap = {
|
|||||||
tempmail: 'Tempmail.lol',
|
tempmail: 'Tempmail.lol',
|
||||||
outlook: 'Outlook',
|
outlook: 'Outlook',
|
||||||
custom_domain: '自定义域名',
|
custom_domain: '自定义域名',
|
||||||
temp_mail: 'Temp-Mail(自部署)'
|
temp_mail: 'Temp-Mail(自部署)',
|
||||||
|
duck_mail: 'DuckMail',
|
||||||
|
freemail: 'Freemail'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -210,6 +210,7 @@
|
|||||||
<option value="moemail">MoeMail(自定义域名 API)</option>
|
<option value="moemail">MoeMail(自定义域名 API)</option>
|
||||||
<option value="tempmail">TempMail(自部署 Cloudflare Worker)</option>
|
<option value="tempmail">TempMail(自部署 Cloudflare Worker)</option>
|
||||||
<option value="duckmail">DuckMail(DuckMail API)</option>
|
<option value="duckmail">DuckMail(DuckMail API)</option>
|
||||||
|
<option value="freemail">Freemail(自部署 Cloudflare Worker)</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<!-- MoeMail 字段 -->
|
<!-- MoeMail 字段 -->
|
||||||
@@ -261,6 +262,21 @@
|
|||||||
<input type="number" id="custom-dm-password-length" name="dm_password_length" min="6" max="64" value="12">
|
<input type="number" id="custom-dm-password-length" name="dm_password_length" min="6" max="64" value="12">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<!-- Freemail 字段 -->
|
||||||
|
<div id="add-freemail-fields" style="display:none;">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="custom-fm-base-url">Worker 地址</label>
|
||||||
|
<input type="text" id="custom-fm-base-url" name="fm_base_url" placeholder="https://freemail.example.com">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="custom-fm-admin-token">Admin Token</label>
|
||||||
|
<input type="password" id="custom-fm-admin-token" name="fm_admin_token" placeholder="JWT_TOKEN 值">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="custom-fm-domain">邮箱域名 (可选)</label>
|
||||||
|
<input type="text" id="custom-fm-domain" name="fm_domain" placeholder="example.com">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="custom-priority">优先级</label>
|
<label for="custom-priority">优先级</label>
|
||||||
@@ -354,6 +370,22 @@
|
|||||||
<input type="number" id="edit-dm-password-length" name="dm_password_length" min="6" max="64" value="12">
|
<input type="number" id="edit-dm-password-length" name="dm_password_length" min="6" max="64" value="12">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<!-- Freemail 字段 -->
|
||||||
|
<div id="edit-freemail-fields" style="display:none;">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="edit-fm-base-url">Worker 地址</label>
|
||||||
|
<input type="text" id="edit-fm-base-url" name="fm_base_url" placeholder="https://freemail.example.com">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="edit-fm-admin-token">Admin Token</label>
|
||||||
|
<input type="password" id="edit-fm-admin-token" name="fm_admin_token" placeholder="留空则不修改">
|
||||||
|
<small style="color: var(--text-muted);">留空则保持原值不变</small>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="edit-fm-domain">邮箱域名 (可选)</label>
|
||||||
|
<input type="text" id="edit-fm-domain" name="fm_domain" placeholder="example.com">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="edit-custom-priority">优先级</label>
|
<label for="edit-custom-priority">优先级</label>
|
||||||
|
|||||||
Reference in New Issue
Block a user