- 统一设置系统:create_scheduler_service 读取 DB 设置覆盖默认值 - 修复 ProxyRepository.update_score 误删所有无效代理的 SQL - ValidationQueue:修复 Worker 计数漂移与启动恢复任务饿死 - SchedulerService:移除 drain() 阻塞,主循环可正常响应 stop - TaskService:在调度器周期内自动清理过期任务,防止内存泄漏 - lifespan/conftest:规范关闭顺序,消除 Event loop closed 警告 - Repository:异常日志增加 exc_info,今日新增按 created_at 统计 - ValidatorService:防止 HTTP session 重复关闭,移除 SOCKS 多余 close - 前端:补全 pluginsStore.isEmpty,ProxyList 最低分数上限改为 100 - 删除 config.py 中冗余的 cors_origins_list property
114 lines
4.2 KiB
Python
114 lines
4.2 KiB
Python
"""代理验证服务 - 支持 HTTP/HTTPS/SOCKS4/SOCKS5"""
|
||
import asyncio
|
||
import random
|
||
import time
|
||
import aiohttp
|
||
import aiohttp_socks
|
||
from typing import Tuple
|
||
from app.core.log import logger
|
||
|
||
|
||
class ValidatorService:
|
||
"""代理验证器"""
|
||
|
||
# 测试 URL
|
||
TEST_URLS = {
|
||
"http": ["http://httpbin.org/ip", "http://api.ipify.org"],
|
||
"https": ["https://httpbin.org/ip", "https://api.ipify.org"],
|
||
}
|
||
|
||
def __init__(
|
||
self,
|
||
timeout: float = 5.0,
|
||
connect_timeout: float = 3.0,
|
||
max_concurrency: int = 50,
|
||
):
|
||
self.timeout = timeout
|
||
self.connect_timeout = connect_timeout
|
||
self.max_concurrency = max_concurrency
|
||
self.semaphore = asyncio.Semaphore(max_concurrency)
|
||
|
||
# 共享 HTTP/HTTPS ClientSession
|
||
self._http_connector = aiohttp.TCPConnector(
|
||
ssl=False,
|
||
limit=max_concurrency,
|
||
limit_per_host=max_concurrency,
|
||
force_close=False,
|
||
)
|
||
self._timeout = aiohttp.ClientTimeout(
|
||
total=timeout, connect=connect_timeout
|
||
)
|
||
self._http_session = aiohttp.ClientSession(
|
||
connector=self._http_connector,
|
||
timeout=self._timeout,
|
||
)
|
||
|
||
def _get_test_url(self, protocol: str) -> str:
|
||
"""获取测试 URL"""
|
||
urls = self.TEST_URLS.get(protocol.lower(), self.TEST_URLS["http"])
|
||
return random.choice(urls)
|
||
|
||
async def validate(self, ip: str, port: int, protocol: str = "http") -> Tuple[bool, float]:
|
||
"""验证单个代理,返回 (是否有效, 延迟毫秒)"""
|
||
protocol = protocol.lower()
|
||
|
||
async with self.semaphore:
|
||
start = time.time()
|
||
try:
|
||
if protocol in ("socks4", "socks5"):
|
||
return await self._validate_socks(ip, port, protocol, start)
|
||
else:
|
||
return await self._validate_http(ip, port, protocol, start)
|
||
except asyncio.TimeoutError:
|
||
logger.debug(f"Validation timeout: {ip}:{port} ({protocol})")
|
||
return False, 0.0
|
||
except Exception as e:
|
||
logger.debug(f"Validation error {ip}:{port} ({protocol}): {e}")
|
||
return False, 0.0
|
||
|
||
async def _validate_http(self, ip: str, port: int, protocol: str, start: float) -> Tuple[bool, float]:
|
||
"""验证 HTTP/HTTPS 代理"""
|
||
proxy_url = f"http://{ip}:{port}"
|
||
test_url = self._get_test_url(protocol)
|
||
|
||
async with self._http_session.get(test_url, proxy=proxy_url, allow_redirects=True) as response:
|
||
if response.status in (200, 301, 302):
|
||
latency = round((time.time() - start) * 1000, 2)
|
||
logger.info(f"HTTP valid: {ip}:{port} ({protocol}) {latency}ms")
|
||
return True, latency
|
||
return False, 0.0
|
||
|
||
async def _validate_socks(self, ip: str, port: int, protocol: str, start: float) -> Tuple[bool, float]:
|
||
"""验证 SOCKS4/SOCKS5 代理"""
|
||
proxy_type = (
|
||
aiohttp_socks.ProxyType.SOCKS4
|
||
if protocol == "socks4"
|
||
else aiohttp_socks.ProxyType.SOCKS5
|
||
)
|
||
connector = aiohttp_socks.ProxyConnector(
|
||
proxy_type=proxy_type,
|
||
host=ip,
|
||
port=port,
|
||
rdns=True,
|
||
ssl=False,
|
||
)
|
||
timeout = aiohttp.ClientTimeout(total=self.timeout, connect=self.connect_timeout)
|
||
test_url = self._get_test_url("http")
|
||
|
||
try:
|
||
async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
|
||
async with session.get(test_url, allow_redirects=True) as response:
|
||
if response.status in (200, 301, 302):
|
||
latency = round((time.time() - start) * 1000, 2)
|
||
logger.info(f"SOCKS valid: {ip}:{port} ({protocol}) {latency}ms")
|
||
return True, latency
|
||
return False, 0.0
|
||
finally:
|
||
# ClientSession 的 async with 退出时会自动关闭 connector,无需手动重复关闭
|
||
pass
|
||
|
||
async def close(self):
|
||
"""关闭共享的 HTTP ClientSession"""
|
||
if self._http_session and not self._http_session.closed:
|
||
await self._http_session.close()
|