feat: 账号可配置 HTTP/HTTPS/SOCKS 代理

This commit is contained in:
2026-11-15 01:21:17 +08:00
parent 1141df17f7
commit e9a1e9b315
15 changed files with 848 additions and 55 deletions
+45
View File
@@ -38,6 +38,14 @@ class MonthlyTargetBody(_BaseModel):
monthly_target: int
class ProxyBody(_BaseModel):
scheme: str
host: str
port: int
username: str = ""
password: str | None = None
@asynccontextmanager
async def lifespan(_app: FastAPI):
init_db()
@@ -217,6 +225,43 @@ def put_monthly_target(body: MonthlyTargetBody, request: Request, db: Session =
return {"monthly_target": account.monthly_target}
@app.get("/api/settings/proxy")
def get_proxy(request: Request, db: Session = Depends(get_db)) -> dict:
from euniv.proxy import view_of
account = db.get(Account, _signed_in(request, db))
if account is None:
raise HTTPException(status_code=401, detail="未登录")
return view_of(account)
@app.put("/api/settings/proxy")
def put_proxy(body: ProxyBody, request: Request, db: Session = Depends(get_db)) -> dict:
from euniv.proxy import ProxyError, apply, view_of
account = db.get(Account, _signed_in(request, db))
if account is None:
raise HTTPException(status_code=401, detail="未登录")
try:
apply(account, body.scheme, body.host, body.port, body.username, body.password)
except ProxyError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
db.commit()
return view_of(account)
@app.delete("/api/settings/proxy")
def delete_proxy(request: Request, db: Session = Depends(get_db)) -> dict:
from euniv.proxy import clear, view_of
account = db.get(Account, _signed_in(request, db))
if account is None:
raise HTTPException(status_code=401, detail="未登录")
clear(account)
db.commit()
return view_of(account)
@app.get("/api/autostudy/status")
def autostudy_state(request: Request, db: Session = Depends(get_db)) -> dict:
_signed_in(request, db)