修复(CK检测): 统一斗鱼CK有效性检测入口并禁止非鉴权接口覆盖状态
This commit is contained in:
@@ -566,6 +566,8 @@ def _derive_session(
|
||||
"message": "活动接口可用",
|
||||
"checked_at": _now().isoformat(),
|
||||
"detail": {},
|
||||
# 活动接口(如 storehome / getUserInfo)不完整鉴权,绝不允许直接反写账号 CK 为 valid。
|
||||
"authoritative": False,
|
||||
}
|
||||
text = ";".join(errors.values())
|
||||
if any(marker in text for marker in ("登录", "Cookie", "重定向")):
|
||||
@@ -579,9 +581,88 @@ def _derive_session(
|
||||
"message": _text(text or "没有可用查询结果"),
|
||||
"checked_at": _now().isoformat(),
|
||||
"detail": {},
|
||||
"authoritative": False,
|
||||
}
|
||||
|
||||
|
||||
def _utc_aware(dt: datetime | None) -> datetime | None:
|
||||
if dt is None:
|
||||
return None
|
||||
return dt if dt.tzinfo else dt.replace(tzinfo=UTC)
|
||||
|
||||
|
||||
def verify_account_session(
|
||||
db: Session,
|
||||
account: Account,
|
||||
*,
|
||||
source: str,
|
||||
current: User | None = None,
|
||||
checker: Callable[[str], dict[str, Any]] | None = None,
|
||||
max_age_seconds: int | None = None,
|
||||
record_event: bool = False,
|
||||
commit: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""全系统统一的账号 CK 检测与写库入口。"""
|
||||
cookie = str(account.cookie or "").strip()
|
||||
if not cookie:
|
||||
return {
|
||||
"status": SESSION_UNVERIFIED,
|
||||
"message": "账号没有可用登录 Cookie",
|
||||
"checked_at": _now().isoformat(),
|
||||
"detail": {},
|
||||
}
|
||||
|
||||
now = _now()
|
||||
if max_age_seconds is not None and max_age_seconds > 0:
|
||||
checked_at = _utc_aware(account.ck_checked_at)
|
||||
stored_msg = (
|
||||
str((account.ck_check_result or {}).get("message") or "")
|
||||
if isinstance(account.ck_check_result, dict)
|
||||
else ""
|
||||
)
|
||||
if (
|
||||
account.ck_check_status == SESSION_VALID
|
||||
and checked_at is not None
|
||||
and stored_msg != "活动接口可用"
|
||||
and (now - checked_at).total_seconds() <= max_age_seconds
|
||||
):
|
||||
return {
|
||||
"status": SESSION_VALID,
|
||||
"message": stored_msg or "有效",
|
||||
"checked_at": checked_at.isoformat(),
|
||||
"detail": dict(account.ck_check_result or {}),
|
||||
}
|
||||
|
||||
session = probe_session(cookie, checker=checker or check_douyu_cookie)
|
||||
detail = session.get("detail") if isinstance(session.get("detail"), dict) else {}
|
||||
persist_probe_result(
|
||||
db,
|
||||
account,
|
||||
{
|
||||
"probe_id": f"{source}:{now.timestamp()}:{account.id or 0}",
|
||||
"identity": {
|
||||
"uid": account.uid,
|
||||
"username": account.username,
|
||||
"nickname": detail.get("nickname") or "",
|
||||
"nickname_source": "session",
|
||||
},
|
||||
"session": session,
|
||||
"elite": {},
|
||||
"eligibility": {},
|
||||
"roles": {},
|
||||
"gold": {},
|
||||
"goods": {},
|
||||
"errors": {},
|
||||
},
|
||||
current=current,
|
||||
source=source,
|
||||
record_event=record_event,
|
||||
commit=commit,
|
||||
update_session=True,
|
||||
)
|
||||
return session
|
||||
|
||||
|
||||
def _identity_result(
|
||||
account: Account,
|
||||
cookie: str,
|
||||
@@ -684,7 +765,7 @@ def probe_account(
|
||||
),
|
||||
)
|
||||
if session_checker is _DEFAULT_SESSION_CHECKER:
|
||||
session_checker = None if client is not None else check_douyu_cookie
|
||||
session_checker = check_douyu_cookie
|
||||
session = (
|
||||
probe_session(cookie, checker=session_checker)
|
||||
if callable(session_checker)
|
||||
@@ -1048,11 +1129,15 @@ def persist_probe_result(
|
||||
|
||||
session = probe.get("session") or {}
|
||||
session_status = str(session.get("status") or "")
|
||||
if update_session and session_status in {
|
||||
SESSION_VALID,
|
||||
SESSION_INVALID,
|
||||
SESSION_ERROR,
|
||||
}:
|
||||
session_message = str(session.get("message") or "")
|
||||
is_authoritative_valid = (
|
||||
session_status == SESSION_VALID
|
||||
and session.get("authoritative", True) is not False
|
||||
and session_message != "活动接口可用"
|
||||
)
|
||||
if update_session and (
|
||||
is_authoritative_valid or session_status in {SESSION_INVALID, SESSION_ERROR}
|
||||
):
|
||||
detail = (
|
||||
session.get("detail") if isinstance(session.get("detail"), dict) else {}
|
||||
)
|
||||
@@ -1279,4 +1364,5 @@ __all__ = [
|
||||
"resolve_esports_role",
|
||||
"resolve_peace_role",
|
||||
"update_identity_from_cookie",
|
||||
"verify_account_session",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user