fix: 清掉无用接口和样式,并收紧登录与工号边界

This commit is contained in:
2026-11-16 11:30:59 +08:00
parent 7381b49e4c
commit 5b83b7e415
15 changed files with 232 additions and 329 deletions
+55 -27
View File
@@ -32,6 +32,8 @@ _pending: dict[str, "PendingLogin"] = {}
_pending_qr: dict[str, "PendingQr"] = {}
_credential_guard = threading.Lock()
_credential_locks: dict[str, threading.Lock] = {}
_pending_guard = threading.RLock()
_pending_busy: set[str] = set()
def _credential_lock(employee_id: str) -> threading.Lock:
@@ -43,6 +45,11 @@ def _credential_lock(employee_id: str) -> threading.Lock:
return lock
def release_credential_lock(employee_id: str) -> None:
with _credential_guard:
_credential_locks.pop((employee_id or "").strip(), None)
@dataclass
class PendingLogin:
client: EunivClient
@@ -80,27 +87,40 @@ def start_pending_cleaner() -> None:
def _purge_pending() -> None:
now = time.time()
for attempt_id, pending in list(_pending.items()):
if now - pending.created > _PENDING_SECONDS:
with _pending_guard:
for attempt_id, pending in list(_pending.items()):
if attempt_id in _pending_busy or now - pending.created <= _PENDING_SECONDS:
continue
pending.client.close()
_pending.pop(attempt_id, None)
for attempt_id, pending in list(_pending_qr.items()):
if now - pending.created > _QR_SECONDS:
for attempt_id, pending in list(_pending_qr.items()):
if attempt_id in _pending_busy or now - pending.created <= _QR_SECONDS:
continue
pending.client.close()
_pending_qr.pop(attempt_id, None)
def _drop_qr(attempt_id: str) -> None:
pending = _pending_qr.pop(attempt_id, None)
with _pending_guard:
pending = _pending_qr.pop(attempt_id, None)
if pending is not None:
pending.client.close()
def _drop_employee(employee_id: str) -> None:
for attempt_id, pending in list(_pending.items()):
if pending.employee_id == employee_id:
pending.client.close()
_pending.pop(attempt_id, None)
closing = []
with _pending_guard:
for attempt_id, pending in list(_pending.items()):
if pending.employee_id == employee_id and attempt_id not in _pending_busy:
closing.append(pending)
_pending.pop(attempt_id, None)
for pending in closing:
pending.client.close()
def _release_pending(attempt_id: str) -> None:
with _pending_guard:
_pending_busy.discard(attempt_id)
def _persist(db: Session, employee_id: str, password: str | None, client: EunivClient, profile: dict) -> None:
@@ -221,7 +241,8 @@ def login_and_store(
profile = client.login(manual=manual, auto_attempts=auto_attempts)
except ManualJigsaw as exc:
attempt_id = secrets.token_urlsafe(18)
_pending[attempt_id] = PendingLogin(client, employee_id, password, time.time())
with _pending_guard:
_pending[attempt_id] = PendingLogin(client, employee_id, password, time.time())
held = True
return {"attempt_id": attempt_id, "jigsaw": exc.puzzle}
except Exception as exc:
@@ -265,7 +286,10 @@ def _credential_still_valid(account: Account, client_factory) -> bool:
def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
_purge_pending()
pending = _pending.get(attempt_id)
with _pending_guard:
pending = _pending.get(attempt_id)
if pending is not None:
_pending_busy.add(attempt_id)
if pending is None:
raise RuntimeError("请重新登录")
try:
@@ -273,16 +297,21 @@ def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
except JigsawRejected:
pending.tries += 1
if pending.tries >= 5:
_pending.pop(attempt_id, None)
with _pending_guard:
_pending.pop(attempt_id, None)
pending.client.close()
raise RuntimeError("请重新登录")
pending.created = time.time()
return {"attempt_id": attempt_id, "jigsaw": pending.client.next_jigsaw()}
except Exception:
_pending.pop(attempt_id, None)
with _pending_guard:
_pending.pop(attempt_id, None)
pending.client.close()
raise
_pending.pop(attempt_id, None)
finally:
_release_pending(attempt_id)
with _pending_guard:
_pending.pop(attempt_id, None)
try:
resolved = _resolved_id(profile, pending.employee_id)
_persist(db, resolved, pending.password, pending.client, profile)
@@ -303,13 +332,17 @@ def start_qr_login(client_factory=EunivClient) -> dict:
client.close()
raise
attempt_id = secrets.token_urlsafe(18)
_pending_qr[attempt_id] = PendingQr(client, time.time())
with _pending_guard:
_pending_qr[attempt_id] = PendingQr(client, time.time())
return {"attempt_id": attempt_id, "qr_image": image, "ttl": 60}
def poll_qr_login(db: Session, attempt_id: str, client_factory=EunivClient) -> dict:
def poll_qr_login(db: Session, attempt_id: str) -> dict:
_purge_pending()
pending = _pending_qr.get(attempt_id)
with _pending_guard:
pending = _pending_qr.get(attempt_id)
if pending is not None:
_pending_busy.add(attempt_id)
if pending is None:
raise RuntimeError("请重新扫码")
try:
@@ -319,7 +352,10 @@ def poll_qr_login(db: Session, attempt_id: str, client_factory=EunivClient) -> d
except Exception:
_drop_qr(attempt_id)
raise
_pending_qr.pop(attempt_id, None)
finally:
_release_pending(attempt_id)
with _pending_guard:
_pending_qr.pop(attempt_id, None)
employee_id = str(profile.get("accountId") or profile.get("userName") or "").strip()
if not employee_id:
pending.client.close()
@@ -332,11 +368,6 @@ def poll_qr_login(db: Session, attempt_id: str, client_factory=EunivClient) -> d
pending.client.close()
def fetch_home(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
"""向 e 学现查首页信息。个人信息只出现在返回值里,不写入数据库。凭证变化时更新密文。"""
return _live(db, employee_id, client_factory, lambda client: _apply_hours_source(client, client.home_snapshot()))
def fetch_home_summary(db: Session, employee_id: str, client_factory=EunivClient) -> dict:
return _live(db, employee_id, client_factory, lambda client: _apply_hours_source(client, client.home_summary()))
@@ -484,6 +515,7 @@ def revoke_credential(db: Session, employee_id: str) -> None:
from euniv.autostudy import stop_account
stop_account(employee_id)
release_credential_lock(employee_id)
def refresh_stored_credentials(client_factory=EunivClient) -> None:
@@ -690,7 +722,3 @@ def _request_course_sync(employee_id: str) -> None:
from euniv.courses import request_course_sync
request_course_sync(employee_id)
def stored_password(account: Account) -> str:
return decrypt_secret(account.password_cipher)