- 新建 ProxyFetcher 替代 ProxyManager,每次从API取1个代理,无池无锁无冷却 - 极验最多3次尝试,失败直接抛异常回到login整体重试取新代理 - 删掉极验内部的 refresh_proxy/proxy_switches/_soft_fail_streak 逻辑 - login整体重试时取新代理从头走完整流程 - 代理验证超时从(5,8)缩短为(3,5),白名单同步后立即重试不等待 - login_service 删掉60行代理池预检/等待代码 - 修复邮件验证码日期比较:Roundcube分钟精度与秒级时间戳对齐
70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
"""简化版代理获取器(替代 ProxyManager)。
|
|
|
|
专为短效代理设计:无池、无锁、无冷却、无复用。
|
|
每次调用 fetch_new_proxy() 从 API 取 1 个新代理,用完即弃。
|
|
|
|
线程安全:白名单同步由 ProxyResolver 内部的锁保证。
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
from loguru import logger
|
|
|
|
from .proxy_resolver import ProxyResolver
|
|
from .proxy_whitelist import DouyuWhitelistSyncer
|
|
|
|
|
|
class ProxyFetcher:
|
|
"""每次从代理 API 取 1 个新代理,无缓存无复用。"""
|
|
|
|
def __init__(
|
|
self,
|
|
api_url: str,
|
|
whitelist_platform: str = "xiequ",
|
|
whitelist_credentials: dict = None,
|
|
):
|
|
self.api_url = api_url
|
|
|
|
_wl_platform = whitelist_platform or "xiequ"
|
|
_wl_credentials = whitelist_credentials
|
|
|
|
self._whitelist_syncer = (
|
|
DouyuWhitelistSyncer(platform=_wl_platform, credentials=_wl_credentials)
|
|
if _wl_credentials
|
|
else None
|
|
)
|
|
|
|
def fetch_new_proxy(self, max_attempts: int = 3) -> Optional[str]:
|
|
"""从代理 API 获取 1 个可用代理,失败返回 None。"""
|
|
resolver = ProxyResolver(
|
|
api_url=self.api_url,
|
|
whitelist_syncer=self._whitelist_syncer,
|
|
sync_local_exit_ip=bool(self._whitelist_syncer),
|
|
sync_whitelist_once=False,
|
|
)
|
|
result, msg = resolver.fetch_verified(
|
|
max_attempts=max_attempts,
|
|
return_all=False,
|
|
)
|
|
if isinstance(result, str):
|
|
logger.info(f"获取新代理: {result}")
|
|
return result
|
|
if isinstance(result, list) and result:
|
|
logger.info(f"获取新代理: {result[0]}")
|
|
return result[0]
|
|
logger.warning(f"获取代理失败: {msg}")
|
|
return None
|
|
|
|
|
|
def get_proxy_fetcher(
|
|
api_url: str,
|
|
whitelist_platform: str = "xiequ",
|
|
whitelist_credentials: dict = None,
|
|
) -> ProxyFetcher:
|
|
"""获取代理获取器实例。"""
|
|
return ProxyFetcher(
|
|
api_url,
|
|
whitelist_platform=whitelist_platform,
|
|
whitelist_credentials=whitelist_credentials,
|
|
)
|