From 138096df1770c15be250f67403753a454f8f67f0 Mon Sep 17 00:00:00 2001 From: Nixevol Date: Sun, 25 Oct 2026 07:11:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B8=85=E7=90=86=E8=B6=85=E8=BF=87?= =?UTF-8?q?=E5=8D=81=E5=88=86=E9=92=9F=E7=9A=84=E7=99=BB=E5=BD=95=E6=B5=81?= =?UTF-8?q?=E7=A8=8B=E5=92=8C=E6=98=8E=E6=96=87=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/platform/auth.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/backend/app/platform/auth.py b/backend/app/platform/auth.py index 0992286..0688880 100644 --- a/backend/app/platform/auth.py +++ b/backend/app/platform/auth.py @@ -4,6 +4,7 @@ from __future__ import annotations import base64 import logging +import time import uuid from fastapi import APIRouter, HTTPException @@ -14,6 +15,7 @@ from app.shared.state import state logger = logging.getLogger("nqi.auth") router = APIRouter(prefix="/api/auth", tags=["auth"]) +FLOW_TTL_SECONDS = 10 * 60 def _bad(message: str, status: int = 400) -> HTTPException: @@ -29,10 +31,28 @@ def _captcha_payload(session_id: str, challenge, **extra) -> dict: } +async def purge_expired_flows(now: float | None = None) -> None: + """关掉超过 10 分钟的登录流程,并清掉内存里的密码。""" + moment = time.monotonic() if now is None else now + expired: list[str] = [] + for session_id, (client, challenge) in list(state.flows.items()): + if client is state.client: + continue + created = getattr(challenge, "created_at", None) + if created is None or moment - float(created) < FLOW_TTL_SECONDS: + continue + expired.append(session_id) + for session_id in expired: + client, challenge = state.flows.pop(session_id) + challenge.password = "" + await client.aclose() + + @router.post("/captcha") async def captcha() -> dict: from app.platform.client import NqiClient + await purge_expired_flows() client = NqiClient() try: challenge = await client.open_login() @@ -41,6 +61,7 @@ async def captcha() -> dict: logger.info("打开登录页失败: %s", exc) raise _bad("连接失败,请稍后重试") from exc session_id = uuid.uuid4().hex + challenge.created_at = time.monotonic() state.flows[session_id] = (client, challenge) return _captcha_payload(session_id, challenge)