fix: 扫码不再覆盖已有密码,登录按地址限速

This commit is contained in:
2026-11-16 09:25:01 +08:00
parent 337d842543
commit 89d9452967
8 changed files with 191 additions and 31 deletions
+36 -16
View File
@@ -8,6 +8,7 @@ import io
import json
import random
import re
import secrets
import threading
import time
import uuid
@@ -188,7 +189,7 @@ def make_qr_payload(prefix: str = QR_PREFIX) -> str:
return hashlib.md5(text.encode("utf-8")).hexdigest()
def digits(count: int) -> str:
return "".join(random.choice("0123456789") for _ in range(count))
return "".join(secrets.choice("0123456789") for _ in range(count))
key = digest(prefix + str(now_ms()) + digits(10))
value = digest(key + digits(10))
@@ -247,21 +248,28 @@ def _authorize_lock(account_id: str) -> threading.Lock:
_SITE_SHARE_SECONDS = 90
_site_sessions: dict[str, tuple[float, list[tuple[str, str, str, str]]]] = {}
_site_sessions: dict[tuple[str, str], tuple[float, list[tuple[str, str, str, str]]]] = {}
def forget_site(account_id: str) -> None:
_site_sessions.pop(account_id, None)
key = (account_id or "").strip()
for item in list(_site_sessions):
if item[0] == key:
_site_sessions.pop(item, None)
def _remember_site(account_id: str, client: httpx.Client) -> None:
def _remember_site(account_id: str, client: httpx.Client, token: str = "") -> None:
jar = getattr(getattr(client, "cookies", None), "jar", None)
if not account_id or jar is None:
return
now = time.time()
for item, (saved, _rows) in list(_site_sessions.items()):
if now - saved > _SITE_SHARE_SECONDS:
_site_sessions.pop(item, None)
dumped = []
for cookie in jar:
dumped.append((cookie.name, cookie.value or "", cookie.domain or "", cookie.path or "/"))
_site_sessions[account_id] = (time.time(), dumped)
_site_sessions[(account_id, token)] = (now, dumped)
def _load_cookies(client: httpx.Client | httpx.AsyncClient, dumped: list[tuple[str, str, str, str]]) -> None:
@@ -845,16 +853,25 @@ class EunivClient:
parts = self._qr_str.split(":")
if len(parts) < 3:
raise RuntimeError("请重新扫码")
payload = self._uac_post(
"/authentication/qrscan.serv",
json_body={"qrCodeKey": parts[1], "qrCodeValue": parts[2]},
).json()
try:
payload = self._uac_post(
"/authentication/qrscan.serv",
json_body={"qrCodeKey": parts[1], "qrCodeValue": parts[2]},
).json()
except (httpx.TransportError, TimeoutError):
self._qr_misses = getattr(self, "_qr_misses", 0) + 1
if self._qr_misses >= 5:
raise RuntimeError("扫码状态没有获取到") from None
raise QrWaiting() from None
top, inner, data, message = parse_uac(payload)
if top == UAC_OK and inner == UAC_QR_WAIT:
raise QrWaiting()
if top == UAC_OK and inner == UAC_OK and isinstance(data, dict) and data.get("token"):
self.login_name = str(data.get("accountId") or data.get("userName") or self.login_name)
self._qr_misses = 0
return data
if any(word in (message or "") for word in ("已扫码", "待确认", "确认中")):
raise QrWaiting()
raise RuntimeError(message or "扫码登录失败")
def _login_with_jigsaw(self, manual: bool, auto_attempts: int) -> dict:
@@ -1175,6 +1192,8 @@ class EunivClient:
"""读取「我的学时」列表:来源、获得学时(分钟)、日期。"""
try:
items = self._study_history_search(pageable=False)
except SessionExpired:
raise
except Exception:
items = None
if items is not None:
@@ -1193,10 +1212,10 @@ class EunivClient:
try:
return accumulate_month_minutes(fetch_page, year, month)
except Exception:
items = self.study_history()
total, _ = fold_month_minutes(items, year, month)
return total
except SessionExpired:
raise
except Exception as exc:
raise RuntimeError("学时历史没有获取到") from exc
def _study_history_search(
self, *, pageable: bool, offset: int = 0, limit: int = HISTORY_PAGE_SIZE
@@ -1243,7 +1262,8 @@ class EunivClient:
with _authorize_lock(account_id):
if getattr(self, "_site_ready", False):
return ""
shared = _site_sessions.get(account_id)
token = str(self._profile.get("token") or "")
shared = _site_sessions.get((account_id, token))
if shared and time.time() - shared[0] < _SITE_SHARE_SECONDS:
_load_cookies(self.client, shared[1])
self._site_ready = True
@@ -1255,10 +1275,10 @@ class EunivClient:
if "spMonthlyPeriod" not in probe.text:
self.authorize(self._profile)
self._site_ready = True
_remember_site(account_id, self.client)
_remember_site(account_id, self.client, token)
return ""
self._site_ready = True
_remember_site(account_id, self.client)
_remember_site(account_id, self.client, token)
return probe.text
return ""