继续优化登录逻辑

This commit is contained in:
yml2213
2026-06-25 08:40:00 +08:00
parent a62e7c192f
commit 0b4fe1daa2
6 changed files with 105 additions and 48 deletions
+20 -1
View File
@@ -1,5 +1,6 @@
"""代理获取、白名单同步、可用性验证的统一流程。"""
import threading
import time
from typing import Callable, Optional, Protocol
@@ -33,15 +34,28 @@ class ProxyResolver:
log_func: Optional[LogFunc] = None,
sync_local_exit_ip: bool = False,
sync_whitelist_once: bool = True,
stop_event: Optional[threading.Event] = None,
):
self.api_url = api_url
self.whitelist_syncer = whitelist_syncer
self.log_func = log_func
self.sync_local_exit_ip = sync_local_exit_ip
self.sync_whitelist_once = sync_whitelist_once
self.stop_event = stop_event
self._last_synced_ip: Optional[str] = None
self._has_synced_whitelist = False
def _is_stopped(self) -> bool:
return bool(self.stop_event and self.stop_event.is_set())
def _wait_or_stopped(self, seconds: float) -> bool:
"""等待 seconds 秒,期间收到停止信号返回 True。"""
if self.stop_event:
return self.stop_event.wait(seconds)
if seconds > 0:
time.sleep(seconds)
return False
def _log(self, level: str, message: str) -> None:
if self.log_func:
self.log_func(level, message)
@@ -92,13 +106,18 @@ class ProxyResolver:
last_error = ""
for attempt in range(1, max_attempts + 1):
if self._is_stopped():
return None, '任务已停止'
if attempt > 1:
delay = min(attempt - 1, 2)
if self.log_func:
self._log('info', f'等待 {delay}s 后重试...')
time.sleep(delay)
if self._wait_or_stopped(delay):
return None, '任务已停止'
self._sync_local_exit_ip_if_needed(attempt)
if self._is_stopped():
return None, '任务已停止'
self._log('info', f'代理预检 {attempt}/{max_attempts}: 正在获取代理')
try: