fix: 扫码不再覆盖已有密码,登录按地址限速
This commit is contained in:
+80
-13
@@ -7,7 +7,7 @@ import json
|
||||
import secrets
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from dataclasses import dataclass
|
||||
|
||||
from sqlalchemy import select
|
||||
@@ -49,6 +49,7 @@ class PendingLogin:
|
||||
employee_id: str
|
||||
password: str
|
||||
created: float
|
||||
tries: int = 0
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -61,6 +62,22 @@ def _dump(credential: dict) -> str:
|
||||
return json.dumps(credential, ensure_ascii=False, sort_keys=True)
|
||||
|
||||
|
||||
def start_pending_cleaner() -> None:
|
||||
"""定时丢掉过期的登录会话,不依赖下一次请求。"""
|
||||
|
||||
def loop() -> None:
|
||||
while True:
|
||||
time.sleep(30)
|
||||
try:
|
||||
_purge_pending()
|
||||
except Exception:
|
||||
import logging
|
||||
|
||||
logging.getLogger("euniv.service").exception("清理未完成的登录失败")
|
||||
|
||||
threading.Thread(target=loop, name="euniv-pending", daemon=True).start()
|
||||
|
||||
|
||||
def _purge_pending() -> None:
|
||||
now = time.time()
|
||||
for attempt_id, pending in list(_pending.items()):
|
||||
@@ -86,16 +103,45 @@ def _drop_employee(employee_id: str) -> None:
|
||||
_pending.pop(attempt_id, None)
|
||||
|
||||
|
||||
def _persist(db: Session, employee_id: str, password: str, client: EunivClient, profile: dict) -> None:
|
||||
def _persist(db: Session, employee_id: str, password: str | None, client: EunivClient, profile: dict) -> None:
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
client.authorize(profile)
|
||||
account = db.get(Account, employee_id)
|
||||
if account is None:
|
||||
account = Account(employee_id=employee_id, password_cipher="", credential_cipher="")
|
||||
db.add(account)
|
||||
account.password_cipher = encrypt_secret(password)
|
||||
if password is None:
|
||||
stored = ""
|
||||
if account.password_cipher:
|
||||
try:
|
||||
stored = decrypt_secret(account.password_cipher)
|
||||
except Exception:
|
||||
stored = ""
|
||||
if not stored or is_qr_password(stored):
|
||||
account.password_cipher = encrypt_secret(QR_PASSWORD_MARK)
|
||||
else:
|
||||
account.password_cipher = encrypt_secret(password)
|
||||
account.password_login_at = datetime.now(timezone.utc)
|
||||
account.credential_cipher = encrypt_secret(_dump(client.export_credential()))
|
||||
account.credential_valid = True
|
||||
db.commit()
|
||||
try:
|
||||
db.commit()
|
||||
except IntegrityError:
|
||||
db.rollback()
|
||||
account = db.get(Account, employee_id)
|
||||
if account is None:
|
||||
raise
|
||||
if password is None:
|
||||
stored = decrypt_secret(account.password_cipher) if account.password_cipher else ""
|
||||
if not stored or is_qr_password(stored):
|
||||
account.password_cipher = encrypt_secret(QR_PASSWORD_MARK)
|
||||
else:
|
||||
account.password_cipher = encrypt_secret(password)
|
||||
account.password_login_at = datetime.now(timezone.utc)
|
||||
account.credential_cipher = encrypt_secret(_dump(client.export_credential()))
|
||||
account.credential_valid = True
|
||||
db.commit()
|
||||
from euniv.autostudy import sync_work
|
||||
|
||||
sync_work(employee_id)
|
||||
@@ -124,6 +170,19 @@ def _switch_proxy(account: Account | None, client, exc: BaseException) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def fresh_password(account: Account) -> bool:
|
||||
confirmed = getattr(account, "password_login_at", None)
|
||||
if confirmed is None:
|
||||
return False
|
||||
if confirmed.tzinfo is None:
|
||||
confirmed = confirmed.replace(tzinfo=timezone.utc)
|
||||
return datetime.now(timezone.utc) - confirmed <= timedelta(days=7)
|
||||
|
||||
|
||||
def _resolved_id(profile: dict, fallback: str) -> str:
|
||||
return str(profile.get("accountId") or fallback or "").strip() or fallback
|
||||
|
||||
|
||||
def login_and_store(
|
||||
db: Session,
|
||||
employee_id: str,
|
||||
@@ -142,10 +201,9 @@ def login_and_store(
|
||||
not is_qr_password(stored)
|
||||
and hmac.compare_digest(stored.encode("utf-8"), password.encode("utf-8"))
|
||||
)
|
||||
if same_password and _credential_still_valid(account, client_factory):
|
||||
if same_password and fresh_password(account) and _credential_still_valid(account, client_factory):
|
||||
_request_course_sync(employee_id)
|
||||
return {"employee_id": employee_id}
|
||||
_drop_employee(employee_id)
|
||||
from euniv.proxy_pool import MAX_SWITCHES
|
||||
|
||||
held = False
|
||||
@@ -168,9 +226,12 @@ def login_and_store(
|
||||
client = None
|
||||
continue
|
||||
raise
|
||||
_persist(db, employee_id, password, client, profile)
|
||||
_request_course_sync(employee_id)
|
||||
return {"employee_id": employee_id}
|
||||
resolved = _resolved_id(profile, employee_id)
|
||||
_persist(db, resolved, password, client, profile)
|
||||
_drop_employee(employee_id)
|
||||
_drop_employee(resolved)
|
||||
_request_course_sync(resolved)
|
||||
return {"employee_id": resolved}
|
||||
if last_exc is not None:
|
||||
raise last_exc
|
||||
raise RuntimeError("登录失败")
|
||||
@@ -205,6 +266,11 @@ def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
|
||||
try:
|
||||
profile = pending.client.submit_jigsaw(offset)
|
||||
except JigsawRejected:
|
||||
pending.tries += 1
|
||||
if pending.tries >= 5:
|
||||
_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:
|
||||
@@ -213,9 +279,10 @@ def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
|
||||
raise
|
||||
_pending.pop(attempt_id, None)
|
||||
try:
|
||||
_persist(db, pending.employee_id, pending.password, pending.client, profile)
|
||||
_request_course_sync(pending.employee_id)
|
||||
return {"employee_id": pending.employee_id}
|
||||
resolved = _resolved_id(profile, pending.employee_id)
|
||||
_persist(db, resolved, pending.password, pending.client, profile)
|
||||
_request_course_sync(resolved)
|
||||
return {"employee_id": resolved}
|
||||
finally:
|
||||
pending.client.close()
|
||||
|
||||
@@ -253,7 +320,7 @@ def poll_qr_login(db: Session, attempt_id: str, client_factory=EunivClient) -> d
|
||||
pending.client.close()
|
||||
raise RuntimeError("扫码登录没有返回工号")
|
||||
try:
|
||||
_persist(db, employee_id, QR_PASSWORD_MARK, pending.client, profile)
|
||||
_persist(db, employee_id, None, pending.client, profile)
|
||||
_request_course_sync(employee_id)
|
||||
return {"employee_id": employee_id}
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user