mirror of
https://github.com/zc-zhangchen/any-auto-register.git
synced 2026-05-07 15:54:07 +08:00
104 lines
3.0 KiB
Python
104 lines
3.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks
|
|
from sqlmodel import Session, select
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
from core.db import ProxyModel, get_session
|
|
from core.proxy_pool import proxy_pool
|
|
|
|
router = APIRouter(prefix="/proxies", tags=["proxies"])
|
|
|
|
|
|
class ProxyCreate(BaseModel):
|
|
url: str
|
|
region: str = ""
|
|
|
|
|
|
class ProxyBulkCreate(BaseModel):
|
|
proxies: list[str]
|
|
region: str = ""
|
|
|
|
|
|
class ProxyBatchDelete(BaseModel):
|
|
ids: list[int]
|
|
|
|
|
|
@router.get("")
|
|
def list_proxies(session: Session = Depends(get_session)):
|
|
items = session.exec(select(ProxyModel)).all()
|
|
return items
|
|
|
|
|
|
@router.post("")
|
|
def add_proxy(body: ProxyCreate, session: Session = Depends(get_session)):
|
|
existing = session.exec(select(ProxyModel).where(ProxyModel.url == body.url)).first()
|
|
if existing:
|
|
raise HTTPException(400, "代理已存在")
|
|
p = ProxyModel(url=body.url, region=body.region)
|
|
session.add(p)
|
|
session.commit()
|
|
session.refresh(p)
|
|
return p
|
|
|
|
|
|
@router.post("/bulk")
|
|
def bulk_add_proxies(body: ProxyBulkCreate, session: Session = Depends(get_session)):
|
|
added = 0
|
|
for url in body.proxies:
|
|
url = url.strip()
|
|
if not url:
|
|
continue
|
|
existing = session.exec(select(ProxyModel).where(ProxyModel.url == url)).first()
|
|
if not existing:
|
|
session.add(ProxyModel(url=url, region=body.region))
|
|
added += 1
|
|
session.commit()
|
|
return {"added": added}
|
|
|
|
|
|
@router.delete("/{proxy_id}")
|
|
def delete_proxy(proxy_id: int, session: Session = Depends(get_session)):
|
|
p = session.get(ProxyModel, proxy_id)
|
|
if not p:
|
|
raise HTTPException(404, "代理不存在")
|
|
session.delete(p)
|
|
session.commit()
|
|
return {"ok": True}
|
|
|
|
|
|
@router.post("/batch-delete")
|
|
def batch_delete_proxies(body: ProxyBatchDelete, session: Session = Depends(get_session)):
|
|
if not body.ids:
|
|
raise HTTPException(400, "代理 ID 列表不能为空")
|
|
ids = list(dict.fromkeys(int(i) for i in body.ids))
|
|
if len(ids) > 1000:
|
|
raise HTTPException(400, "单次最多删除 1000 条代理")
|
|
|
|
proxies = session.exec(select(ProxyModel).where(ProxyModel.id.in_(ids))).all()
|
|
found_ids = {p.id for p in proxies if p.id is not None}
|
|
for p in proxies:
|
|
session.delete(p)
|
|
session.commit()
|
|
|
|
return {
|
|
"deleted": len(found_ids),
|
|
"not_found": [pid for pid in ids if pid not in found_ids],
|
|
"total_requested": len(ids),
|
|
}
|
|
|
|
|
|
@router.patch("/{proxy_id}/toggle")
|
|
def toggle_proxy(proxy_id: int, session: Session = Depends(get_session)):
|
|
p = session.get(ProxyModel, proxy_id)
|
|
if not p:
|
|
raise HTTPException(404, "代理不存在")
|
|
p.is_active = not p.is_active
|
|
session.add(p)
|
|
session.commit()
|
|
return {"is_active": p.is_active}
|
|
|
|
|
|
@router.post("/check")
|
|
def check_proxies(background_tasks: BackgroundTasks):
|
|
background_tasks.add_task(proxy_pool.check_all)
|
|
return {"message": "检测任务已启动"}
|