修复问题: - 添加缺失的 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 个测试通过
83 lines
3.4 KiB
Python
83 lines
3.4 KiB
Python
import re
|
||
from typing import List
|
||
from app.core.plugin_system import ProxyRaw
|
||
from app.plugins.base import BaseHTTPPlugin
|
||
from app.core.log import logger
|
||
|
||
VALID_PROTOCOLS = ("http", "https", "socks4", "socks5")
|
||
|
||
|
||
class YunDaiLiPlugin(BaseHTTPPlugin):
|
||
default_config = {"max_pages": 5}
|
||
name = "yundaili"
|
||
display_name = "云代理"
|
||
description = "从 GitHub 公开代理列表获取免费代理"
|
||
|
||
def __init__(self):
|
||
super().__init__()
|
||
# 主数据源:GitHub raw
|
||
self.urls = [
|
||
("http", "https://raw.githubusercontent.com/mmpx12/proxy-list/master/http.txt"),
|
||
("socks4", "https://raw.githubusercontent.com/mmpx12/proxy-list/master/socks4.txt"),
|
||
("socks5", "https://raw.githubusercontent.com/mmpx12/proxy-list/master/socks5.txt"),
|
||
]
|
||
# Fallback:jsdelivr CDN 加速
|
||
self.fallback_urls = [
|
||
("http", "https://cdn.jsdelivr.net/gh/mmpx12/proxy-list@master/http.txt"),
|
||
("socks4", "https://cdn.jsdelivr.net/gh/mmpx12/proxy-list@master/socks4.txt"),
|
||
("socks5", "https://cdn.jsdelivr.net/gh/mmpx12/proxy-list@master/socks5.txt"),
|
||
]
|
||
|
||
def _parse_htmls(self, htmls: List[str], url_mapping: List[tuple]) -> List[ProxyRaw]:
|
||
results: List[ProxyRaw] = []
|
||
for (protocol, _), html in zip(url_mapping, htmls):
|
||
if not html:
|
||
logger.warning(f"{self.display_name} {protocol.upper()} 返回空内容,可能网络受限或源已失效")
|
||
continue
|
||
|
||
count = 0
|
||
for line in html.splitlines():
|
||
line = line.strip()
|
||
if not line or ":" not in line:
|
||
continue
|
||
ip, _, port_str = line.rpartition(":")
|
||
ip = ip.strip()
|
||
port_str = port_str.strip()
|
||
if not re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
|
||
continue
|
||
if not port_str.isdigit() or not (1 <= int(port_str) <= 65535):
|
||
continue
|
||
final_protocol = protocol if protocol in VALID_PROTOCOLS else "http"
|
||
try:
|
||
results.append(ProxyRaw(ip, int(port_str), final_protocol))
|
||
except ValueError:
|
||
continue
|
||
count += 1
|
||
|
||
if count:
|
||
logger.info(f"{self.display_name} {protocol.upper()} 解析完成,获取 {count} 个潜在代理")
|
||
return results
|
||
|
||
async def crawl(self) -> List[ProxyRaw]:
|
||
results: List[ProxyRaw] = []
|
||
|
||
# 顺序请求主源,避免某个 URL 卡住拖慢整体
|
||
for protocol, url in self.urls:
|
||
html = await self.fetch(url, timeout=12)
|
||
if html:
|
||
results.extend(self._parse_htmls([html], [(protocol, url)]))
|
||
|
||
# 主源为空时尝试 fallback(也顺序请求)
|
||
if not results:
|
||
logger.warning(f"{self.display_name} GitHub 主源全部返回空,尝试 jsdelivr fallback")
|
||
for protocol, url in self.fallback_urls:
|
||
html = await self.fetch(url, timeout=12)
|
||
if html:
|
||
results.extend(self._parse_htmls([html], [(protocol, url)]))
|
||
|
||
if results:
|
||
logger.info(f"{self.display_name} 总计解析完成,获取 {len(results)} 个潜在代理")
|
||
else:
|
||
logger.warning(f"{self.display_name} 未获取到任何代理")
|
||
return results
|