style: 统一 Ruff 代码格式
This commit is contained in:
@@ -17,11 +17,9 @@ LogFunc = Callable[[str, str], None]
|
||||
class WhitelistSyncer(Protocol):
|
||||
"""代理解析流程需要的白名单能力。"""
|
||||
|
||||
def sync_ip(self, ip: str) -> tuple[bool, str]:
|
||||
...
|
||||
def sync_ip(self, ip: str) -> tuple[bool, str]: ...
|
||||
|
||||
def get_local_exit_ip(self) -> Optional[str]:
|
||||
...
|
||||
def get_local_exit_ip(self) -> Optional[str]: ...
|
||||
|
||||
|
||||
class ProxyResolver:
|
||||
@@ -62,14 +60,16 @@ class ProxyResolver:
|
||||
return
|
||||
log_method = getattr(
|
||||
logger,
|
||||
level if level in ('debug', 'info', 'warning', 'error', 'success') else 'info',
|
||||
level
|
||||
if level in ("debug", "info", "warning", "error", "success")
|
||||
else "info",
|
||||
logger.info,
|
||||
)
|
||||
log_method(message)
|
||||
|
||||
def _sync_ip(self, ip: str) -> tuple[bool, str]:
|
||||
if not self.whitelist_syncer:
|
||||
return False, '未配置白名单 UID/UKEY'
|
||||
return False, "未配置白名单 UID/UKEY"
|
||||
ok, sync_msg = self.whitelist_syncer.sync_ip(ip)
|
||||
if ok:
|
||||
self._last_synced_ip = ip
|
||||
@@ -82,14 +82,14 @@ class ProxyResolver:
|
||||
|
||||
local_ip = self.whitelist_syncer.get_local_exit_ip()
|
||||
if local_ip and local_ip != self._last_synced_ip:
|
||||
self._log('info', f"[尝试 {attempt}] 检测出口IP: {local_ip},同步白名单...")
|
||||
self._log("info", f"[尝试 {attempt}] 检测出口IP: {local_ip},同步白名单...")
|
||||
ok, sync_msg = self._sync_ip(local_ip)
|
||||
if ok:
|
||||
self._log('info', f"白名单同步成功: {sync_msg}")
|
||||
self._log("info", f"白名单同步成功: {sync_msg}")
|
||||
else:
|
||||
self._log('warning', f"白名单同步失败: {sync_msg}")
|
||||
self._log("warning", f"白名单同步失败: {sync_msg}")
|
||||
elif local_ip == self._last_synced_ip:
|
||||
self._log('debug', f"[尝试 {attempt}] 出口IP未变: {local_ip}")
|
||||
self._log("debug", f"[尝试 {attempt}] 出口IP未变: {local_ip}")
|
||||
|
||||
def fetch_verified(
|
||||
self,
|
||||
@@ -107,18 +107,18 @@ class ProxyResolver:
|
||||
|
||||
for attempt in range(1, max_attempts + 1):
|
||||
if self._is_stopped():
|
||||
return None, '任务已停止'
|
||||
return None, "任务已停止"
|
||||
if attempt > 1:
|
||||
delay = min(attempt - 1, 2)
|
||||
if self.log_func:
|
||||
self._log('info', f'等待 {delay}s 后重试...')
|
||||
self._log("info", f"等待 {delay}s 后重试...")
|
||||
if self._wait_or_stopped(delay):
|
||||
return None, '任务已停止'
|
||||
return None, "任务已停止"
|
||||
|
||||
self._sync_local_exit_ip_if_needed(attempt)
|
||||
if self._is_stopped():
|
||||
return None, '任务已停止'
|
||||
self._log('info', f'代理预检 {attempt}/{max_attempts}: 正在获取代理')
|
||||
return None, "任务已停止"
|
||||
self._log("info", f"代理预检 {attempt}/{max_attempts}: 正在获取代理")
|
||||
|
||||
try:
|
||||
response = requests.get(self.api_url, timeout=10)
|
||||
@@ -128,38 +128,49 @@ class ProxyResolver:
|
||||
proxy_urls, whitelist_ip = parse_proxy_response(text)
|
||||
|
||||
if proxy_urls:
|
||||
self._log('info', f'获取到 {len(proxy_urls)} 个代理,并发验证')
|
||||
available, msg = verify_proxies_concurrent(proxy_urls, return_all=return_all)
|
||||
self._log("info", f"获取到 {len(proxy_urls)} 个代理,并发验证")
|
||||
available, msg = verify_proxies_concurrent(
|
||||
proxy_urls, return_all=return_all
|
||||
)
|
||||
if available:
|
||||
if not return_all:
|
||||
self._log('success', f'代理预检成功: {available}')
|
||||
self._log("success", f"代理预检成功: {available}")
|
||||
return available, msg
|
||||
last_error = msg
|
||||
self._log('warning', f"代理预检 {attempt}/{max_attempts}: {last_error}")
|
||||
self._log(
|
||||
"warning", f"代理预检 {attempt}/{max_attempts}: {last_error}"
|
||||
)
|
||||
continue
|
||||
|
||||
if whitelist_ip and self.whitelist_syncer:
|
||||
if self.sync_whitelist_once and self._has_synced_whitelist:
|
||||
last_error = f'白名单已同步但代理API仍返回白名单错误: {whitelist_ip}'
|
||||
self._log('warning', last_error)
|
||||
last_error = (
|
||||
f"白名单已同步但代理API仍返回白名单错误: {whitelist_ip}"
|
||||
)
|
||||
self._log("warning", last_error)
|
||||
continue
|
||||
|
||||
self._log('warning', f'代理需要白名单IP: {whitelist_ip},自动同步...')
|
||||
self._log(
|
||||
"warning", f"代理需要白名单IP: {whitelist_ip},自动同步..."
|
||||
)
|
||||
ok, sync_msg = self._sync_ip(whitelist_ip)
|
||||
self._log('success' if ok else 'error', f'白名单同步: {sync_msg}')
|
||||
self._log("success" if ok else "error", f"白名单同步: {sync_msg}")
|
||||
if ok:
|
||||
self._log('info', '白名单已更新,立即重试...')
|
||||
self._log("info", "白名单已更新,立即重试...")
|
||||
continue
|
||||
return None, f'白名单同步失败: {sync_msg}'
|
||||
return None, f"白名单同步失败: {sync_msg}"
|
||||
|
||||
last_error = '代理API响应无法解析'
|
||||
self._log('warning', f"代理预检 {attempt}/{max_attempts}: {last_error}: {text[:80]}")
|
||||
last_error = "代理API响应无法解析"
|
||||
self._log(
|
||||
"warning",
|
||||
f"代理预检 {attempt}/{max_attempts}: {last_error}: {text[:80]}",
|
||||
)
|
||||
|
||||
except Exception as exc:
|
||||
last_error = f'代理API请求失败: {exc}'
|
||||
self._log('warning', f"代理预检 {attempt}/{max_attempts}: {last_error}")
|
||||
last_error = f"代理API请求失败: {exc}"
|
||||
self._log("warning", f"代理预检 {attempt}/{max_attempts}: {last_error}")
|
||||
|
||||
return None, f'代理预检失败({max_attempts}次尝试均失败): {last_error}'
|
||||
return None, f"代理预检失败({max_attempts}次尝试均失败): {last_error}"
|
||||
|
||||
|
||||
def resolve_working_proxy(
|
||||
|
||||
Reference in New Issue
Block a user