feat: 自动代理每批同时领取 SOCKS5、HTTP 和 HTTPS
This commit is contained in:
+38
-25
@@ -1,10 +1,10 @@
|
||||
"""自动代理:一个任务最多向 proxy.scdn.io 领取 8 批中国代理,每批 8 个。
|
||||
"""自动代理:一个任务最多向 proxy.scdn.io 领取 8 批中国代理。
|
||||
|
||||
每批按顺序检查,第一个能打开学习平台的就用。学习过程中这个代理失效,再领下一批。
|
||||
8 批都用完任务还没结束,就改走本机网络。
|
||||
每批先要 8 个 SOCKS5,再要 8 个 HTTP,最后要 8 个 HTTPS。按这个顺序检查,第一个能打开学习平台的就用。
|
||||
学习过程中这个代理失效,再领下一批。8 批都用完任务还没结束,就改走本机网络。
|
||||
|
||||
接口的 https 表示这个代理能访问 HTTPS 网站。这类节点本身仍是 HTTP 代理,
|
||||
用 HTTP CONNECT 转发。SOCKS4 常因 ident 校验被拒绝,不用。
|
||||
接口的 https 表示这个代理能访问 HTTPS 网站,连接方式仍是 HTTP 转发。
|
||||
SOCKS4 常因 ident 校验被拒绝,不用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -27,8 +27,8 @@ LATENCY_LIMIT = 3.0
|
||||
LATENCY_LIMIT_MS = 3000
|
||||
PROBE_URL = EUNIV + "/"
|
||||
SOURCE_URL = "https://proxy.scdn.io/api/get_proxy.php"
|
||||
SOURCE_PROTOCOL = "https"
|
||||
CLIENT_SCHEME = "http"
|
||||
# 先试 SOCKS5,再试 HTTP。接口里的 https 仍是 HTTP 代理,用 HTTP CONNECT 打开学习平台。
|
||||
SOURCE_PROTOCOLS = (("socks5", "socks5"), ("http", "http"), ("https", "http"))
|
||||
|
||||
_lock = threading.Lock()
|
||||
_holds: dict[str, str] = {}
|
||||
@@ -115,18 +115,7 @@ def _parse_endpoint(value: object) -> tuple[str, int] | None:
|
||||
return host, port
|
||||
|
||||
|
||||
def _fetch_batch() -> list[tuple[str, int]]:
|
||||
"""向接口要一批中国代理。接口失败时返回空列表,这一批仍然算一次领取。"""
|
||||
try:
|
||||
with httpx.Client(timeout=10, trust_env=False, verify=shared_tls()) as client:
|
||||
response = client.get(
|
||||
SOURCE_URL,
|
||||
params={"protocol": SOURCE_PROTOCOL, "count": BATCH_SIZE, "country_code": "CN"},
|
||||
)
|
||||
payload = response.json()
|
||||
except Exception:
|
||||
log.warning("代理接口没有获取到", exc_info=True)
|
||||
return []
|
||||
def _read_proxies(payload: object) -> list[tuple[str, int]]:
|
||||
if not isinstance(payload, dict) or payload.get("code") != 200:
|
||||
return []
|
||||
data = payload.get("data")
|
||||
@@ -134,18 +123,42 @@ def _fetch_batch() -> list[tuple[str, int]]:
|
||||
if not isinstance(proxies, list):
|
||||
return []
|
||||
found: list[tuple[str, int]] = []
|
||||
seen: set[tuple[str, int]] = set()
|
||||
for item in proxies:
|
||||
parsed = _parse_endpoint(item)
|
||||
if parsed is None or parsed in seen:
|
||||
if parsed is None:
|
||||
continue
|
||||
seen.add(parsed)
|
||||
found.append(parsed)
|
||||
if len(found) >= BATCH_SIZE:
|
||||
break
|
||||
return found
|
||||
|
||||
|
||||
def _fetch_batch() -> list[tuple[str, str, int]]:
|
||||
"""向接口要一批中国代理。顺序是 SOCKS5、HTTP、HTTPS。接口失败时这一批仍然算一次领取。"""
|
||||
found: list[tuple[str, str, int]] = []
|
||||
seen: set[tuple[str, int]] = set()
|
||||
try:
|
||||
with httpx.Client(timeout=10, trust_env=False, verify=shared_tls()) as client:
|
||||
for protocol, scheme in SOURCE_PROTOCOLS:
|
||||
try:
|
||||
response = client.get(
|
||||
SOURCE_URL,
|
||||
params={"protocol": protocol, "count": BATCH_SIZE, "country_code": "CN"},
|
||||
)
|
||||
rows = _read_proxies(response.json())
|
||||
except Exception:
|
||||
log.warning("代理接口没有获取到 %s", protocol, exc_info=True)
|
||||
continue
|
||||
for host, port in rows:
|
||||
if (host, port) in seen:
|
||||
continue
|
||||
seen.add((host, port))
|
||||
found.append((scheme, host, port))
|
||||
except Exception:
|
||||
log.warning("代理接口没有获取到", exc_info=True)
|
||||
return found
|
||||
|
||||
|
||||
def force_direct(employee_id: str) -> None:
|
||||
"""这个任务不再领代理,之后的访问走本机网络。"""
|
||||
key = (employee_id or "").strip()
|
||||
@@ -183,12 +196,12 @@ def acquire(employee_id: str) -> str | None:
|
||||
return None
|
||||
_fetches[key] = used + 1
|
||||
# 同批按顺序测,第一个能打开学习平台的立即使用,不再测后面的。
|
||||
for host, port in _fetch_batch():
|
||||
ok, _latency, reason = probe(CLIENT_SCHEME, host, port)
|
||||
for scheme, host, port in _fetch_batch():
|
||||
ok, _latency, reason = probe(scheme, host, port)
|
||||
if not ok:
|
||||
log.info("代理 %s:%s 不能访问学习平台:%s", host, port, reason or "失效")
|
||||
continue
|
||||
url = build_proxy_url(CLIENT_SCHEME, host, port)
|
||||
url = build_proxy_url(scheme, host, port)
|
||||
with _lock:
|
||||
current = _holds.get(key)
|
||||
if current:
|
||||
|
||||
Reference in New Issue
Block a user