优化代理管理与增加请求日志系统
代理优化(P0+P1): - 修复极验失败后代理刷新空操作bug(共享ProxyManager+白名单参数) - 极验请求超时从(3.05,12)调大到(10,30) - 白名单sync_ip加全局锁防并发限流,保留多个出口IP应对漂移 - 代理池缓存共享:Condition防并发获取+mark_bad移除坏代理 - 适配代理API的JSON响应格式(code/data/白名单错误) - 简化代理验证只验斗鱼主站,减少日志噪音 - 获取代理前主动同步白名单(解决ow=1模式不报白名单错误的问题) - 每次重试重新检测出口IP并同步白名单 请求日志系统: - 新增HttpLogger记录请求/响应详情到JSONL文件 - login.py的_request和proxy.py的verify_proxy_url接入日志 - 新增/api/logs路由查看和清空HTTP详情日志 - 前端新增请求日志页面(筛选/搜索/分页/自动刷新/详情查看) 其他: - 添加pysocks依赖支持SOCKS5代理 - gitignore添加*.log
This commit is contained in:
+75
-38
@@ -2,6 +2,7 @@
|
||||
|
||||
import json
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
from typing import Optional
|
||||
from urllib.parse import urlencode
|
||||
@@ -9,6 +10,9 @@ from urllib.parse import urlencode
|
||||
import requests
|
||||
from loguru import logger
|
||||
|
||||
# 全局锁:防止多个并发登录任务同时同步白名单,触发代理服务商的30秒限流
|
||||
_whitelist_sync_lock = threading.Lock()
|
||||
|
||||
|
||||
class WhitelistManager:
|
||||
"""协固代理IP白名单管理器"""
|
||||
@@ -165,60 +169,65 @@ class WhitelistManager:
|
||||
records = self.get_whitelist_json()
|
||||
return [r for r in records if r.get("MEMO") == self._memo]
|
||||
|
||||
def sync_ip(self, current_ip: str) -> tuple[bool, str]:
|
||||
def sync_ip(self, current_ip: str, keep_recent: int = 3) -> tuple[bool, str]:
|
||||
"""
|
||||
同步白名单IP
|
||||
同步白名单IP(保留多个近期出口IP,应对移动网络IP漂移)
|
||||
|
||||
检查当前备注是否有记录:
|
||||
- 如果IP相同,无需操作
|
||||
- 如果IP不同,删除旧的并添加新的
|
||||
- 如果IP已存在但备注不同(如手动添加无备注),删除后重新添加
|
||||
- 如果无记录,添加新的
|
||||
策略:
|
||||
- 当前IP已在白名单(同备注),无需操作
|
||||
- 当前IP不在白名单,添加(不删旧的,保留多个出口IP)
|
||||
- 超过 keep_recent 个同备注IP时,删除最老的(按列表顺序)
|
||||
- 当前IP已存在但备注不同,不处理(避免误删他人配置)
|
||||
|
||||
线程安全:使用全局锁串行化同步操作,避免并发调用触发代理服务商限流。
|
||||
|
||||
Args:
|
||||
current_ip: 当前出口IP
|
||||
keep_recent: 保留的同备注IP数量上限(默认3个,应对IP漂移)
|
||||
|
||||
Returns:
|
||||
(是否成功, 消息)
|
||||
"""
|
||||
try:
|
||||
existing_ip = self.get_memo_ip()
|
||||
with _whitelist_sync_lock:
|
||||
try:
|
||||
records = self.get_whitelist_json()
|
||||
memo_records = [r for r in records if r.get("MEMO") == self._memo]
|
||||
memo_ips = [r.get("IP") for r in memo_records]
|
||||
|
||||
# IP相同,无需更新
|
||||
if existing_ip == current_ip:
|
||||
msg = f"白名单IP已是最新的: {current_ip}"
|
||||
logger.info(msg)
|
||||
return True, msg
|
||||
# 当前IP已在白名单(同备注),无需操作
|
||||
if current_ip in memo_ips:
|
||||
msg = f"白名单IP已是最新的: {current_ip}"
|
||||
logger.info(msg)
|
||||
return True, msg
|
||||
|
||||
# 有旧记录,先删除
|
||||
if existing_ip:
|
||||
logger.info(f"白名单IP变化: {existing_ip} -> {current_ip}")
|
||||
self.delete_ip(existing_ip)
|
||||
time.sleep(1)
|
||||
# 当前IP已存在但备注不同,不处理(避免误删他人配置)
|
||||
if any(r.get('IP') == current_ip for r in records):
|
||||
logger.info(f"白名单IP {current_ip} 已存在(备注不同),无需重复添加")
|
||||
return True, f"白名单IP已存在: {current_ip}"
|
||||
|
||||
# IP已存在但备注不同(或备注为空),先删除后重新添加
|
||||
records = self.get_whitelist_json()
|
||||
if any(r.get('IP') == current_ip for r in records):
|
||||
logger.info(f"白名单IP {current_ip} 已存在但备注不同,删除后重新添加")
|
||||
self.delete_ip(current_ip)
|
||||
time.sleep(1)
|
||||
# 超过上限,删除最老的(列表前面的)
|
||||
if len(memo_ips) >= keep_recent:
|
||||
# 删除最早添加的(列表顺序)
|
||||
to_delete = memo_ips[:len(memo_ips) - keep_recent + 1]
|
||||
for old_ip in to_delete:
|
||||
logger.info(f"白名单同备注IP超限,删除旧的: {old_ip}")
|
||||
self.delete_ip(old_ip)
|
||||
time.sleep(1)
|
||||
|
||||
# 添加新IP
|
||||
ok, resp = self.add_ip(current_ip)
|
||||
if ok:
|
||||
if existing_ip:
|
||||
msg = f"白名单IP已更新: {existing_ip} -> {current_ip}"
|
||||
else:
|
||||
# 添加新IP
|
||||
logger.info(f"白名单添加新出口IP: {current_ip} (当前 {len(memo_ips)} 个同备注)")
|
||||
ok, resp = self.add_ip(current_ip)
|
||||
if ok:
|
||||
msg = f"白名单IP已添加: {current_ip}"
|
||||
logger.info(msg)
|
||||
return True, msg
|
||||
logger.info(msg)
|
||||
return True, msg
|
||||
|
||||
return False, f"白名单添加失败,API响应: {resp}"
|
||||
return False, f"白名单添加失败,API响应: {resp}"
|
||||
|
||||
except Exception as e:
|
||||
msg = f"白名单同步失败: {e}"
|
||||
logger.error(msg)
|
||||
return False, msg
|
||||
except Exception as e:
|
||||
msg = f"白名单同步失败: {e}"
|
||||
logger.error(msg)
|
||||
return False, msg
|
||||
|
||||
def test_connection(self) -> tuple[bool, str]:
|
||||
"""
|
||||
@@ -242,6 +251,34 @@ class WhitelistManager:
|
||||
return False, msg
|
||||
|
||||
|
||||
def get_local_exit_ip() -> Optional[str]:
|
||||
"""
|
||||
获取本机当前公网出口IP(不走代理)。
|
||||
|
||||
用于在获取代理前主动同步白名单,避免出口IP漂移导致代理拒绝连接。
|
||||
|
||||
Returns:
|
||||
出口IP地址,获取失败返回 None
|
||||
"""
|
||||
targets = [
|
||||
"https://myip.ipip.net",
|
||||
"https://4.ipw.cn",
|
||||
"https://api.ipify.org",
|
||||
]
|
||||
for url in targets:
|
||||
try:
|
||||
response = requests.get(url, timeout=8, headers={"User-Agent": "Mozilla/5.0"})
|
||||
response.raise_for_status()
|
||||
text = response.text.strip()
|
||||
# 从文本中提取IP
|
||||
match = re.search(r'(\d{1,3}(?:\.\d{1,3}){3})', text)
|
||||
if match:
|
||||
return match.group(1)
|
||||
except Exception:
|
||||
continue
|
||||
return None
|
||||
|
||||
|
||||
def get_exit_ip_via_proxy(proxy: str) -> Optional[str]:
|
||||
"""
|
||||
通过代理获取出口IP
|
||||
|
||||
Reference in New Issue
Block a user