增加斗鱼账号检测功能

This commit is contained in:
yml2213
2026-07-09 23:12:39 +08:00
parent 1f16193690
commit cd2d02c86e
16 changed files with 1206 additions and 53 deletions
+112
View File
@@ -0,0 +1,112 @@
"""斗鱼账号检测路由。"""
from __future__ import annotations
import threading
from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import StreamingResponse
from sqlalchemy.orm import Session
from ..database import get_db
from ..deps import get_current_user
from ..models import ProxyConfig as ProxyConfigModel, User
from ..permissions import user_has_permission
from ..schemas import AccountCheckBatchOut, AccountCheckBatchRequest
from ..services.account_check_service import (
account_check_registry,
parse_account_check_lines,
)
router = APIRouter(prefix="/api/account-check", tags=["账号检测"])
def _get_runner_or_404(batch_id: str):
"""读取账号检测批次。"""
runner = account_check_registry.get(batch_id)
if not runner:
raise HTTPException(status_code=404, detail="批次不存在或服务已重启")
return runner
def _require_account_check_perm(user: User) -> None:
"""账号检测权限;兼容已有批量登录权限。"""
if not (
user_has_permission(user, "account:check")
or user_has_permission(user, "login:batch")
):
raise HTTPException(status_code=403, detail="权限不足")
@router.post("/batches", response_model=AccountCheckBatchOut)
def create_account_check_batch(
req: AccountCheckBatchRequest,
db: Session = Depends(get_db),
current: User = Depends(get_current_user),
):
"""启动斗鱼账号检测批次。"""
_require_account_check_perm(current)
try:
accounts = parse_account_check_lines(req.text)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
if not accounts:
raise HTTPException(status_code=400, detail="没有识别到有效账号")
proxy_config = db.query(ProxyConfigModel).first()
runner = account_check_registry.create(
accounts=accounts,
created_by=current.id,
concurrency=req.concurrency,
max_login_retries=req.max_login_retries,
max_total_time=req.max_total_time,
proxy_config=proxy_config,
)
thread = threading.Thread(target=runner.run, daemon=True)
thread.start()
return runner.snapshot()
@router.get("/batches/{batch_id}", response_model=AccountCheckBatchOut)
def get_account_check_batch(
batch_id: str,
current: User = Depends(get_current_user),
):
"""查询斗鱼账号检测批次。"""
_require_account_check_perm(current)
runner = _get_runner_or_404(batch_id)
return runner.snapshot()
@router.post("/batches/{batch_id}/stop")
def stop_account_check_batch(
batch_id: str,
current: User = Depends(get_current_user),
):
"""停止斗鱼账号检测批次。"""
_require_account_check_perm(current)
runner = _get_runner_or_404(batch_id)
runner.stop()
return {"message": "已发送停止信号", "success": True}
@router.get("/batches/{batch_id}/download")
def download_account_check_batch(
batch_id: str,
current: User = Depends(get_current_user),
):
"""下载斗鱼账号检测分类结果 zip。"""
_require_account_check_perm(current)
runner = _get_runner_or_404(batch_id)
snapshot = runner.snapshot()
if snapshot["status"] in {"pending", "running"}:
raise HTTPException(status_code=400, detail="批次尚未完成")
content, filename = runner.build_zip()
return StreamingResponse(
iter([content]),
media_type="application/zip",
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
)
+4 -2
View File
@@ -22,7 +22,7 @@ async def create_batch(
db: Session = Depends(get_db),
current: User = Depends(require_permission("login:batch")),
):
"""创建批量登录任务。"""
"""创建批量登录或账号检测任务。"""
if not req.account_ids:
raise HTTPException(status_code=400, detail="请选择账号")
@@ -41,7 +41,8 @@ async def create_batch(
valid_ids.append(aid)
if not valid_ids:
raise HTTPException(status_code=403, detail="没有可登录的账号")
action_name = "检测" if req.mode == "check" else "登录"
raise HTTPException(status_code=403, detail=f"没有可{action_name}的账号")
# 在主事件循环中创建 log_queue,传给后台线程
log_queue = asyncio.Queue()
@@ -61,6 +62,7 @@ async def create_batch(
loop=loop,
concurrency=req.concurrency,
api_strategy=req.api_strategy,
mode=req.mode,
)
batch_id = runner.batch_id