优化代理获取和登录逻辑
- 每个账号独立获取代理,不再共用一个代理IP - 代理预检失败只标记该账号失败,不终止整个批次 - 代理验证改为并发:verify_proxies_concurrent 多IP同时验证 - resolve_working_proxy 多IP并发验证,找到可用即返回 - parse_proxy_response 改为返回列表,支持多行/逗号分隔 - 邮箱验证码匹配收件人地址,防止多账号并发取错验证码 - 修正 mail.bdhg.xyz 旧数据端口和SSL配置 - bdhg.xyz 默认配置改为 143端口非SSL Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
9f6b405571
commit
8fdf8d4b69
+51
-8
@@ -164,6 +164,51 @@ def verify_proxy_url(proxy_url: str, timeout: tuple = (4, 6)) -> tuple[bool, str
|
||||
return False, '代理验证失败(所有目标不可达)'
|
||||
|
||||
|
||||
def verify_proxies_concurrent(proxy_urls: list[str], timeout: tuple = (4, 6), max_workers: int = 5) -> tuple[Optional[str], str]:
|
||||
"""
|
||||
并发验证多个代理URL,返回第一个可用的。
|
||||
|
||||
Args:
|
||||
proxy_urls: 代理URL列表
|
||||
timeout: 验证超时
|
||||
max_workers: 最大并发数
|
||||
|
||||
Returns:
|
||||
(可用的代理URL, 消息)
|
||||
"""
|
||||
if not proxy_urls:
|
||||
return None, '无代理可验证'
|
||||
|
||||
if len(proxy_urls) == 1:
|
||||
ok, msg = verify_proxy_url(proxy_urls[0], timeout)
|
||||
if ok:
|
||||
return proxy_urls[0], msg
|
||||
return None, msg
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
with ThreadPoolExecutor(max_workers=min(max_workers, len(proxy_urls))) as executor:
|
||||
future_map = {
|
||||
executor.submit(verify_proxy_url, p, timeout): p
|
||||
for p in proxy_urls
|
||||
}
|
||||
for future in as_completed(future_map):
|
||||
proxy_url = future_map[future]
|
||||
try:
|
||||
ok, msg = future.result()
|
||||
if ok:
|
||||
logger.success(f"并发验证找到可用代理: {proxy_url}")
|
||||
# 取消剩余任务
|
||||
for f in future_map:
|
||||
if f != future:
|
||||
f.cancel()
|
||||
return proxy_url, msg
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
return None, f'共 {len(proxy_urls)} 个代理均不可用'
|
||||
|
||||
|
||||
def resolve_working_proxy(
|
||||
api_url: str,
|
||||
whitelist_uid: str = "",
|
||||
@@ -210,14 +255,12 @@ def resolve_working_proxy(
|
||||
proxy_urls, whitelist_ip = parse_proxy_response(text)
|
||||
|
||||
if proxy_urls:
|
||||
log('info', f'获取到 {len(proxy_urls)} 个代理,逐一验证')
|
||||
for proxy_url in proxy_urls:
|
||||
ok, msg = verify_proxy_url(proxy_url)
|
||||
if ok:
|
||||
log('success', f'代理预检成功: {proxy_url}')
|
||||
return proxy_url, msg
|
||||
log('warning', f'代理 {proxy_url} 不可用: {msg}')
|
||||
last_error = f'共 {len(proxy_urls)} 个代理均不可用'
|
||||
log('info', f'获取到 {len(proxy_urls)} 个代理,并发验证')
|
||||
proxy_url, msg = verify_proxies_concurrent(proxy_urls)
|
||||
if proxy_url:
|
||||
log('success', f'代理预检成功: {proxy_url}')
|
||||
return proxy_url, msg
|
||||
last_error = msg
|
||||
log('warning', f'代理预检 {attempt}/{max_attempts}: {last_error}')
|
||||
continue
|
||||
|
||||
|
||||
Reference in New Issue
Block a user