fix: 全面修复代码问题并优化架构
修复问题: - 添加缺失的 httpx 依赖到 requirements.txt - 修复前端批量删除参数格式与后端不匹配(数组->对象数组) - 移除 app/api/main.py 中重复创建 app 的冗余代码 - 修复 Plugins.vue v-model 直接修改 store 状态的 Vue 警告 - 修复 README 端口/启动命令文档与实际配置不一致 - 修正 pytest.ini 过时配置 (asyncio_default_fixture_loop_scope) - 修复 WebUI index.html 语言设置为 zh-CN - 修复 .gitignore 错误忽略 tests/ 目录 后端优化: - 修复调度器默认间隔从 5 秒改为 30 分钟,避免无节制验证 - 修复 validate_all_now 在调度器停止时无法执行的 bug - 设置保存后热更新运行中调度器的验证间隔 - 将 update_score 优化为原子单事务 SQL,消除并发竞态 - 导出功能改为真正的流式分批读取(iter_batches),降低大导出内存占用 - ProxyResponse Schema 补齐 response_time_ms 字段 - 日志级别改为从配置动态读取,不再硬编码 INFO - 清理 validator_service 中的冗余 try/finally 代码 插件健壮性: - 修复 ip3366/ip89/kuaidaili/proxylist_download/speedx/yundaili/proxyscrape 的端口范围检查和 IPv6 地址解析问题(改用 rsplit + 1-65535 校验) - 修复 PluginService.list_plugins 并发竞争条件 - 修复 run_all_plugins 去重逻辑与数据库 UNIQUE 约束保持一致 - 修复 proxyscrape 异常时错误跳过 fallback 的 bug 测试: - 新增 7 个插件解析单元测试 - 新增 update_score 自动删除和 iter_batches 流式读取测试 - 全部 74 个测试通过
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -53,7 +53,7 @@ logs/
|
|||||||
|
|
||||||
# Test
|
# Test
|
||||||
test/
|
test/
|
||||||
tests/
|
# tests/ # 保留测试目录以便版本控制
|
||||||
|
|
||||||
# Share Directory
|
# Share Directory
|
||||||
share/
|
share/
|
||||||
|
|||||||
14
README.md
14
README.md
@@ -14,12 +14,12 @@
|
|||||||
## 📦 技术栈
|
## 📦 技术栈
|
||||||
|
|
||||||
### 后端
|
### 后端
|
||||||
- **框架**: FastAPI (端口 9949)
|
- **框架**: FastAPI (端口 18080)
|
||||||
- **数据库**: SQLite + aiosqlite
|
- **数据库**: SQLite + aiosqlite
|
||||||
- **异步**: asyncio
|
- **异步**: asyncio
|
||||||
|
|
||||||
### 前端
|
### 前端
|
||||||
- **框架**: Vue 3 + Vite (端口 9948)
|
- **框架**: Vue 3 + Vite (端口 18081)
|
||||||
- **UI库**: Element Plus
|
- **UI库**: Element Plus
|
||||||
- **状态管理**: Pinia
|
- **状态管理**: Pinia
|
||||||
- **图表**: ECharts
|
- **图表**: ECharts
|
||||||
@@ -53,7 +53,7 @@ start.bat
|
|||||||
|
|
||||||
**启动后端服务**(终端 1)
|
**启动后端服务**(终端 1)
|
||||||
```bash
|
```bash
|
||||||
python api_server.py
|
python main.py
|
||||||
```
|
```
|
||||||
|
|
||||||
**启动前端服务**(终端 2)
|
**启动前端服务**(终端 2)
|
||||||
@@ -71,7 +71,7 @@ stop.bat
|
|||||||
|
|
||||||
### 4. 访问 WebUI
|
### 4. 访问 WebUI
|
||||||
|
|
||||||
打开浏览器访问:**http://localhost:9948**
|
打开浏览器访问:**http://localhost:18081**
|
||||||
|
|
||||||
## 📁 项目结构
|
## 📁 项目结构
|
||||||
|
|
||||||
@@ -202,10 +202,10 @@ POST /api/settings
|
|||||||
2. **手动测试 API**
|
2. **手动测试 API**
|
||||||
```bash
|
```bash
|
||||||
# 获取统计信息
|
# 获取统计信息
|
||||||
curl http://localhost:9949/api/stats
|
curl http://localhost:18080/api/stats
|
||||||
|
|
||||||
# 获取代理列表
|
# 获取代理列表
|
||||||
curl -X POST http://localhost:9949/api/proxies \
|
curl -X POST http://localhost:18080/api/proxies \
|
||||||
-H "Content-Type: application/json" \
|
-H "Content-Type: application/json" \
|
||||||
-d '{"page": 1, "page_size": 20}'
|
-d '{"page": 1, "page_size": 20}'
|
||||||
```
|
```
|
||||||
@@ -229,7 +229,7 @@ POST /api/settings
|
|||||||
## 🔧 常见问题
|
## 🔧 常见问题
|
||||||
|
|
||||||
### Q: 启动后端口被占用?
|
### Q: 启动后端口被占用?
|
||||||
A: 修改 `config.py` 中的端口号(默认9949)或 `frontend/vite.config.js` 中的端口号(默认9948)
|
A: 修改 `.env` 中的端口号(默认18080)或 `WebUI/vite.config.js` 中的端口号(默认18081)
|
||||||
|
|
||||||
### Q: 爬虫无法抓取代理?
|
### Q: 爬虫无法抓取代理?
|
||||||
A: 检查网络连接,确保能访问目标网站,或尝试更换代理源插件
|
A: 检查网络连接,确保能访问目标网站,或尝试更换代理源插件
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="zh-CN">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ export const statsAPI = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const proxiesAPI = {
|
export const proxiesAPI = {
|
||||||
getProxies: (params, signal) =>
|
getProxies: (params, signal) =>
|
||||||
api.post('/api/proxies', cleanParams(params), signal ? { signal } : {}),
|
api.post('/api/proxies', cleanParams(params), { signal }),
|
||||||
|
|
||||||
deleteProxy: (ip, port) => api.delete(`/api/proxies/${ip}/${port}`),
|
deleteProxy: (ip, port) => api.delete(`/api/proxies/${ip}/${port}`),
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ export const useProxyStore = defineStore('proxy', () => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除代理
|
* 批量删除代理
|
||||||
* @param {Array<[string, number|string]>} proxyList
|
* @param {Array<{ip: string, port: number}>} proxyList
|
||||||
* @returns {Promise<number>} 实际删除的数量
|
* @returns {Promise<number>} 实际删除的数量
|
||||||
*/
|
*/
|
||||||
async function batchDeleteProxies(proxyList) {
|
async function batchDeleteProxies(proxyList) {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@
|
|||||||
<el-table-column label="状态" width="120" align="center">
|
<el-table-column label="状态" width="120" align="center">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
<el-switch
|
<el-switch
|
||||||
v-model="row.enabled"
|
:model-value="row.enabled"
|
||||||
@change="(val) => handleToggle(row.id, val)"
|
@change="(val) => handleToggle(row.id, val)"
|
||||||
class="theme-switch"
|
class="theme-switch"
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -212,7 +212,7 @@ function handleSearch() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleSelectionChange(selection) {
|
function handleSelectionChange(selection) {
|
||||||
selectedProxies.value = selection.map(item => [item.ip, item.port])
|
selectedProxies.value = selection.map(item => ({ ip: item.ip, port: item.port }))
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleCopy(proxy) {
|
async function handleCopy(proxy) {
|
||||||
|
|||||||
@@ -48,6 +48,6 @@ def create_scheduler_service(db_settings: dict | None = None) -> SchedulerServic
|
|||||||
)
|
)
|
||||||
svc = SchedulerService(validation_queue=queue, proxy_repo=proxy_repo)
|
svc = SchedulerService(validation_queue=queue, proxy_repo=proxy_repo)
|
||||||
svc.interval_minutes = db_settings.get(
|
svc.interval_minutes = db_settings.get(
|
||||||
"validate_interval_minutes", app_settings.validator_timeout
|
"validate_interval_minutes", 30
|
||||||
)
|
)
|
||||||
return svc
|
return svc
|
||||||
|
|||||||
@@ -53,6 +53,3 @@ def create_app() -> FastAPI:
|
|||||||
}
|
}
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
||||||
|
|
||||||
app = create_app()
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
"""设置相关路由"""
|
"""设置相关路由"""
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter, Request
|
||||||
from app.core.db import get_db
|
from app.core.db import get_db
|
||||||
from app.repositories.settings_repo import SettingsRepository
|
from app.repositories.settings_repo import SettingsRepository
|
||||||
from app.models.schemas import SettingsSchema
|
from app.models.schemas import SettingsSchema
|
||||||
from app.api.common import success_response, error_response
|
from app.api.common import success_response, error_response
|
||||||
|
from app.core.log import logger
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/settings", tags=["settings"])
|
router = APIRouter(prefix="/api/settings", tags=["settings"])
|
||||||
settings_repo = SettingsRepository()
|
settings_repo = SettingsRepository()
|
||||||
@@ -17,9 +18,18 @@ async def get_settings():
|
|||||||
|
|
||||||
|
|
||||||
@router.post("")
|
@router.post("")
|
||||||
async def save_settings(request: SettingsSchema):
|
async def save_settings(request: SettingsSchema, http_request: Request):
|
||||||
async with get_db() as db:
|
async with get_db() as db:
|
||||||
success = await settings_repo.save(db, request.model_dump())
|
success = await settings_repo.save(db, request.model_dump())
|
||||||
if not success:
|
if not success:
|
||||||
return error_response("保存设置失败", 500)
|
return error_response("保存设置失败", 500)
|
||||||
|
|
||||||
|
# 热更新运行中调度器的间隔时间
|
||||||
|
scheduler = getattr(http_request.app.state, "scheduler_service", None)
|
||||||
|
if scheduler and scheduler.running:
|
||||||
|
new_interval = request.validate_interval_minutes
|
||||||
|
if scheduler.interval_minutes != new_interval:
|
||||||
|
scheduler.interval_minutes = new_interval
|
||||||
|
logger.info(f"Scheduler interval updated to {new_interval} minutes")
|
||||||
|
|
||||||
return success_response("保存设置成功", request.model_dump())
|
return success_response("保存设置成功", request.model_dump())
|
||||||
|
|||||||
@@ -29,7 +29,16 @@ console_handler.setFormatter(formatter)
|
|||||||
|
|
||||||
# 获取标准 logger
|
# 获取标准 logger
|
||||||
logger = logging.getLogger('ProxyPool')
|
logger = logging.getLogger('ProxyPool')
|
||||||
logger.setLevel(logging.INFO)
|
|
||||||
|
# 尝试从配置读取日志级别,默认 INFO
|
||||||
|
try:
|
||||||
|
from app.core.config import settings
|
||||||
|
log_level = getattr(logging, settings.log_level.upper(), logging.INFO)
|
||||||
|
except Exception:
|
||||||
|
log_level = logging.INFO
|
||||||
|
logger.setLevel(log_level)
|
||||||
|
file_handler.setLevel(log_level)
|
||||||
|
console_handler.setLevel(log_level)
|
||||||
|
|
||||||
# 防止重复添加 handler(如模块重导入)
|
# 防止重复添加 handler(如模块重导入)
|
||||||
if not logger.handlers:
|
if not logger.handlers:
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ class ProxyResponse(BaseModel):
|
|||||||
port: int
|
port: int
|
||||||
protocol: str
|
protocol: str
|
||||||
score: int
|
score: int
|
||||||
|
response_time_ms: Optional[float] = None
|
||||||
last_check: Optional[str] = None
|
last_check: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -53,8 +53,11 @@ class Ip3366Plugin(BaseHTTPPlugin):
|
|||||||
protocol = tds[4].get_text(strip=True).lower() if len(tds) > 4 else "http"
|
protocol = tds[4].get_text(strip=True).lower() if len(tds) > 4 else "http"
|
||||||
if protocol not in VALID_PROTOCOLS:
|
if protocol not in VALID_PROTOCOLS:
|
||||||
protocol = "http"
|
protocol = "http"
|
||||||
if re.match(r"^\d+\.\d+\.\d+\.\d+$", ip) and port.isdigit():
|
if re.match(r"^\d+\.\d+\.\d+\.\d+$", ip) and port.isdigit() and 1 <= int(port) <= 65535:
|
||||||
results.append(ProxyRaw(ip, int(port), protocol))
|
try:
|
||||||
|
results.append(ProxyRaw(ip, int(port), protocol))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
|
||||||
if results:
|
if results:
|
||||||
logger.info(f"{self.display_name} 解析完成,获得 {len(results)} 个潜在代理")
|
logger.info(f"{self.display_name} 解析完成,获得 {len(results)} 个潜在代理")
|
||||||
|
|||||||
@@ -34,8 +34,11 @@ class Ip89Plugin(BaseHTTPPlugin):
|
|||||||
if len(tds) >= 2:
|
if len(tds) >= 2:
|
||||||
ip = tds[0].get_text(strip=True)
|
ip = tds[0].get_text(strip=True)
|
||||||
port = tds[1].get_text(strip=True)
|
port = tds[1].get_text(strip=True)
|
||||||
if re.match(r"^\d+\.\d+\.\d+\.\d+$", ip) and port.isdigit():
|
if re.match(r"^\d+\.\d+\.\d+\.\d+$", ip) and port.isdigit() and 1 <= int(port) <= 65535:
|
||||||
results.append(ProxyRaw(ip, int(port), "http"))
|
try:
|
||||||
|
results.append(ProxyRaw(ip, int(port), "http"))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
|
||||||
await asyncio.sleep(random.uniform(1, 2))
|
await asyncio.sleep(random.uniform(1, 2))
|
||||||
|
|
||||||
|
|||||||
@@ -61,8 +61,11 @@ class KuaiDaiLiPlugin(BaseHTTPPlugin):
|
|||||||
protocol = tds[4].get_text(strip=True).lower() if len(tds) > 4 else "http"
|
protocol = tds[4].get_text(strip=True).lower() if len(tds) > 4 else "http"
|
||||||
if protocol not in VALID_PROTOCOLS:
|
if protocol not in VALID_PROTOCOLS:
|
||||||
protocol = "http"
|
protocol = "http"
|
||||||
if re.match(r"^\d+\.\d+\.\d+\.\d+$", ip) and port.isdigit():
|
if re.match(r"^\d+\.\d+\.\d+\.\d+$", ip) and port.isdigit() and 1 <= int(port) <= 65535:
|
||||||
results.append(ProxyRaw(ip, int(port), protocol))
|
try:
|
||||||
|
results.append(ProxyRaw(ip, int(port), protocol))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
await asyncio.sleep(random.uniform(5, 8))
|
await asyncio.sleep(random.uniform(5, 8))
|
||||||
|
|
||||||
if results:
|
if results:
|
||||||
|
|||||||
@@ -59,12 +59,14 @@ class ProxyListDownloadPlugin(BaseHTTPPlugin):
|
|||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line or ":" not in line:
|
if not line or ":" not in line:
|
||||||
continue
|
continue
|
||||||
parts = line.split(":")
|
ip, _, port = line.rpartition(":")
|
||||||
if len(parts) >= 2:
|
ip = ip.strip()
|
||||||
ip = parts[0].strip()
|
port = port.strip()
|
||||||
port = parts[1].strip()
|
if ip and port.isdigit() and 1 <= int(port) <= 65535:
|
||||||
if ip and port.isdigit():
|
try:
|
||||||
results.append(ProxyRaw(ip, int(port), protocol))
|
results.append(ProxyRaw(ip, int(port), protocol))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
return results
|
return results
|
||||||
|
|
||||||
async def crawl(self) -> List[ProxyRaw]:
|
async def crawl(self) -> List[ProxyRaw]:
|
||||||
|
|||||||
@@ -42,12 +42,14 @@ class ProxyScrapePlugin(BaseHTTPPlugin):
|
|||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line or ":" not in line:
|
if not line or ":" not in line:
|
||||||
continue
|
continue
|
||||||
parts = line.split(":")
|
ip, _, port_str = line.rpartition(":")
|
||||||
if len(parts) >= 2:
|
ip = ip.strip()
|
||||||
ip = parts[0].strip()
|
port_str = port_str.strip()
|
||||||
port_str = parts[1].strip()
|
if port_str.isdigit() and 1 <= int(port_str) <= 65535:
|
||||||
if port_str.isdigit():
|
try:
|
||||||
proxies.append(ProxyRaw(ip, int(port_str), protocol))
|
proxies.append(ProxyRaw(ip, int(port_str), protocol))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
return proxies
|
return proxies
|
||||||
|
|
||||||
async def crawl(self) -> List[ProxyRaw]:
|
async def crawl(self) -> List[ProxyRaw]:
|
||||||
@@ -71,7 +73,7 @@ class ProxyScrapePlugin(BaseHTTPPlugin):
|
|||||||
htmls.append("")
|
htmls.append("")
|
||||||
except Exception:
|
except Exception:
|
||||||
htmls.append("")
|
htmls.append("")
|
||||||
done_protocols.add(protocols[i])
|
# 异常时不加入 done_protocols,以便触发 API fallback
|
||||||
|
|
||||||
fallback_protocols = []
|
fallback_protocols = []
|
||||||
for protocol, html in zip(protocols, htmls):
|
for protocol, html in zip(protocols, htmls):
|
||||||
|
|||||||
@@ -42,15 +42,17 @@ class SpeedXPlugin(BaseHTTPPlugin):
|
|||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line or ":" not in line:
|
if not line or ":" not in line:
|
||||||
continue
|
continue
|
||||||
parts = line.split(":")
|
ip, _, port = line.rpartition(":")
|
||||||
if len(parts) >= 2:
|
ip = ip.strip()
|
||||||
ip = parts[0].strip()
|
port = port.strip()
|
||||||
port = parts[1].strip()
|
if not re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
|
||||||
if not re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
|
continue
|
||||||
continue
|
if not port.isdigit() or not (1 <= int(port) <= 65535):
|
||||||
if not port.isdigit() or not (1 <= int(port) <= 65535):
|
continue
|
||||||
continue
|
try:
|
||||||
results.append(ProxyRaw(ip, int(port), protocol))
|
results.append(ProxyRaw(ip, int(port), protocol))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
return results
|
return results
|
||||||
|
|
||||||
async def crawl(self) -> List[ProxyRaw]:
|
async def crawl(self) -> List[ProxyRaw]:
|
||||||
|
|||||||
@@ -40,17 +40,18 @@ class YunDaiLiPlugin(BaseHTTPPlugin):
|
|||||||
line = line.strip()
|
line = line.strip()
|
||||||
if not line or ":" not in line:
|
if not line or ":" not in line:
|
||||||
continue
|
continue
|
||||||
parts = line.split(":")
|
ip, _, port_str = line.rpartition(":")
|
||||||
if len(parts) < 2:
|
ip = ip.strip()
|
||||||
continue
|
port_str = port_str.strip()
|
||||||
ip = parts[0].strip()
|
|
||||||
port_str = parts[1].strip()
|
|
||||||
if not re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
|
if not re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
|
||||||
continue
|
continue
|
||||||
if not port_str.isdigit() or not (1 <= int(port_str) <= 65535):
|
if not port_str.isdigit() or not (1 <= int(port_str) <= 65535):
|
||||||
continue
|
continue
|
||||||
final_protocol = protocol if protocol in VALID_PROTOCOLS else "http"
|
final_protocol = protocol if protocol in VALID_PROTOCOLS else "http"
|
||||||
results.append(ProxyRaw(ip, int(port_str), final_protocol))
|
try:
|
||||||
|
results.append(ProxyRaw(ip, int(port_str), final_protocol))
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
if count:
|
if count:
|
||||||
|
|||||||
@@ -76,23 +76,23 @@ class ProxyRepository:
|
|||||||
max_score: int = 100,
|
max_score: int = 100,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
try:
|
try:
|
||||||
async with db.execute(
|
# 原子更新:计算新分数并直接更新
|
||||||
"SELECT score FROM proxies WHERE ip = ? AND port = ?", (ip, port)
|
await db.execute(
|
||||||
) as cursor:
|
"""
|
||||||
row = await cursor.fetchone()
|
UPDATE proxies
|
||||||
if not row:
|
SET score = MAX(?, MIN(?, score + ?)),
|
||||||
return False
|
last_check = CURRENT_TIMESTAMP
|
||||||
current_score = row[0]
|
WHERE ip = ? AND port = ?
|
||||||
new_score = max(min_score, min(max_score, current_score + delta))
|
""",
|
||||||
await db.execute(
|
(min_score, max_score, delta, ip, port),
|
||||||
"UPDATE proxies SET score = ?, last_check = CURRENT_TIMESTAMP WHERE ip = ? AND port = ?",
|
)
|
||||||
(new_score, ip, port),
|
# 删除分数已降至 0 及以下的代理
|
||||||
)
|
await db.execute(
|
||||||
if new_score <= 0:
|
"DELETE FROM proxies WHERE ip = ? AND port = ? AND score <= ?",
|
||||||
# 只删除当前代理,避免误删其他无效代理
|
(ip, port, min_score),
|
||||||
await db.execute("DELETE FROM proxies WHERE ip = ? AND port = ?", (ip, port))
|
)
|
||||||
await db.commit()
|
await db.commit()
|
||||||
return True
|
return db.total_changes > 0
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"update_score failed: {e}", exc_info=True)
|
logger.error(f"update_score failed: {e}", exc_info=True)
|
||||||
return False
|
return False
|
||||||
@@ -156,19 +156,35 @@ class ProxyRepository:
|
|||||||
db: aiosqlite.Connection,
|
db: aiosqlite.Connection,
|
||||||
protocol: Optional[str] = None,
|
protocol: Optional[str] = None,
|
||||||
limit: int = 100000,
|
limit: int = 100000,
|
||||||
|
offset: int = 0,
|
||||||
) -> List[Proxy]:
|
) -> List[Proxy]:
|
||||||
query = "SELECT ip, port, protocol, score, response_time_ms, last_check, created_at FROM proxies"
|
query = "SELECT ip, port, protocol, score, response_time_ms, last_check, created_at FROM proxies"
|
||||||
params: List = []
|
params: List = []
|
||||||
if protocol:
|
if protocol:
|
||||||
query += " WHERE protocol = ?"
|
query += " WHERE protocol = ?"
|
||||||
params.append(protocol.lower())
|
params.append(protocol.lower())
|
||||||
query += " LIMIT ?"
|
query += " LIMIT ? OFFSET ?"
|
||||||
params.append(limit)
|
params.extend([limit, offset])
|
||||||
|
|
||||||
async with db.execute(query, params) as cursor:
|
async with db.execute(query, params) as cursor:
|
||||||
rows = await cursor.fetchall()
|
rows = await cursor.fetchall()
|
||||||
return [_row_to_proxy(row) for row in rows]
|
return [_row_to_proxy(row) for row in rows]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def iter_batches(
|
||||||
|
db: aiosqlite.Connection,
|
||||||
|
protocol: Optional[str] = None,
|
||||||
|
batch_size: int = 1000,
|
||||||
|
):
|
||||||
|
"""流式分批读取代理,避免一次性加载大量数据到内存"""
|
||||||
|
offset = 0
|
||||||
|
while True:
|
||||||
|
batch = await ProxyRepository.list_all(db, protocol, batch_size, offset)
|
||||||
|
if not batch:
|
||||||
|
break
|
||||||
|
yield batch
|
||||||
|
offset += batch_size
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def list_paginated(
|
async def list_paginated(
|
||||||
db: aiosqlite.Connection,
|
db: aiosqlite.Connection,
|
||||||
|
|||||||
@@ -24,10 +24,9 @@ class PluginService:
|
|||||||
|
|
||||||
result = []
|
result = []
|
||||||
for plugin in registry.list_plugins():
|
for plugin in registry.list_plugins():
|
||||||
# 合并持久化状态
|
# 合并持久化状态(不修改全局实例,避免并发竞争)
|
||||||
state = db_states.get(plugin.name, {})
|
state = db_states.get(plugin.name, {})
|
||||||
if "enabled" in state:
|
enabled = state.get("enabled", plugin.enabled)
|
||||||
plugin.enabled = state["enabled"]
|
|
||||||
if "config" in state and isinstance(state["config"], dict):
|
if "config" in state and isinstance(state["config"], dict):
|
||||||
plugin.update_config(state["config"])
|
plugin.update_config(state["config"])
|
||||||
|
|
||||||
@@ -50,7 +49,7 @@ class PluginService:
|
|||||||
name=plugin.name,
|
name=plugin.name,
|
||||||
display_name=plugin.display_name or plugin.name,
|
display_name=plugin.display_name or plugin.name,
|
||||||
description=plugin.description or f"从 {plugin.name} 爬取代理",
|
description=plugin.description or f"从 {plugin.name} 爬取代理",
|
||||||
enabled=plugin.enabled,
|
enabled=enabled,
|
||||||
last_run=stat.get("last_run"),
|
last_run=stat.get("last_run"),
|
||||||
success_count=stat.get("success_count", 0),
|
success_count=stat.get("success_count", 0),
|
||||||
failure_count=stat.get("failure_count", 0),
|
failure_count=stat.get("failure_count", 0),
|
||||||
@@ -133,11 +132,11 @@ class PluginService:
|
|||||||
logger.error(f"Run all plugins error: {results}")
|
logger.error(f"Run all plugins error: {results}")
|
||||||
continue
|
continue
|
||||||
all_results.extend(results)
|
all_results.extend(results)
|
||||||
# 去重
|
# 去重(与数据库 UNIQUE(ip, port) 约束保持一致)
|
||||||
seen = set()
|
seen = set()
|
||||||
unique = []
|
unique = []
|
||||||
for p in all_results:
|
for p in all_results:
|
||||||
key = (p.ip, p.port, p.protocol)
|
key = (p.ip, p.port)
|
||||||
if key not in seen:
|
if key not in seen:
|
||||||
seen.add(key)
|
seen.add(key)
|
||||||
unique.append(p)
|
unique.append(p)
|
||||||
|
|||||||
@@ -61,28 +61,41 @@ class ProxyService:
|
|||||||
protocol: Optional[str] = None,
|
protocol: Optional[str] = None,
|
||||||
limit: int = 10000,
|
limit: int = 10000,
|
||||||
) -> AsyncIterator[str]:
|
) -> AsyncIterator[str]:
|
||||||
async with get_db() as db:
|
|
||||||
proxies = await self.proxy_repo.list_all(db, protocol=protocol, limit=limit)
|
|
||||||
|
|
||||||
if fmt == "csv":
|
if fmt == "csv":
|
||||||
yield "IP,Port,Protocol,Score,Last Check\n"
|
yield "\ufeffIP,Port,Protocol,Score,Last Check\n"
|
||||||
for p in proxies:
|
|
||||||
yield f"{p.ip},{p.port},{p.protocol},{p.score},{self._fmt_time(p.last_check)}\n"
|
|
||||||
elif fmt == "txt":
|
elif fmt == "txt":
|
||||||
for p in proxies:
|
pass
|
||||||
yield f"{p.ip}:{p.port}\n"
|
|
||||||
elif fmt == "json":
|
elif fmt == "json":
|
||||||
data = [
|
yield "["
|
||||||
{
|
first = True
|
||||||
"ip": p.ip,
|
|
||||||
"port": p.port,
|
exported = 0
|
||||||
"protocol": p.protocol,
|
async with get_db() as db:
|
||||||
"score": p.score,
|
async for batch in self.proxy_repo.iter_batches(db, protocol=protocol, batch_size=1000):
|
||||||
"last_check": self._fmt_time(p.last_check),
|
for p in batch:
|
||||||
}
|
if exported >= limit:
|
||||||
for p in proxies
|
break
|
||||||
]
|
if fmt == "csv":
|
||||||
yield json.dumps(data, ensure_ascii=False, indent=2)
|
yield f"{p.ip},{p.port},{p.protocol},{p.score},{self._fmt_time(p.last_check)}\n"
|
||||||
|
elif fmt == "txt":
|
||||||
|
yield f"{p.ip}:{p.port}\n"
|
||||||
|
elif fmt == "json":
|
||||||
|
item = {
|
||||||
|
"ip": p.ip,
|
||||||
|
"port": p.port,
|
||||||
|
"protocol": p.protocol,
|
||||||
|
"score": p.score,
|
||||||
|
"last_check": self._fmt_time(p.last_check),
|
||||||
|
}
|
||||||
|
prefix = "" if first else ","
|
||||||
|
yield prefix + json.dumps(item, ensure_ascii=False)
|
||||||
|
first = False
|
||||||
|
exported += 1
|
||||||
|
if exported >= limit:
|
||||||
|
break
|
||||||
|
|
||||||
|
if fmt == "json":
|
||||||
|
yield "]"
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _fmt_time(dt: Optional[datetime]) -> str:
|
def _fmt_time(dt: Optional[datetime]) -> str:
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from app.core.tasks.queue import ValidationQueue
|
|||||||
from app.core.config import settings as app_settings
|
from app.core.config import settings as app_settings
|
||||||
from app.core.log import logger
|
from app.core.log import logger
|
||||||
from app.models.domain import ProxyRaw
|
from app.models.domain import ProxyRaw
|
||||||
|
from app.services.task_service import task_service
|
||||||
|
|
||||||
|
|
||||||
class SchedulerService:
|
class SchedulerService:
|
||||||
@@ -58,16 +59,15 @@ class SchedulerService:
|
|||||||
"""立即执行一次全量验证(后台运行,不阻塞)"""
|
"""立即执行一次全量验证(后台运行,不阻塞)"""
|
||||||
if self._validate_task and not self._validate_task.done():
|
if self._validate_task and not self._validate_task.done():
|
||||||
return
|
return
|
||||||
self._validate_task = asyncio.create_task(self._do_validate_all())
|
self._validate_task = asyncio.create_task(self._do_validate_all(from_loop=False))
|
||||||
|
|
||||||
async def _run_loop(self):
|
async def _run_loop(self):
|
||||||
"""定时循环"""
|
"""定时循环"""
|
||||||
while self.running:
|
while self.running:
|
||||||
try:
|
try:
|
||||||
# 清理过期任务,防止内存无限增长
|
# 清理过期任务,防止内存无限增长
|
||||||
from app.services.task_service import task_service
|
|
||||||
task_service.cleanup_old_tasks()
|
task_service.cleanup_old_tasks()
|
||||||
await self._do_validate_all()
|
await self._do_validate_all(from_loop=True)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Scheduler loop error: {e}", exc_info=True)
|
logger.error(f"Scheduler loop error: {e}", exc_info=True)
|
||||||
# 等待下一次
|
# 等待下一次
|
||||||
@@ -76,10 +76,17 @@ class SchedulerService:
|
|||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def _do_validate_all(self):
|
async def _do_validate_all(self, from_loop: bool = True):
|
||||||
"""验证数据库中所有存量代理"""
|
"""验证数据库中所有存量代理"""
|
||||||
|
queue_started_here = False
|
||||||
try:
|
try:
|
||||||
logger.info("Starting scheduled validation for all proxies")
|
logger.info("Starting scheduled validation for all proxies")
|
||||||
|
|
||||||
|
# 如果队列未运行,临时启动它(适用于 validate_all_now 在调度器停止时调用)
|
||||||
|
if not self.validation_queue._running:
|
||||||
|
await self.validation_queue.start()
|
||||||
|
queue_started_here = True
|
||||||
|
|
||||||
async with get_db() as db:
|
async with get_db() as db:
|
||||||
# 清理 7 天前的验证任务记录,防止表无限增长
|
# 清理 7 天前的验证任务记录,防止表无限增长
|
||||||
cleaned = await ValidationTaskRepository.cleanup_old(db, days=7)
|
cleaned = await ValidationTaskRepository.cleanup_old(db, days=7)
|
||||||
@@ -95,7 +102,7 @@ class SchedulerService:
|
|||||||
batch_size = 100
|
batch_size = 100
|
||||||
total_batches = (len(proxies) - 1) // batch_size + 1
|
total_batches = (len(proxies) - 1) // batch_size + 1
|
||||||
for i in range(0, len(proxies), batch_size):
|
for i in range(0, len(proxies), batch_size):
|
||||||
if not self.running:
|
if from_loop and not self.running:
|
||||||
break
|
break
|
||||||
batch = proxies[i : i + batch_size]
|
batch = proxies[i : i + batch_size]
|
||||||
await self.validation_queue.submit([
|
await self.validation_queue.submit([
|
||||||
@@ -106,3 +113,6 @@ class SchedulerService:
|
|||||||
logger.info("Scheduled validation batches submitted")
|
logger.info("Scheduled validation batches submitted")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Scheduled validation error: {e}", exc_info=True)
|
logger.error(f"Scheduled validation error: {e}", exc_info=True)
|
||||||
|
finally:
|
||||||
|
if queue_started_here:
|
||||||
|
await self.validation_queue.stop()
|
||||||
|
|||||||
@@ -95,17 +95,13 @@ class ValidatorService:
|
|||||||
timeout = aiohttp.ClientTimeout(total=self.timeout, connect=self.connect_timeout)
|
timeout = aiohttp.ClientTimeout(total=self.timeout, connect=self.connect_timeout)
|
||||||
test_url = self._get_test_url("http")
|
test_url = self._get_test_url("http")
|
||||||
|
|
||||||
try:
|
async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
|
||||||
async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
|
async with session.get(test_url, allow_redirects=True) as response:
|
||||||
async with session.get(test_url, allow_redirects=True) as response:
|
if response.status in (200, 301, 302):
|
||||||
if response.status in (200, 301, 302):
|
latency = round((time.time() - start) * 1000, 2)
|
||||||
latency = round((time.time() - start) * 1000, 2)
|
logger.info(f"SOCKS valid: {ip}:{port} ({protocol}) {latency}ms")
|
||||||
logger.info(f"SOCKS valid: {ip}:{port} ({protocol}) {latency}ms")
|
return True, latency
|
||||||
return True, latency
|
return False, 0.0
|
||||||
return False, 0.0
|
|
||||||
finally:
|
|
||||||
# ClientSession 的 async with 退出时会自动关闭 connector,无需手动重复关闭
|
|
||||||
pass
|
|
||||||
|
|
||||||
async def close(self):
|
async def close(self):
|
||||||
"""关闭共享的 HTTP ClientSession"""
|
"""关闭共享的 HTTP ClientSession"""
|
||||||
|
|||||||
@@ -14,4 +14,4 @@ markers =
|
|||||||
e2e: 端到端测试
|
e2e: 端到端测试
|
||||||
slow: 慢速测试
|
slow: 慢速测试
|
||||||
async_test: 异步测试
|
async_test: 异步测试
|
||||||
asyncio_mode = auto
|
asyncio_default_fixture_loop_scope = function
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ aiohttp-socks==0.9.1
|
|||||||
beautifulsoup4==4.12.3
|
beautifulsoup4==4.12.3
|
||||||
lxml==5.1.0
|
lxml==5.1.0
|
||||||
pydantic-settings==2.8.1
|
pydantic-settings==2.8.1
|
||||||
|
httpx==0.27.0
|
||||||
|
|||||||
@@ -68,6 +68,37 @@ class TestProxyRepository:
|
|||||||
# 清理
|
# 清理
|
||||||
await proxy_repo.delete(db, "192.168.1.1", 8080)
|
await proxy_repo.delete(db, "192.168.1.1", 8080)
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_update_score_deletes_on_zero_or_below(self, db, proxy_repo):
|
||||||
|
"""测试分数降至 0 及以下时自动删除代理"""
|
||||||
|
await proxy_repo.insert_or_update(db, "192.168.1.1", 8080, "http", 5)
|
||||||
|
|
||||||
|
result = await proxy_repo.update_score(db, "192.168.1.1", 8080, -10)
|
||||||
|
assert result is True
|
||||||
|
|
||||||
|
proxy = await proxy_repo.get_by_ip_port(db, "192.168.1.1", 8080)
|
||||||
|
assert proxy is None
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_iter_batches(self, db, proxy_repo):
|
||||||
|
"""测试流式分批读取"""
|
||||||
|
# 插入 5 条测试数据
|
||||||
|
for i in range(5):
|
||||||
|
await proxy_repo.insert_or_update(db, f"192.168.1.{i}", 8000 + i, "http", 10)
|
||||||
|
|
||||||
|
batches = []
|
||||||
|
async for batch in proxy_repo.iter_batches(db, batch_size=2):
|
||||||
|
batches.append(batch)
|
||||||
|
|
||||||
|
assert len(batches) == 3
|
||||||
|
assert len(batches[0]) == 2
|
||||||
|
assert len(batches[1]) == 2
|
||||||
|
assert len(batches[2]) == 1
|
||||||
|
|
||||||
|
# 清理
|
||||||
|
for i in range(5):
|
||||||
|
await proxy_repo.delete(db, f"192.168.1.{i}", 8000 + i)
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_batch_delete(self, db, proxy_repo):
|
async def test_batch_delete(self, db, proxy_repo):
|
||||||
"""测试批量删除"""
|
"""测试批量删除"""
|
||||||
|
|||||||
Reference in New Issue
Block a user