feat: 维护中国代理池并支持账号自动均匀使用
This commit is contained in:
+123
-62
@@ -69,15 +69,28 @@ def _persist(db: Session, employee_id: str, password: str, client: EunivClient,
|
||||
|
||||
|
||||
def _client(factory, employee_id: str, password: str, account: Account | None = None):
|
||||
from euniv.proxy import url_of
|
||||
from euniv.proxy import bind_url
|
||||
|
||||
proxy = url_of(account) if account is not None else None
|
||||
proxy = bind_url(account) if account is not None else None
|
||||
try:
|
||||
return factory(employee_id, password, proxy)
|
||||
except TypeError:
|
||||
return factory(employee_id, password)
|
||||
|
||||
|
||||
def _switch_proxy(account: Account | None, client, exc: BaseException) -> bool:
|
||||
from euniv.proxy import is_proxy_failure, uses_pool
|
||||
from euniv.proxy_pool import mark_failed
|
||||
|
||||
if account is None or not uses_pool(account):
|
||||
return False
|
||||
proxy = getattr(client, "proxy", None)
|
||||
if not proxy or not is_proxy_failure(exc):
|
||||
return False
|
||||
mark_failed(account.employee_id, proxy, str(exc)[:200])
|
||||
return True
|
||||
|
||||
|
||||
def login_and_store(
|
||||
db: Session,
|
||||
employee_id: str,
|
||||
@@ -93,33 +106,55 @@ def login_and_store(
|
||||
_request_course_sync(employee_id)
|
||||
return {"employee_id": employee_id}
|
||||
_drop_employee(employee_id)
|
||||
client = _client(client_factory, employee_id, password, account)
|
||||
from euniv.proxy_pool import MAX_SWITCHES
|
||||
|
||||
held = False
|
||||
client = None
|
||||
last_exc: BaseException | None = None
|
||||
try:
|
||||
try:
|
||||
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())
|
||||
held = True
|
||||
return {"attempt_id": attempt_id, "jigsaw": exc.puzzle}
|
||||
_persist(db, employee_id, password, client, profile)
|
||||
_request_course_sync(employee_id)
|
||||
return {"employee_id": employee_id}
|
||||
for _ in range(MAX_SWITCHES + 1):
|
||||
client = _client(client_factory, employee_id, password, account)
|
||||
try:
|
||||
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())
|
||||
held = True
|
||||
return {"attempt_id": attempt_id, "jigsaw": exc.puzzle}
|
||||
except Exception as exc:
|
||||
last_exc = exc
|
||||
if _switch_proxy(account, client, exc):
|
||||
client.close()
|
||||
client = None
|
||||
continue
|
||||
raise
|
||||
_persist(db, employee_id, password, client, profile)
|
||||
_request_course_sync(employee_id)
|
||||
return {"employee_id": employee_id}
|
||||
if last_exc is not None:
|
||||
raise last_exc
|
||||
raise RuntimeError("登录失败")
|
||||
finally:
|
||||
if not held:
|
||||
if client is not None and not held:
|
||||
client.close()
|
||||
|
||||
|
||||
def _credential_still_valid(account: Account, client_factory) -> bool:
|
||||
client = _client(client_factory, account.employee_id, decrypt_secret(account.password_cipher), account)
|
||||
try:
|
||||
client.restore(json.loads(decrypt_secret(account.credential_cipher)))
|
||||
return bool(client.credential_alive())
|
||||
except Exception:
|
||||
return False
|
||||
finally:
|
||||
client.close()
|
||||
from euniv.proxy_pool import MAX_SWITCHES
|
||||
|
||||
password = decrypt_secret(account.password_cipher)
|
||||
for _ in range(MAX_SWITCHES + 1):
|
||||
client = _client(client_factory, account.employee_id, password, account)
|
||||
try:
|
||||
client.restore(json.loads(decrypt_secret(account.credential_cipher)))
|
||||
return bool(client.credential_alive())
|
||||
except Exception as exc:
|
||||
if _switch_proxy(account, client, exc):
|
||||
continue
|
||||
return False
|
||||
finally:
|
||||
client.close()
|
||||
return False
|
||||
|
||||
|
||||
def submit_jigsaw(db: Session, attempt_id: str, offset: int) -> dict:
|
||||
@@ -177,39 +212,54 @@ def _auto_attempts(db: Session) -> int:
|
||||
|
||||
|
||||
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:
|
||||
raise RuntimeError("请先登录")
|
||||
credential = json.loads(decrypt_secret(account.credential_cipher)) if account.credential_cipher else {}
|
||||
attempts = _auto_attempts(db)
|
||||
client = _client(client_factory, employee_id, decrypt_secret(account.password_cipher), account)
|
||||
try:
|
||||
if credential.get("yxt_token"):
|
||||
client.restore(credential)
|
||||
else:
|
||||
try:
|
||||
client.authorize(client.login(manual=False, auto_attempts=attempts))
|
||||
except ManualJigsaw:
|
||||
raise RuntimeError("登录需要重新验证,请重新登录") from None
|
||||
password = decrypt_secret(account.password_cipher)
|
||||
last_exc: BaseException | None = None
|
||||
for _ in range(MAX_SWITCHES + 1):
|
||||
client = _client(client_factory, employee_id, password, account)
|
||||
try:
|
||||
data = read(client)
|
||||
except SessionExpired:
|
||||
if credential.get("yxt_token"):
|
||||
client.restore(credential)
|
||||
else:
|
||||
try:
|
||||
client.authorize(client.login(manual=False, auto_attempts=attempts))
|
||||
except ManualJigsaw:
|
||||
raise RuntimeError("登录需要重新验证,请重新登录") from None
|
||||
try:
|
||||
client.authorize(client.login(manual=False, auto_attempts=attempts))
|
||||
except ManualJigsaw:
|
||||
raise RuntimeError("登录需要重新验证,请重新登录") from None
|
||||
data = read(client)
|
||||
updated = client.export_credential()
|
||||
with _credential_lock(employee_id):
|
||||
db.refresh(account)
|
||||
current_raw = account.credential_cipher
|
||||
current = json.loads(decrypt_secret(current_raw)) if current_raw else {}
|
||||
if _dump(updated) != _dump(current) and _dump(current) == _dump(credential):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
db.commit()
|
||||
return data
|
||||
finally:
|
||||
client.close()
|
||||
data = read(client)
|
||||
except SessionExpired:
|
||||
try:
|
||||
client.authorize(client.login(manual=False, auto_attempts=attempts))
|
||||
except ManualJigsaw:
|
||||
raise RuntimeError("登录需要重新验证,请重新登录") from None
|
||||
data = read(client)
|
||||
updated = client.export_credential()
|
||||
with _credential_lock(employee_id):
|
||||
db.refresh(account)
|
||||
current_raw = account.credential_cipher
|
||||
current = json.loads(decrypt_secret(current_raw)) if current_raw else {}
|
||||
if _dump(updated) != _dump(current) and _dump(current) == _dump(credential):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
db.commit()
|
||||
return data
|
||||
except RuntimeError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
last_exc = exc
|
||||
if _switch_proxy(account, client, exc):
|
||||
continue
|
||||
raise
|
||||
finally:
|
||||
client.close()
|
||||
if last_exc is not None:
|
||||
raise last_exc
|
||||
raise RuntimeError("请先登录")
|
||||
|
||||
|
||||
def revoke_credential(db: Session, employee_id: str) -> None:
|
||||
@@ -245,22 +295,33 @@ def _refresh_one(employee_id: str, attempts: int, client_factory) -> None:
|
||||
checked_alive = False
|
||||
hours = ""
|
||||
try:
|
||||
from euniv.proxy_pool import MAX_SWITCHES
|
||||
|
||||
password = decrypt_secret(account.password_cipher)
|
||||
previous = json.loads(decrypt_secret(account.credential_cipher))
|
||||
client = _client(client_factory, employee_id, password, account)
|
||||
client.restore(previous)
|
||||
if client.credential_alive():
|
||||
updated = client.export_credential()
|
||||
if _dump(updated) != _dump(previous):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
valid = True
|
||||
checked_alive = True
|
||||
else:
|
||||
profile = client.login(manual=False, auto_attempts=attempts)
|
||||
_persist(db, employee_id, password, client, profile)
|
||||
valid = True
|
||||
if valid:
|
||||
hours = _month_hours(client)
|
||||
for _ in range(MAX_SWITCHES + 1):
|
||||
client = _client(client_factory, employee_id, password, account)
|
||||
try:
|
||||
client.restore(previous)
|
||||
if client.credential_alive():
|
||||
updated = client.export_credential()
|
||||
if _dump(updated) != _dump(previous):
|
||||
account.credential_cipher = encrypt_secret(_dump(updated))
|
||||
valid = True
|
||||
checked_alive = True
|
||||
else:
|
||||
profile = client.login(manual=False, auto_attempts=attempts)
|
||||
_persist(db, employee_id, password, client, profile)
|
||||
valid = True
|
||||
if valid:
|
||||
hours = _month_hours(client)
|
||||
break
|
||||
except Exception as exc:
|
||||
if _switch_proxy(account, client, exc):
|
||||
client.close()
|
||||
client = None
|
||||
continue
|
||||
raise
|
||||
except Exception:
|
||||
db.rollback()
|
||||
valid = False
|
||||
|
||||
Reference in New Issue
Block a user