feat: 账号可配置 HTTP/HTTPS/SOCKS 代理
This commit is contained in:
+66
-3
@@ -11,6 +11,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from euniv.crypto import decrypt_secret
|
||||
from euniv.models import Account
|
||||
from euniv.proxy import ProxyError, apply, clear, summary_of, url_of, view_of
|
||||
|
||||
_TOKEN_KEYS = (
|
||||
"ucs_token",
|
||||
@@ -22,10 +23,17 @@ _TOKEN_KEYS = (
|
||||
)
|
||||
|
||||
|
||||
def build_client(login_name: str, password: str):
|
||||
def build_client(login_name: str, password: str, proxy: str | None = None):
|
||||
from euniv.client import EunivClient
|
||||
|
||||
return EunivClient(login_name, password)
|
||||
return EunivClient(login_name, password, proxy=proxy)
|
||||
|
||||
|
||||
def _open_client(login_name: str, password: str, proxy: str | None = None):
|
||||
try:
|
||||
return build_client(login_name, password, proxy)
|
||||
except TypeError:
|
||||
return build_client(login_name, password)
|
||||
|
||||
|
||||
def record_outcome(account: Account, valid: bool | None) -> None:
|
||||
@@ -57,6 +65,8 @@ def list_users(db: Session) -> list[dict]:
|
||||
"last_refresh_at": _iso_time(account.last_refresh_at),
|
||||
"credential_valid": _optional_bool(account.credential_valid),
|
||||
"month_hours": account.month_hours or "",
|
||||
"has_proxy": bool(account.proxy_scheme and account.proxy_host and account.proxy_port),
|
||||
"proxy": summary_of(account),
|
||||
}
|
||||
for account in accounts
|
||||
]
|
||||
@@ -79,7 +89,7 @@ def check_employee(db: Session, employee_id: str) -> dict:
|
||||
result = "检查失败"
|
||||
valid: bool | None = None
|
||||
try:
|
||||
client = build_client(employee_id, decrypt_secret(account.password_cipher))
|
||||
client = _open_client(employee_id, decrypt_secret(account.password_cipher), url_of(account))
|
||||
client.restore(json.loads(decrypt_secret(account.credential_cipher)))
|
||||
alive = bool(client.credential_alive())
|
||||
result = "有效" if alive else "已失效"
|
||||
@@ -154,6 +164,59 @@ def read_credentials(db: Session, employee_id: str) -> dict:
|
||||
return payload
|
||||
|
||||
|
||||
def _accounts_by_ids(db: Session, employee_ids: list[str]) -> list[Account]:
|
||||
ids = []
|
||||
seen: set[str] = set()
|
||||
for raw in employee_ids:
|
||||
employee_id = str(raw or "").strip()
|
||||
if not employee_id or employee_id in seen:
|
||||
continue
|
||||
seen.add(employee_id)
|
||||
ids.append(employee_id)
|
||||
if not ids:
|
||||
raise HTTPException(status_code=400, detail="请选择账号")
|
||||
if len(ids) > 200:
|
||||
raise HTTPException(status_code=400, detail="一次最多选择 200 个账号")
|
||||
accounts = db.scalars(select(Account).where(Account.employee_id.in_(ids))).all()
|
||||
found = {account.employee_id for account in accounts}
|
||||
missing = [employee_id for employee_id in ids if employee_id not in found]
|
||||
if missing:
|
||||
raise HTTPException(status_code=404, detail="用户不存在")
|
||||
return accounts
|
||||
|
||||
|
||||
def read_proxy(db: Session, employee_id: str) -> dict:
|
||||
return view_of(_require(db, employee_id))
|
||||
|
||||
|
||||
def set_proxy(
|
||||
db: Session,
|
||||
employee_ids: list[str],
|
||||
scheme: str,
|
||||
host: str,
|
||||
port: int,
|
||||
username: str = "",
|
||||
password: str | None = None,
|
||||
) -> dict:
|
||||
accounts = _accounts_by_ids(db, employee_ids)
|
||||
try:
|
||||
for account in accounts:
|
||||
apply(account, scheme, host, port, username, password)
|
||||
except ProxyError as exc:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
db.commit()
|
||||
return {"ok": True, "count": len(accounts)}
|
||||
|
||||
|
||||
def clear_proxy(db: Session, employee_ids: list[str]) -> dict:
|
||||
accounts = _accounts_by_ids(db, employee_ids)
|
||||
for account in accounts:
|
||||
clear(account)
|
||||
db.commit()
|
||||
return {"ok": True, "count": len(accounts)}
|
||||
|
||||
|
||||
def delete_user(db: Session, employee_id: str) -> dict:
|
||||
account = _require(db, employee_id)
|
||||
db.delete(account)
|
||||
|
||||
Reference in New Issue
Block a user