mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-06-28 10:41:36 +08:00
Merge branch 'master' into fix/temp-mail-registration-flow
This commit is contained in:
@@ -160,6 +160,13 @@ def create_app() -> FastAPI:
|
||||
async def startup_event():
|
||||
"""应用启动事件"""
|
||||
import asyncio
|
||||
from ..database.init_db import initialize_database
|
||||
|
||||
# 确保数据库已初始化(reload 模式下子进程也需要初始化)
|
||||
try:
|
||||
initialize_database()
|
||||
except Exception as e:
|
||||
logger.warning(f"数据库初始化: {e}")
|
||||
|
||||
# 设置 TaskManager 的事件循环
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
@@ -7,9 +7,11 @@ from fastapi import APIRouter
|
||||
from .accounts import router as accounts_router
|
||||
from .registration import router as registration_router
|
||||
from .settings import router as settings_router
|
||||
from .email_services import router as email_services_router
|
||||
from .email import router as email_services_router
|
||||
from .payment import router as payment_router
|
||||
from .cpa_services import router as cpa_services_router
|
||||
from .upload.cpa_services import router as cpa_services_router
|
||||
from .upload.sub2api_services import router as sub2api_services_router
|
||||
from .upload.tm_services import router as tm_services_router
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
@@ -20,3 +22,5 @@ api_router.include_router(settings_router, prefix="/settings", tags=["settings"]
|
||||
api_router.include_router(email_services_router, prefix="/email-services", tags=["email-services"])
|
||||
api_router.include_router(payment_router, prefix="/payment", tags=["payment"])
|
||||
api_router.include_router(cpa_services_router, prefix="/cpa-services", tags=["cpa-services"])
|
||||
api_router.include_router(sub2api_services_router, prefix="/sub2api-services", tags=["sub2api-services"])
|
||||
api_router.include_router(tm_services_router, prefix="/tm-services", tags=["tm-services"])
|
||||
|
||||
@@ -785,3 +785,106 @@ async def batch_upload_accounts_to_cpa(request: BatchCPAUploadRequest):
|
||||
|
||||
results = batch_upload_to_cpa(ids, proxy, api_url=cpa_api_url, api_token=cpa_api_token)
|
||||
return results
|
||||
|
||||
|
||||
class Sub2ApiUploadRequest(BaseModel):
|
||||
"""单账号 Sub2API 上传请求"""
|
||||
service_id: Optional[int] = None
|
||||
concurrency: int = 3
|
||||
priority: int = 50
|
||||
|
||||
|
||||
@router.post("/{account_id}/upload-sub2api")
|
||||
async def upload_account_to_sub2api(account_id: int, request: Sub2ApiUploadRequest = None):
|
||||
"""上传单个账号到 Sub2API"""
|
||||
from ...core.sub2api_upload import upload_to_sub2api
|
||||
|
||||
service_id = request.service_id if request else None
|
||||
concurrency = request.concurrency if request else 3
|
||||
priority = request.priority if request else 50
|
||||
|
||||
api_url = None
|
||||
api_key = None
|
||||
if service_id:
|
||||
with get_db() as db:
|
||||
svc = crud.get_sub2api_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="指定的 Sub2API 服务不存在")
|
||||
api_url = svc.api_url
|
||||
api_key = svc.api_key
|
||||
else:
|
||||
with get_db() as db:
|
||||
svcs = crud.get_sub2api_services(db, enabled=True)
|
||||
if svcs:
|
||||
api_url = svcs[0].api_url
|
||||
api_key = svcs[0].api_key
|
||||
|
||||
if not api_url or not api_key:
|
||||
raise HTTPException(status_code=400, detail="未找到可用的 Sub2API 服务,请先在设置中配置")
|
||||
|
||||
with get_db() as db:
|
||||
account = crud.get_account_by_id(db, account_id)
|
||||
if not account:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
if not account.access_token:
|
||||
return {"success": False, "error": "账号缺少 Token,无法上传"}
|
||||
|
||||
success, message = upload_to_sub2api(
|
||||
[account], api_url, api_key,
|
||||
concurrency=concurrency, priority=priority
|
||||
)
|
||||
if success:
|
||||
return {"success": True, "message": message}
|
||||
else:
|
||||
return {"success": False, "error": message}
|
||||
|
||||
|
||||
class BatchSub2ApiUploadRequest(BaseModel):
|
||||
"""批量 Sub2API 上传请求"""
|
||||
ids: List[int] = []
|
||||
select_all: bool = False
|
||||
status_filter: Optional[str] = None
|
||||
email_service_filter: Optional[str] = None
|
||||
search_filter: Optional[str] = None
|
||||
service_id: Optional[int] = None # 指定 Sub2API 服务 ID,不传则使用第一个启用的
|
||||
concurrency: int = 3
|
||||
priority: int = 50
|
||||
|
||||
|
||||
@router.post("/batch-upload-sub2api")
|
||||
async def batch_upload_accounts_to_sub2api(request: BatchSub2ApiUploadRequest):
|
||||
"""批量上传账号到 Sub2API"""
|
||||
from ...core.sub2api_upload import batch_upload_to_sub2api
|
||||
|
||||
# 解析指定的 Sub2API 服务
|
||||
api_url = None
|
||||
api_key = None
|
||||
if request.service_id:
|
||||
with get_db() as db:
|
||||
svc = crud.get_sub2api_service_by_id(db, request.service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="指定的 Sub2API 服务不存在")
|
||||
api_url = svc.api_url
|
||||
api_key = svc.api_key
|
||||
else:
|
||||
with get_db() as db:
|
||||
svcs = crud.get_sub2api_services(db, enabled=True)
|
||||
if svcs:
|
||||
api_url = svcs[0].api_url
|
||||
api_key = svcs[0].api_key
|
||||
|
||||
if not api_url or not api_key:
|
||||
raise HTTPException(status_code=400, detail="未找到可用的 Sub2API 服务,请先在设置中配置")
|
||||
|
||||
with get_db() as db:
|
||||
ids = resolve_account_ids(
|
||||
db, request.ids, request.select_all,
|
||||
request.status_filter, request.email_service_filter, request.search_filter
|
||||
)
|
||||
|
||||
results = batch_upload_to_sub2api(
|
||||
ids, api_url, api_key,
|
||||
concurrency=request.concurrency,
|
||||
priority=request.priority,
|
||||
)
|
||||
return results
|
||||
|
||||
@@ -11,15 +11,16 @@ from pydantic import BaseModel
|
||||
|
||||
from ...database.session import get_db
|
||||
from ...database.models import Account
|
||||
from ...database import crud
|
||||
from ...config.settings import get_settings
|
||||
from .accounts import resolve_account_ids
|
||||
from ...core.payment import (
|
||||
from ...core.openai.payment import (
|
||||
generate_plus_link,
|
||||
generate_team_link,
|
||||
open_url_incognito,
|
||||
check_subscription_status,
|
||||
)
|
||||
from ...core.team_manager import (
|
||||
from ...core.upload.team_manager_upload import (
|
||||
upload_to_team_manager,
|
||||
batch_upload_to_team_manager,
|
||||
)
|
||||
@@ -61,12 +62,14 @@ class BatchCheckSubscriptionRequest(BaseModel):
|
||||
|
||||
class UploadTMRequest(BaseModel):
|
||||
proxy: Optional[str] = None # 保留,TM 上传不走代理
|
||||
service_id: Optional[int] = None # 指定 TM 服务 ID,不传则使用第一个启用的
|
||||
|
||||
|
||||
class BatchUploadTMRequest(BaseModel):
|
||||
ids: List[int] = []
|
||||
select_all: bool = False
|
||||
status_filter: Optional[str] = None
|
||||
service_id: Optional[int] = None # 指定 TM 服务 ID,不传则使用第一个启用的
|
||||
email_service_filter: Optional[str] = None
|
||||
search_filter: Optional[str] = None
|
||||
|
||||
@@ -200,14 +203,21 @@ def batch_check_subscription(request: BatchCheckSubscriptionRequest):
|
||||
@router.post("/accounts/{account_id}/upload-tm")
|
||||
def upload_account_tm(account_id: int, request: UploadTMRequest = None):
|
||||
"""上传单账号到 Team Manager"""
|
||||
settings = get_settings()
|
||||
if not settings.tm_enabled:
|
||||
raise HTTPException(status_code=400, detail="Team Manager 上传未启用")
|
||||
|
||||
api_url = settings.tm_api_url
|
||||
api_key = settings.tm_api_key.get_secret_value() if settings.tm_api_key else ""
|
||||
service_id = request.service_id if request and hasattr(request, 'service_id') else None
|
||||
|
||||
with get_db() as db:
|
||||
if service_id:
|
||||
svc = crud.get_tm_service_by_id(db, service_id)
|
||||
else:
|
||||
svcs = crud.get_tm_services(db, enabled=True)
|
||||
svc = svcs[0] if svcs else None
|
||||
|
||||
if not svc:
|
||||
raise HTTPException(status_code=400, detail="未找到可用的 Team Manager 服务,请先在设置中配置")
|
||||
|
||||
api_url = svc.api_url
|
||||
api_key = svc.api_key
|
||||
|
||||
account = db.query(Account).filter(Account.id == account_id).first()
|
||||
if not account:
|
||||
raise HTTPException(status_code=404, detail="账号不存在")
|
||||
@@ -219,14 +229,21 @@ def upload_account_tm(account_id: int, request: UploadTMRequest = None):
|
||||
@router.post("/accounts/batch-upload-tm")
|
||||
def batch_upload_tm(request: BatchUploadTMRequest):
|
||||
"""批量上传账号到 Team Manager"""
|
||||
settings = get_settings()
|
||||
if not settings.tm_enabled:
|
||||
raise HTTPException(status_code=400, detail="Team Manager 上传未启用")
|
||||
|
||||
api_url = settings.tm_api_url
|
||||
api_key = settings.tm_api_key.get_secret_value() if settings.tm_api_key else ""
|
||||
service_id = request.service_id if hasattr(request, 'service_id') else None
|
||||
|
||||
with get_db() as db:
|
||||
if service_id:
|
||||
svc = crud.get_tm_service_by_id(db, service_id)
|
||||
else:
|
||||
svcs = crud.get_tm_services(db, enabled=True)
|
||||
svc = svcs[0] if svcs else None
|
||||
|
||||
if not svc:
|
||||
raise HTTPException(status_code=400, detail="未找到可用的 Team Manager 服务,请先在设置中配置")
|
||||
|
||||
api_url = svc.api_url
|
||||
api_key = svc.api_key
|
||||
|
||||
ids = resolve_account_ids(
|
||||
db, request.ids, request.select_all,
|
||||
request.status_filter, request.email_service_filter, request.search_filter
|
||||
|
||||
@@ -70,24 +70,32 @@ class RegistrationTaskCreate(BaseModel):
|
||||
email_service_type: str = "tempmail"
|
||||
proxy: Optional[str] = None
|
||||
email_service_config: Optional[dict] = None
|
||||
email_service_id: Optional[int] = None # 使用数据库中已配置的邮箱服务 ID
|
||||
auto_upload_cpa: bool = False # 注册成功后自动上传到 CPA
|
||||
cpa_service_id: Optional[int] = None # 指定 CPA 服务 ID,不传则使用全局配置
|
||||
email_service_id: Optional[int] = None
|
||||
auto_upload_cpa: bool = False
|
||||
cpa_service_ids: List[int] = [] # 指定 CPA 服务 ID 列表,空则取第一个启用的
|
||||
auto_upload_sub2api: bool = False
|
||||
sub2api_service_ids: List[int] = [] # 指定 Sub2API 服务 ID 列表
|
||||
auto_upload_tm: bool = False
|
||||
tm_service_ids: List[int] = [] # 指定 TM 服务 ID 列表
|
||||
|
||||
|
||||
class BatchRegistrationRequest(BaseModel):
|
||||
"""批量注册请求"""
|
||||
count: int = 1 # 注册数量
|
||||
count: int = 1
|
||||
email_service_type: str = "tempmail"
|
||||
proxy: Optional[str] = None
|
||||
email_service_config: Optional[dict] = None
|
||||
email_service_id: Optional[int] = None # 使用数据库中已配置的邮箱服务 ID
|
||||
interval_min: int = 5 # 最小间隔秒数
|
||||
interval_max: int = 30 # 最大间隔秒数
|
||||
concurrency: int = 1 # 并发线程数 (1-50)
|
||||
mode: str = "pipeline" # 执行模式: "parallel" 或 "pipeline"
|
||||
auto_upload_cpa: bool = False # 注册成功后自动上传到 CPA
|
||||
cpa_service_id: Optional[int] = None # 指定 CPA 服务 ID,不传则使用全局配置
|
||||
email_service_id: Optional[int] = None
|
||||
interval_min: int = 5
|
||||
interval_max: int = 30
|
||||
concurrency: int = 1
|
||||
mode: str = "pipeline"
|
||||
auto_upload_cpa: bool = False
|
||||
cpa_service_ids: List[int] = []
|
||||
auto_upload_sub2api: bool = False
|
||||
sub2api_service_ids: List[int] = []
|
||||
auto_upload_tm: bool = False
|
||||
tm_service_ids: List[int] = []
|
||||
|
||||
|
||||
class RegistrationTaskResponse(BaseModel):
|
||||
@@ -143,15 +151,19 @@ class OutlookAccountsListResponse(BaseModel):
|
||||
|
||||
class OutlookBatchRegistrationRequest(BaseModel):
|
||||
"""Outlook 批量注册请求"""
|
||||
service_ids: List[int] # 选中的 EmailService ID
|
||||
skip_registered: bool = True # 自动跳过已注册邮箱
|
||||
service_ids: List[int]
|
||||
skip_registered: bool = True
|
||||
proxy: Optional[str] = None
|
||||
interval_min: int = 5
|
||||
interval_max: int = 30
|
||||
concurrency: int = 1 # 并发线程数 (1-50)
|
||||
mode: str = "pipeline" # 执行模式: "parallel" 或 "pipeline"
|
||||
auto_upload_cpa: bool = False # 注册成功后自动上传到 CPA
|
||||
cpa_service_id: Optional[int] = None # 指定 CPA 服务 ID,不传则使用全局配置
|
||||
concurrency: int = 1
|
||||
mode: str = "pipeline"
|
||||
auto_upload_cpa: bool = False
|
||||
cpa_service_ids: List[int] = []
|
||||
auto_upload_sub2api: bool = False
|
||||
sub2api_service_ids: List[int] = []
|
||||
auto_upload_tm: bool = False
|
||||
tm_service_ids: List[int] = []
|
||||
|
||||
|
||||
class OutlookBatchRegistrationResponse(BaseModel):
|
||||
@@ -206,7 +218,7 @@ def _normalize_email_service_config(
|
||||
return normalized
|
||||
|
||||
|
||||
def _run_sync_registration_task(task_uuid: str, email_service_type: str, proxy: Optional[str], email_service_config: Optional[dict], email_service_id: Optional[int] = None, log_prefix: str = "", batch_id: str = "", auto_upload_cpa: bool = False, cpa_service_id: Optional[int] = None):
|
||||
def _run_sync_registration_task(task_uuid: str, email_service_type: str, proxy: Optional[str], email_service_config: Optional[dict], email_service_id: Optional[int] = None, log_prefix: str = "", batch_id: str = "", auto_upload_cpa: bool = False, cpa_service_ids: List[int] = None, auto_upload_sub2api: bool = False, sub2api_service_ids: List[int] = None, auto_upload_tm: bool = False, tm_service_ids: List[int] = None):
|
||||
"""
|
||||
在线程池中执行的同步注册任务
|
||||
|
||||
@@ -354,7 +366,7 @@ def _run_sync_registration_task(task_uuid: str, email_service_type: str, proxy:
|
||||
# 保存到数据库
|
||||
engine.save_to_database(result)
|
||||
|
||||
# 自动上传到 CPA
|
||||
# 自动上传到 CPA(可多服务)
|
||||
if auto_upload_cpa:
|
||||
try:
|
||||
from ...core.cpa_upload import upload_to_cpa, generate_token_json
|
||||
@@ -362,33 +374,81 @@ def _run_sync_registration_task(task_uuid: str, email_service_type: str, proxy:
|
||||
saved_account = db.query(AccountModel).filter_by(email=result.email).first()
|
||||
if saved_account and saved_account.access_token:
|
||||
token_data = generate_token_json(saved_account)
|
||||
# 解析指定 CPA 服务,未指定则取第一个启用的服务
|
||||
_cpa_api_url = None
|
||||
_cpa_api_token = None
|
||||
_svc = None
|
||||
if cpa_service_id:
|
||||
_cpa_ids = cpa_service_ids or []
|
||||
if not _cpa_ids:
|
||||
# 未指定则取所有启用的服务
|
||||
_cpa_ids = [s.id for s in crud.get_cpa_services(db, enabled=True)]
|
||||
if not _cpa_ids:
|
||||
log_callback("[CPA] 无可用 CPA 服务,跳过上传")
|
||||
for _sid in _cpa_ids:
|
||||
try:
|
||||
_svc = crud.get_cpa_service_by_id(db, cpa_service_id)
|
||||
except Exception:
|
||||
pass
|
||||
if _svc is None:
|
||||
svcs = crud.get_cpa_services(db, enabled=True)
|
||||
_svc = svcs[0] if svcs else None
|
||||
if _svc:
|
||||
_cpa_api_url = _svc.api_url
|
||||
_cpa_api_token = _svc.api_token
|
||||
log_callback(f"[CPA] 使用服务: {_svc.name}")
|
||||
cpa_success, cpa_msg = upload_to_cpa(token_data, api_url=_cpa_api_url, api_token=_cpa_api_token)
|
||||
if cpa_success:
|
||||
saved_account.cpa_uploaded = True
|
||||
saved_account.cpa_uploaded_at = datetime.utcnow()
|
||||
db.commit()
|
||||
log_callback(f"[CPA] 已自动上传到 CPA: {result.email}")
|
||||
else:
|
||||
log_callback(f"[CPA] 上传失败: {cpa_msg}")
|
||||
_svc = crud.get_cpa_service_by_id(db, _sid)
|
||||
if not _svc:
|
||||
continue
|
||||
log_callback(f"[CPA] 上传到服务: {_svc.name}")
|
||||
_ok, _msg = upload_to_cpa(token_data, api_url=_svc.api_url, api_token=_svc.api_token)
|
||||
if _ok:
|
||||
saved_account.cpa_uploaded = True
|
||||
saved_account.cpa_uploaded_at = datetime.utcnow()
|
||||
db.commit()
|
||||
log_callback(f"[CPA] 上传成功: {_svc.name}")
|
||||
else:
|
||||
log_callback(f"[CPA] 上传失败({_svc.name}): {_msg}")
|
||||
except Exception as _e:
|
||||
log_callback(f"[CPA] 异常({_sid}): {_e}")
|
||||
except Exception as cpa_err:
|
||||
log_callback(f"[CPA] 上传异常: {cpa_err}")
|
||||
|
||||
# 自动上传到 Sub2API(可多服务)
|
||||
if auto_upload_sub2api:
|
||||
try:
|
||||
from ...core.sub2api_upload import upload_to_sub2api
|
||||
from ...database.models import Account as AccountModel
|
||||
saved_account = db.query(AccountModel).filter_by(email=result.email).first()
|
||||
if saved_account and saved_account.access_token:
|
||||
_s2a_ids = sub2api_service_ids or []
|
||||
if not _s2a_ids:
|
||||
_s2a_ids = [s.id for s in crud.get_sub2api_services(db, enabled=True)]
|
||||
if not _s2a_ids:
|
||||
log_callback("[Sub2API] 无可用 Sub2API 服务,跳过上传")
|
||||
for _sid in _s2a_ids:
|
||||
try:
|
||||
_svc = crud.get_sub2api_service_by_id(db, _sid)
|
||||
if not _svc:
|
||||
continue
|
||||
log_callback(f"[Sub2API] 上传到服务: {_svc.name}")
|
||||
_ok, _msg = upload_to_sub2api([saved_account], _svc.api_url, _svc.api_key)
|
||||
log_callback(f"[Sub2API] {'成功' if _ok else '失败'}({_svc.name}): {_msg}")
|
||||
except Exception as _e:
|
||||
log_callback(f"[Sub2API] 异常({_sid}): {_e}")
|
||||
except Exception as s2a_err:
|
||||
log_callback(f"[Sub2API] 上传异常: {s2a_err}")
|
||||
|
||||
# 自动上传到 Team Manager(可多服务)
|
||||
if auto_upload_tm:
|
||||
try:
|
||||
from ...core.team_manager import upload_account_to_tm
|
||||
from ...database.models import Account as AccountModel
|
||||
saved_account = db.query(AccountModel).filter_by(email=result.email).first()
|
||||
if saved_account and saved_account.access_token:
|
||||
_tm_ids = tm_service_ids or []
|
||||
if not _tm_ids:
|
||||
_tm_ids = [s.id for s in crud.get_tm_services(db, enabled=True)]
|
||||
if not _tm_ids:
|
||||
log_callback("[TM] 无可用 Team Manager 服务,跳过上传")
|
||||
for _sid in _tm_ids:
|
||||
try:
|
||||
_svc = crud.get_tm_service_by_id(db, _sid)
|
||||
if not _svc:
|
||||
continue
|
||||
log_callback(f"[TM] 上传到服务: {_svc.name}")
|
||||
_ok, _msg = upload_account_to_tm(saved_account, _svc.api_url, _svc.api_key)
|
||||
log_callback(f"[TM] {'成功' if _ok else '失败'}({_svc.name}): {_msg}")
|
||||
except Exception as _e:
|
||||
log_callback(f"[TM] 异常({_sid}): {_e}")
|
||||
except Exception as tm_err:
|
||||
log_callback(f"[TM] 上传异常: {tm_err}")
|
||||
|
||||
# 更新任务状态
|
||||
crud.update_registration_task(
|
||||
db, task_uuid,
|
||||
@@ -433,7 +493,7 @@ def _run_sync_registration_task(task_uuid: str, email_service_type: str, proxy:
|
||||
pass
|
||||
|
||||
|
||||
async def run_registration_task(task_uuid: str, email_service_type: str, proxy: Optional[str], email_service_config: Optional[dict], email_service_id: Optional[int] = None, log_prefix: str = "", batch_id: str = "", auto_upload_cpa: bool = False, cpa_service_id: Optional[int] = None):
|
||||
async def run_registration_task(task_uuid: str, email_service_type: str, proxy: Optional[str], email_service_config: Optional[dict], email_service_id: Optional[int] = None, log_prefix: str = "", batch_id: str = "", auto_upload_cpa: bool = False, cpa_service_ids: List[int] = None, auto_upload_sub2api: bool = False, sub2api_service_ids: List[int] = None, auto_upload_tm: bool = False, tm_service_ids: List[int] = None):
|
||||
"""
|
||||
异步执行注册任务
|
||||
|
||||
@@ -461,7 +521,11 @@ async def run_registration_task(task_uuid: str, email_service_type: str, proxy:
|
||||
log_prefix,
|
||||
batch_id,
|
||||
auto_upload_cpa,
|
||||
cpa_service_id
|
||||
cpa_service_ids or [],
|
||||
auto_upload_sub2api,
|
||||
sub2api_service_ids or [],
|
||||
auto_upload_tm,
|
||||
tm_service_ids or [],
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"线程池执行异常: {task_uuid}, 错误: {e}")
|
||||
@@ -509,7 +573,11 @@ async def run_batch_parallel(
|
||||
email_service_id: Optional[int],
|
||||
concurrency: int,
|
||||
auto_upload_cpa: bool = False,
|
||||
cpa_service_id: Optional[int] = None
|
||||
cpa_service_ids: List[int] = None,
|
||||
auto_upload_sub2api: bool = False,
|
||||
sub2api_service_ids: List[int] = None,
|
||||
auto_upload_tm: bool = False,
|
||||
tm_service_ids: List[int] = None,
|
||||
):
|
||||
"""
|
||||
并行模式:所有任务同时提交,Semaphore 控制最大并发数
|
||||
@@ -525,8 +593,10 @@ async def run_batch_parallel(
|
||||
async with semaphore:
|
||||
await run_registration_task(
|
||||
uuid, email_service_type, proxy, email_service_config, email_service_id,
|
||||
log_prefix=prefix, batch_id=batch_id, auto_upload_cpa=auto_upload_cpa,
|
||||
cpa_service_id=cpa_service_id
|
||||
log_prefix=prefix, batch_id=batch_id,
|
||||
auto_upload_cpa=auto_upload_cpa, cpa_service_ids=cpa_service_ids or [],
|
||||
auto_upload_sub2api=auto_upload_sub2api, sub2api_service_ids=sub2api_service_ids or [],
|
||||
auto_upload_tm=auto_upload_tm, tm_service_ids=tm_service_ids or [],
|
||||
)
|
||||
with get_db() as db:
|
||||
t = crud.get_registration_task(db, uuid)
|
||||
@@ -569,7 +639,11 @@ async def run_batch_pipeline(
|
||||
interval_max: int,
|
||||
concurrency: int,
|
||||
auto_upload_cpa: bool = False,
|
||||
cpa_service_id: Optional[int] = None
|
||||
cpa_service_ids: List[int] = None,
|
||||
auto_upload_sub2api: bool = False,
|
||||
sub2api_service_ids: List[int] = None,
|
||||
auto_upload_tm: bool = False,
|
||||
tm_service_ids: List[int] = None,
|
||||
):
|
||||
"""
|
||||
流水线模式:每隔 interval 秒启动一个新任务,Semaphore 限制最大并发数
|
||||
@@ -585,8 +659,10 @@ async def run_batch_pipeline(
|
||||
try:
|
||||
await run_registration_task(
|
||||
uuid, email_service_type, proxy, email_service_config, email_service_id,
|
||||
log_prefix=pfx, batch_id=batch_id, auto_upload_cpa=auto_upload_cpa,
|
||||
cpa_service_id=cpa_service_id
|
||||
log_prefix=pfx, batch_id=batch_id,
|
||||
auto_upload_cpa=auto_upload_cpa, cpa_service_ids=cpa_service_ids or [],
|
||||
auto_upload_sub2api=auto_upload_sub2api, sub2api_service_ids=sub2api_service_ids or [],
|
||||
auto_upload_tm=auto_upload_tm, tm_service_ids=tm_service_ids or [],
|
||||
)
|
||||
with get_db() as db:
|
||||
t = crud.get_registration_task(db, uuid)
|
||||
@@ -653,21 +729,29 @@ async def run_batch_registration(
|
||||
concurrency: int = 1,
|
||||
mode: str = "pipeline",
|
||||
auto_upload_cpa: bool = False,
|
||||
cpa_service_id: Optional[int] = None
|
||||
cpa_service_ids: List[int] = None,
|
||||
auto_upload_sub2api: bool = False,
|
||||
sub2api_service_ids: List[int] = None,
|
||||
auto_upload_tm: bool = False,
|
||||
tm_service_ids: List[int] = None,
|
||||
):
|
||||
"""根据 mode 分发到并行或流水线执行"""
|
||||
if mode == "parallel":
|
||||
await run_batch_parallel(
|
||||
batch_id, task_uuids, email_service_type, proxy,
|
||||
email_service_config, email_service_id, concurrency,
|
||||
auto_upload_cpa=auto_upload_cpa, cpa_service_id=cpa_service_id
|
||||
auto_upload_cpa=auto_upload_cpa, cpa_service_ids=cpa_service_ids,
|
||||
auto_upload_sub2api=auto_upload_sub2api, sub2api_service_ids=sub2api_service_ids,
|
||||
auto_upload_tm=auto_upload_tm, tm_service_ids=tm_service_ids,
|
||||
)
|
||||
else:
|
||||
await run_batch_pipeline(
|
||||
batch_id, task_uuids, email_service_type, proxy,
|
||||
email_service_config, email_service_id,
|
||||
interval_min, interval_max, concurrency,
|
||||
auto_upload_cpa=auto_upload_cpa, cpa_service_id=cpa_service_id
|
||||
auto_upload_cpa=auto_upload_cpa, cpa_service_ids=cpa_service_ids,
|
||||
auto_upload_sub2api=auto_upload_sub2api, sub2api_service_ids=sub2api_service_ids,
|
||||
auto_upload_tm=auto_upload_tm, tm_service_ids=tm_service_ids,
|
||||
)
|
||||
|
||||
|
||||
@@ -715,7 +799,11 @@ async def start_registration(
|
||||
"",
|
||||
"",
|
||||
request.auto_upload_cpa,
|
||||
request.cpa_service_id
|
||||
request.cpa_service_ids,
|
||||
request.auto_upload_sub2api,
|
||||
request.sub2api_service_ids,
|
||||
request.auto_upload_tm,
|
||||
request.tm_service_ids,
|
||||
)
|
||||
|
||||
return task_to_response(task)
|
||||
@@ -788,7 +876,11 @@ async def start_batch_registration(
|
||||
request.concurrency,
|
||||
request.mode,
|
||||
request.auto_upload_cpa,
|
||||
request.cpa_service_id
|
||||
request.cpa_service_ids,
|
||||
request.auto_upload_sub2api,
|
||||
request.sub2api_service_ids,
|
||||
request.auto_upload_tm,
|
||||
request.tm_service_ids,
|
||||
)
|
||||
|
||||
return BatchRegistrationResponse(
|
||||
@@ -1118,7 +1210,11 @@ async def run_outlook_batch_registration(
|
||||
concurrency: int = 1,
|
||||
mode: str = "pipeline",
|
||||
auto_upload_cpa: bool = False,
|
||||
cpa_service_id: Optional[int] = None
|
||||
cpa_service_ids: List[int] = None,
|
||||
auto_upload_sub2api: bool = False,
|
||||
sub2api_service_ids: List[int] = None,
|
||||
auto_upload_tm: bool = False,
|
||||
tm_service_ids: List[int] = None,
|
||||
):
|
||||
"""
|
||||
异步执行 Outlook 批量注册任务,复用通用并发逻辑
|
||||
@@ -1157,7 +1253,11 @@ async def run_outlook_batch_registration(
|
||||
concurrency=concurrency,
|
||||
mode=mode,
|
||||
auto_upload_cpa=auto_upload_cpa,
|
||||
cpa_service_id=cpa_service_id
|
||||
cpa_service_ids=cpa_service_ids,
|
||||
auto_upload_sub2api=auto_upload_sub2api,
|
||||
sub2api_service_ids=sub2api_service_ids,
|
||||
auto_upload_tm=auto_upload_tm,
|
||||
tm_service_ids=tm_service_ids,
|
||||
)
|
||||
|
||||
|
||||
@@ -1257,7 +1357,11 @@ async def start_outlook_batch_registration(
|
||||
request.concurrency,
|
||||
request.mode,
|
||||
request.auto_upload_cpa,
|
||||
request.cpa_service_id
|
||||
request.cpa_service_ids,
|
||||
request.auto_upload_sub2api,
|
||||
request.sub2api_service_ids,
|
||||
request.auto_upload_tm,
|
||||
request.tm_service_ids,
|
||||
)
|
||||
|
||||
return OutlookBatchRegistrationResponse(
|
||||
|
||||
@@ -3,14 +3,15 @@
|
||||
"""
|
||||
|
||||
import logging
|
||||
from typing import Optional, Dict, Any, List
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ...config.settings import get_settings, update_settings
|
||||
from ...database import crud
|
||||
from ...database.session import get_db
|
||||
from ...config.settings import get_settings, update_settings
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
@@ -289,6 +290,7 @@ async def backup_database():
|
||||
raise HTTPException(status_code=404, detail="数据库文件不存在")
|
||||
|
||||
# 创建备份目录
|
||||
from fastapi import Path
|
||||
backup_dir = Path(db_path).parent / "backups"
|
||||
backup_dir.mkdir(exist_ok=True)
|
||||
|
||||
|
||||
2
src/web/routes/upload/__init__.py
Normal file
2
src/web/routes/upload/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
@@ -6,9 +6,9 @@ from typing import List, Optional
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ...database import crud
|
||||
from ...database.session import get_db
|
||||
from ...core.cpa_upload import test_cpa_connection
|
||||
from ....database import crud
|
||||
from ....database.session import get_db
|
||||
from ....core.upload.cpa_upload import test_cpa_connection
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
207
src/web/routes/upload/sub2api_services.py
Normal file
207
src/web/routes/upload/sub2api_services.py
Normal file
@@ -0,0 +1,207 @@
|
||||
"""
|
||||
Sub2API 服务管理 API 路由
|
||||
"""
|
||||
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ....database import crud
|
||||
from ....database.session import get_db
|
||||
from ....core.upload.sub2api_upload import test_sub2api_connection, batch_upload_to_sub2api
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
# ============== Pydantic Models ==============
|
||||
|
||||
class Sub2ApiServiceCreate(BaseModel):
|
||||
name: str
|
||||
api_url: str
|
||||
api_key: str
|
||||
enabled: bool = True
|
||||
priority: int = 0
|
||||
|
||||
|
||||
class Sub2ApiServiceUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
api_url: Optional[str] = None
|
||||
api_key: Optional[str] = None
|
||||
enabled: Optional[bool] = None
|
||||
priority: Optional[int] = None
|
||||
|
||||
|
||||
class Sub2ApiServiceResponse(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
api_url: str
|
||||
has_key: bool
|
||||
enabled: bool
|
||||
priority: int
|
||||
created_at: Optional[str] = None
|
||||
updated_at: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class Sub2ApiTestRequest(BaseModel):
|
||||
api_url: Optional[str] = None
|
||||
api_key: Optional[str] = None
|
||||
|
||||
|
||||
class Sub2ApiUploadRequest(BaseModel):
|
||||
account_ids: List[int]
|
||||
service_id: Optional[int] = None
|
||||
concurrency: int = 3
|
||||
priority: int = 50
|
||||
|
||||
|
||||
def _to_response(svc) -> Sub2ApiServiceResponse:
|
||||
return Sub2ApiServiceResponse(
|
||||
id=svc.id,
|
||||
name=svc.name,
|
||||
api_url=svc.api_url,
|
||||
has_key=bool(svc.api_key),
|
||||
enabled=svc.enabled,
|
||||
priority=svc.priority,
|
||||
created_at=svc.created_at.isoformat() if svc.created_at else None,
|
||||
updated_at=svc.updated_at.isoformat() if svc.updated_at else None,
|
||||
)
|
||||
|
||||
|
||||
# ============== API Endpoints ==============
|
||||
|
||||
@router.get("", response_model=List[Sub2ApiServiceResponse])
|
||||
async def list_sub2api_services(enabled: Optional[bool] = None):
|
||||
"""获取 Sub2API 服务列表"""
|
||||
with get_db() as db:
|
||||
services = crud.get_sub2api_services(db, enabled=enabled)
|
||||
return [_to_response(s) for s in services]
|
||||
|
||||
|
||||
@router.post("", response_model=Sub2ApiServiceResponse)
|
||||
async def create_sub2api_service(request: Sub2ApiServiceCreate):
|
||||
"""新增 Sub2API 服务"""
|
||||
with get_db() as db:
|
||||
svc = crud.create_sub2api_service(
|
||||
db,
|
||||
name=request.name,
|
||||
api_url=request.api_url,
|
||||
api_key=request.api_key,
|
||||
enabled=request.enabled,
|
||||
priority=request.priority,
|
||||
)
|
||||
return _to_response(svc)
|
||||
|
||||
|
||||
@router.get("/{service_id}", response_model=Sub2ApiServiceResponse)
|
||||
async def get_sub2api_service(service_id: int):
|
||||
"""获取单个 Sub2API 服务详情"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_sub2api_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Sub2API 服务不存在")
|
||||
return _to_response(svc)
|
||||
|
||||
|
||||
@router.get("/{service_id}/full")
|
||||
async def get_sub2api_service_full(service_id: int):
|
||||
"""获取 Sub2API 服务完整配置(含 API Key)"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_sub2api_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Sub2API 服务不存在")
|
||||
return {
|
||||
"id": svc.id,
|
||||
"name": svc.name,
|
||||
"api_url": svc.api_url,
|
||||
"api_key": svc.api_key,
|
||||
"enabled": svc.enabled,
|
||||
"priority": svc.priority,
|
||||
}
|
||||
|
||||
|
||||
@router.patch("/{service_id}", response_model=Sub2ApiServiceResponse)
|
||||
async def update_sub2api_service(service_id: int, request: Sub2ApiServiceUpdate):
|
||||
"""更新 Sub2API 服务配置"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_sub2api_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Sub2API 服务不存在")
|
||||
|
||||
update_data = {}
|
||||
if request.name is not None:
|
||||
update_data["name"] = request.name
|
||||
if request.api_url is not None:
|
||||
update_data["api_url"] = request.api_url
|
||||
# api_key 留空则保持原值
|
||||
if request.api_key:
|
||||
update_data["api_key"] = request.api_key
|
||||
if request.enabled is not None:
|
||||
update_data["enabled"] = request.enabled
|
||||
if request.priority is not None:
|
||||
update_data["priority"] = request.priority
|
||||
|
||||
svc = crud.update_sub2api_service(db, service_id, **update_data)
|
||||
return _to_response(svc)
|
||||
|
||||
|
||||
@router.delete("/{service_id}")
|
||||
async def delete_sub2api_service(service_id: int):
|
||||
"""删除 Sub2API 服务"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_sub2api_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Sub2API 服务不存在")
|
||||
crud.delete_sub2api_service(db, service_id)
|
||||
return {"success": True, "message": f"Sub2API 服务 {svc.name} 已删除"}
|
||||
|
||||
|
||||
@router.post("/{service_id}/test")
|
||||
async def test_sub2api_service(service_id: int):
|
||||
"""测试 Sub2API 服务连接"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_sub2api_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Sub2API 服务不存在")
|
||||
success, message = test_sub2api_connection(svc.api_url, svc.api_key)
|
||||
return {"success": success, "message": message}
|
||||
|
||||
|
||||
@router.post("/test-connection")
|
||||
async def test_sub2api_connection_direct(request: Sub2ApiTestRequest):
|
||||
"""直接测试 Sub2API 连接(用于添加前验证)"""
|
||||
if not request.api_url or not request.api_key:
|
||||
raise HTTPException(status_code=400, detail="api_url 和 api_key 不能为空")
|
||||
success, message = test_sub2api_connection(request.api_url, request.api_key)
|
||||
return {"success": success, "message": message}
|
||||
|
||||
|
||||
@router.post("/upload")
|
||||
async def upload_accounts_to_sub2api(request: Sub2ApiUploadRequest):
|
||||
"""批量上传账号到 Sub2API 平台"""
|
||||
if not request.account_ids:
|
||||
raise HTTPException(status_code=400, detail="账号 ID 列表不能为空")
|
||||
|
||||
with get_db() as db:
|
||||
if request.service_id:
|
||||
svc = crud.get_sub2api_service_by_id(db, request.service_id)
|
||||
else:
|
||||
svcs = crud.get_sub2api_services(db, enabled=True)
|
||||
svc = svcs[0] if svcs else None
|
||||
|
||||
if not svc:
|
||||
raise HTTPException(status_code=400, detail="未找到可用的 Sub2API 服务")
|
||||
|
||||
api_url = svc.api_url
|
||||
api_key = svc.api_key
|
||||
|
||||
results = batch_upload_to_sub2api(
|
||||
request.account_ids,
|
||||
api_url,
|
||||
api_key,
|
||||
concurrency=request.concurrency,
|
||||
priority=request.priority,
|
||||
)
|
||||
return results
|
||||
153
src/web/routes/upload/tm_services.py
Normal file
153
src/web/routes/upload/tm_services.py
Normal file
@@ -0,0 +1,153 @@
|
||||
"""
|
||||
Team Manager 服务管理 API 路由
|
||||
"""
|
||||
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ....database import crud
|
||||
from ....database.session import get_db
|
||||
from ....core.upload.team_manager_upload import test_team_manager_connection
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
# ============== Pydantic Models ==============
|
||||
|
||||
class TmServiceCreate(BaseModel):
|
||||
name: str
|
||||
api_url: str
|
||||
api_key: str
|
||||
enabled: bool = True
|
||||
priority: int = 0
|
||||
|
||||
|
||||
class TmServiceUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
api_url: Optional[str] = None
|
||||
api_key: Optional[str] = None
|
||||
enabled: Optional[bool] = None
|
||||
priority: Optional[int] = None
|
||||
|
||||
|
||||
class TmServiceResponse(BaseModel):
|
||||
id: int
|
||||
name: str
|
||||
api_url: str
|
||||
has_key: bool
|
||||
enabled: bool
|
||||
priority: int
|
||||
created_at: Optional[str] = None
|
||||
updated_at: Optional[str] = None
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class TmTestRequest(BaseModel):
|
||||
api_url: Optional[str] = None
|
||||
api_key: Optional[str] = None
|
||||
|
||||
|
||||
def _to_response(svc) -> TmServiceResponse:
|
||||
return TmServiceResponse(
|
||||
id=svc.id,
|
||||
name=svc.name,
|
||||
api_url=svc.api_url,
|
||||
has_key=bool(svc.api_key),
|
||||
enabled=svc.enabled,
|
||||
priority=svc.priority,
|
||||
created_at=svc.created_at.isoformat() if svc.created_at else None,
|
||||
updated_at=svc.updated_at.isoformat() if svc.updated_at else None,
|
||||
)
|
||||
|
||||
|
||||
# ============== API Endpoints ==============
|
||||
|
||||
@router.get("", response_model=List[TmServiceResponse])
|
||||
async def list_tm_services(enabled: Optional[bool] = None):
|
||||
"""获取 Team Manager 服务列表"""
|
||||
with get_db() as db:
|
||||
services = crud.get_tm_services(db, enabled=enabled)
|
||||
return [_to_response(s) for s in services]
|
||||
|
||||
|
||||
@router.post("", response_model=TmServiceResponse)
|
||||
async def create_tm_service(request: TmServiceCreate):
|
||||
"""新增 Team Manager 服务"""
|
||||
with get_db() as db:
|
||||
svc = crud.create_tm_service(
|
||||
db,
|
||||
name=request.name,
|
||||
api_url=request.api_url,
|
||||
api_key=request.api_key,
|
||||
enabled=request.enabled,
|
||||
priority=request.priority,
|
||||
)
|
||||
return _to_response(svc)
|
||||
|
||||
|
||||
@router.get("/{service_id}", response_model=TmServiceResponse)
|
||||
async def get_tm_service(service_id: int):
|
||||
"""获取单个 Team Manager 服务详情"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_tm_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Team Manager 服务不存在")
|
||||
return _to_response(svc)
|
||||
|
||||
|
||||
@router.patch("/{service_id}", response_model=TmServiceResponse)
|
||||
async def update_tm_service(service_id: int, request: TmServiceUpdate):
|
||||
"""更新 Team Manager 服务配置"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_tm_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Team Manager 服务不存在")
|
||||
|
||||
update_data = {}
|
||||
if request.name is not None:
|
||||
update_data["name"] = request.name
|
||||
if request.api_url is not None:
|
||||
update_data["api_url"] = request.api_url
|
||||
if request.api_key:
|
||||
update_data["api_key"] = request.api_key
|
||||
if request.enabled is not None:
|
||||
update_data["enabled"] = request.enabled
|
||||
if request.priority is not None:
|
||||
update_data["priority"] = request.priority
|
||||
|
||||
svc = crud.update_tm_service(db, service_id, **update_data)
|
||||
return _to_response(svc)
|
||||
|
||||
|
||||
@router.delete("/{service_id}")
|
||||
async def delete_tm_service(service_id: int):
|
||||
"""删除 Team Manager 服务"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_tm_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Team Manager 服务不存在")
|
||||
crud.delete_tm_service(db, service_id)
|
||||
return {"success": True, "message": f"Team Manager 服务 {svc.name} 已删除"}
|
||||
|
||||
|
||||
@router.post("/{service_id}/test")
|
||||
async def test_tm_service(service_id: int):
|
||||
"""测试 Team Manager 服务连接"""
|
||||
with get_db() as db:
|
||||
svc = crud.get_tm_service_by_id(db, service_id)
|
||||
if not svc:
|
||||
raise HTTPException(status_code=404, detail="Team Manager 服务不存在")
|
||||
success, message = test_team_manager_connection(svc.api_url, svc.api_key)
|
||||
return {"success": success, "message": message}
|
||||
|
||||
|
||||
@router.post("/test-connection")
|
||||
async def test_tm_connection_direct(request: TmTestRequest):
|
||||
"""直接测试 Team Manager 连接(用于添加前验证)"""
|
||||
if not request.api_url or not request.api_key:
|
||||
raise HTTPException(status_code=400, detail="api_url 和 api_key 不能为空")
|
||||
success, message = test_team_manager_connection(request.api_url, request.api_key)
|
||||
return {"success": success, "message": message}
|
||||
Reference in New Issue
Block a user