feat: 按账号调度自动学习并让首页实时跟上状态
This commit is contained in:
+29
-1
@@ -7,6 +7,7 @@ from urllib.parse import quote, urlparse
|
||||
import httpx
|
||||
|
||||
SCHEMES = ("http", "https", "socks4", "socks5", "socks5h")
|
||||
SOCKS_SCHEMES = ("socks4", "socks5", "socks5h")
|
||||
OFF_SCHEMES = ("", "off", "none", "direct", "close")
|
||||
POOL_SCHEMES = ("pool", "auto")
|
||||
|
||||
@@ -16,7 +17,7 @@ class ProxyError(ValueError):
|
||||
|
||||
|
||||
def parse_proxy(value: str | None) -> str | None:
|
||||
"""把代理地址规范成 httpx 可用的 URL。空值表示直连。"""
|
||||
"""把代理地址规范成内部使用的 URL。空值表示直连。"""
|
||||
text = (value or "").strip()
|
||||
if not text:
|
||||
return None
|
||||
@@ -182,6 +183,33 @@ def bind_url(account) -> str | None:
|
||||
return url_of(account)
|
||||
|
||||
|
||||
def client_proxy_args(proxy: str | None, *, asynchronous: bool = False) -> dict:
|
||||
"""httpx 不认 socks4;SOCKS 走 httpx-socks 传输,HTTP/HTTPS 仍用 proxy=。"""
|
||||
url = parse_proxy(proxy)
|
||||
if not url:
|
||||
return {}
|
||||
scheme = urlparse(url).scheme.lower()
|
||||
if scheme in SOCKS_SCHEMES:
|
||||
if asynchronous:
|
||||
from httpx_socks import AsyncProxyTransport
|
||||
|
||||
return {"transport": AsyncProxyTransport.from_url(url)}
|
||||
from httpx_socks import SyncProxyTransport
|
||||
|
||||
return {"transport": SyncProxyTransport.from_url(url)}
|
||||
return {"proxy": url}
|
||||
|
||||
|
||||
def sync_httpx_client(proxy: str | None = None, **kwargs) -> httpx.Client:
|
||||
kwargs.update(client_proxy_args(proxy))
|
||||
return httpx.Client(**kwargs)
|
||||
|
||||
|
||||
def async_httpx_client(proxy: str | None = None, **kwargs) -> httpx.AsyncClient:
|
||||
kwargs.update(client_proxy_args(proxy, asynchronous=True))
|
||||
return httpx.AsyncClient(**kwargs)
|
||||
|
||||
|
||||
def is_proxy_failure(exc: BaseException) -> bool:
|
||||
if isinstance(exc, (httpx.ProxyError, httpx.ConnectError, httpx.ConnectTimeout)):
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user