mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-06 08:06:37 +08:00
Merge pull request #244 from icesixgod:feature/proxy-health-check
feat: 实现代理健康检查功能
This commit is contained in:
@@ -11,6 +11,7 @@ from pydantic import BaseModel, Field
|
||||
from app.core.security import verify_auth_token
|
||||
from app.log.logger import Logger, get_config_routes_logger
|
||||
from app.service.config.config_service import ConfigService
|
||||
from app.service.proxy.proxy_check_service import get_proxy_check_service, ProxyCheckResult
|
||||
from app.utils.helpers import redact_key_for_logging
|
||||
|
||||
router = APIRouter(prefix="/api/config", tags=["config"])
|
||||
@@ -132,3 +133,93 @@ async def get_ui_models(request: Request):
|
||||
status_code=500,
|
||||
detail=f"An unexpected error occurred while fetching UI models: {str(e)}",
|
||||
)
|
||||
|
||||
|
||||
class ProxyCheckRequest(BaseModel):
|
||||
"""Proxy check request"""
|
||||
proxy: str = Field(..., description="Proxy address to check")
|
||||
use_cache: bool = Field(True, description="Whether to use cached results")
|
||||
|
||||
|
||||
class ProxyBatchCheckRequest(BaseModel):
|
||||
"""Batch proxy check request"""
|
||||
proxies: List[str] = Field(..., description="List of proxy addresses to check")
|
||||
use_cache: bool = Field(True, description="Whether to use cached results")
|
||||
max_concurrent: int = Field(5, description="Maximum concurrent check count", ge=1, le=10)
|
||||
|
||||
|
||||
@router.post("/proxy/check", response_model=ProxyCheckResult)
|
||||
async def check_single_proxy(proxy_request: ProxyCheckRequest, request: Request):
|
||||
"""Check if a single proxy is available"""
|
||||
auth_token = request.cookies.get("auth_token")
|
||||
if not auth_token or not verify_auth_token(auth_token):
|
||||
logger.warning("Unauthorized access attempt to proxy check")
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
|
||||
try:
|
||||
logger.info(f"Checking single proxy: {proxy_request.proxy}")
|
||||
proxy_service = get_proxy_check_service()
|
||||
result = await proxy_service.check_single_proxy(
|
||||
proxy_request.proxy,
|
||||
proxy_request.use_cache
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
logger.error(f"Proxy check failed: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Proxy check failed: {str(e)}")
|
||||
|
||||
|
||||
@router.post("/proxy/check-all", response_model=List[ProxyCheckResult])
|
||||
async def check_all_proxies(batch_request: ProxyBatchCheckRequest, request: Request):
|
||||
"""Check multiple proxies availability"""
|
||||
auth_token = request.cookies.get("auth_token")
|
||||
if not auth_token or not verify_auth_token(auth_token):
|
||||
logger.warning("Unauthorized access attempt to batch proxy check")
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
|
||||
try:
|
||||
logger.info(f"Batch checking {len(batch_request.proxies)} proxies")
|
||||
proxy_service = get_proxy_check_service()
|
||||
results = await proxy_service.check_multiple_proxies(
|
||||
batch_request.proxies,
|
||||
batch_request.use_cache,
|
||||
batch_request.max_concurrent
|
||||
)
|
||||
return results
|
||||
except Exception as e:
|
||||
logger.error(f"Batch proxy check failed: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Batch proxy check failed: {str(e)}")
|
||||
|
||||
|
||||
@router.get("/proxy/cache-stats")
|
||||
async def get_proxy_cache_stats(request: Request):
|
||||
"""Get proxy check cache statistics"""
|
||||
auth_token = request.cookies.get("auth_token")
|
||||
if not auth_token or not verify_auth_token(auth_token):
|
||||
logger.warning("Unauthorized access attempt to proxy cache stats")
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
|
||||
try:
|
||||
proxy_service = get_proxy_check_service()
|
||||
stats = proxy_service.get_cache_stats()
|
||||
return stats
|
||||
except Exception as e:
|
||||
logger.error(f"Get proxy cache stats failed: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Get cache stats failed: {str(e)}")
|
||||
|
||||
|
||||
@router.post("/proxy/clear-cache")
|
||||
async def clear_proxy_cache(request: Request):
|
||||
"""Clear proxy check cache"""
|
||||
auth_token = request.cookies.get("auth_token")
|
||||
if not auth_token or not verify_auth_token(auth_token):
|
||||
logger.warning("Unauthorized access attempt to clear proxy cache")
|
||||
return RedirectResponse(url="/", status_code=302)
|
||||
|
||||
try:
|
||||
proxy_service = get_proxy_check_service()
|
||||
proxy_service.clear_cache()
|
||||
return {"success": True, "message": "Proxy check cache cleared"}
|
||||
except Exception as e:
|
||||
logger.error(f"Clear proxy cache failed: {str(e)}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=f"Clear cache failed: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user