diff --git a/backend/admin/users.py b/backend/admin/users.py index b35e821..f403f83 100644 --- a/backend/admin/users.py +++ b/backend/admin/users.py @@ -350,4 +350,7 @@ def delete_user(db: Session, employee_id: str) -> dict: account = _require(db, employee_id) db.delete(account) db.commit() + from euniv.autostudy import stop_account + + stop_account(employee_id) return {"ok": True} diff --git a/backend/euniv/api.py b/backend/euniv/api.py index 29509c1..55ea4fe 100644 --- a/backend/euniv/api.py +++ b/backend/euniv/api.py @@ -395,13 +395,6 @@ def delete_proxy(request: Request, db: Session = Depends(get_db)) -> dict: return view_of(account) -@app.get("/api/autostudy/status") -def autostudy_state(request: Request, db: Session = Depends(get_db)) -> dict: - _signed_in(request, db) - from euniv.autostudy import autostudy_status - return autostudy_status() - - @app.get("/api/settings/study-control") def get_study_control(request: Request, db: Session = Depends(get_db)) -> dict: from euniv.autostudy import autostudy_status diff --git a/backend/euniv/autostudy.py b/backend/euniv/autostudy.py index d110f83..f0a07a1 100644 --- a/backend/euniv/autostudy.py +++ b/backend/euniv/autostudy.py @@ -256,6 +256,20 @@ def halt_work(employee_id: str) -> None: _notify(key, urgent=True) +def stop_account(employee_id: str) -> None: + """停止学习、释放代理,并清掉内存里的状态和站点会话。""" + key = (employee_id or "").strip() + if not key: + return + request_stop(key) + from euniv.client import forget_site + from euniv.proxy_pool import release + + release(key) + _clear_user_status(key) + forget_site(key) + + def request_stop(employee_id: str) -> None: key = (employee_id or "").strip() if not key: diff --git a/backend/euniv/client.py b/backend/euniv/client.py index 334a621..0e55ebf 100644 --- a/backend/euniv/client.py +++ b/backend/euniv/client.py @@ -249,6 +249,10 @@ _SITE_SHARE_SECONDS = 90 _site_sessions: dict[str, tuple[float, list[tuple[str, str, str, str]]]] = {} +def forget_site(account_id: str) -> None: + _site_sessions.pop(account_id, None) + + def _remember_site(account_id: str, client: httpx.Client) -> None: jar = getattr(getattr(client, "cookies", None), "jar", None) if not account_id or jar is None: diff --git a/backend/euniv/service.py b/backend/euniv/service.py index 380b67a..9f442d6 100644 --- a/backend/euniv/service.py +++ b/backend/euniv/service.py @@ -345,11 +345,11 @@ def _live(db: Session, employee_id: str, client_factory, read): from euniv.proxy_pool import MAX_SWITCHES account = db.get(Account, employee_id) - if account is None: + if account is None or not account.credential_cipher: raise RuntimeError("请先登录") - credential = json.loads(decrypt_secret(account.credential_cipher)) if account.credential_cipher else {} + credential = json.loads(decrypt_secret(account.credential_cipher)) attempts = _auto_attempts(db) - password = decrypt_secret(account.password_cipher) + password = decrypt_secret(account.password_cipher) if account.password_cipher else "" last_exc: BaseException | None = None for _ in range(MAX_SWITCHES + 1): client = _client(client_factory, employee_id, password, account) @@ -401,8 +401,12 @@ def revoke_credential(db: Session, employee_id: str) -> None: if account is None: return account.credential_cipher = "" + account.password_cipher = "" account.sessions_valid_after = datetime.now(timezone.utc) db.commit() + from euniv.autostudy import stop_account + + stop_account(employee_id) def refresh_stored_credentials(client_factory=EunivClient) -> None: diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 4753c6d..7a07225 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -332,7 +332,7 @@ def test_browser_session_logout_and_revoke() -> None: account = db.get(Account, "6650000146") assert account is not None assert account.credential_cipher == "" - assert decrypt_secret(account.password_cipher) == "secret-pass" + assert account.password_cipher == "" def test_logout_keeps_the_other_browser_until_revoke() -> None: