feat: 维护中国代理池并支持账号自动均匀使用

This commit is contained in:
2026-11-15 02:18:17 +08:00
parent aa26d00c60
commit ce2d6e08dd
20 changed files with 1733 additions and 162 deletions
+59 -4
View File
@@ -1,11 +1,14 @@
"""账号级 HTTP/HTTPS/SOCKS4/SOCKS5 代理。账号密码可选。"""
"""账号级 HTTP/HTTPS/SOCKS4/SOCKS5 代理。账号密码可选。也可使用代理池。"""
from __future__ import annotations
from urllib.parse import quote, urlparse
import httpx
SCHEMES = ("http", "https", "socks4", "socks5", "socks5h")
OFF_SCHEMES = ("", "off", "none", "direct", "close")
POOL_SCHEMES = ("pool", "auto")
class ProxyError(ValueError):
@@ -66,12 +69,31 @@ def is_off(scheme: str | None) -> bool:
return (scheme or "").strip().lower() in OFF_SCHEMES
def is_pool(scheme: str | None) -> bool:
return (scheme or "").strip().lower() in POOL_SCHEMES
def uses_pool(account) -> bool:
return bool(getattr(account, "use_proxy_pool", False))
def enabled(account) -> bool:
if uses_pool(account):
return True
return bool(account.proxy_scheme and account.proxy_host and account.proxy_port)
def view_of(account) -> dict:
on = enabled(account)
if uses_pool(account):
return {
"enabled": True,
"scheme": "pool",
"host": "",
"port": None,
"username": "",
"has_password": False,
}
on = bool(account.proxy_scheme and account.proxy_host and account.proxy_port)
return {
"enabled": on,
"scheme": account.proxy_scheme if on else "off",
@@ -83,13 +105,15 @@ def view_of(account) -> dict:
def summary_of(account) -> str:
if not enabled(account):
if uses_pool(account):
return "自动"
if not (account.proxy_scheme and account.proxy_host and account.proxy_port):
return ""
return f"{account.proxy_scheme.upper()} {account.proxy_host}:{account.proxy_port}"
def url_of(account) -> str | None:
if not enabled(account):
if uses_pool(account) or not (account.proxy_scheme and account.proxy_host and account.proxy_port):
return None
password = ""
if account.proxy_password_cipher:
@@ -109,6 +133,16 @@ def apply(account, scheme: str, host: str = "", port: object = None, username: s
if is_off(scheme):
clear(account)
return
if is_pool(scheme):
clear(account)
account.use_proxy_pool = True
return
employee_id = getattr(account, "employee_id", "")
if uses_pool(account) and employee_id:
from euniv.proxy_pool import release
release(str(employee_id))
account.use_proxy_pool = False
kind, address, number, user = validate_parts(scheme, host, port, username)
if password is not None and len(str(password)) > 256:
raise ProxyError("代理密码过长")
@@ -132,3 +166,24 @@ def clear(account) -> None:
account.proxy_port = None
account.proxy_username = ""
account.proxy_password_cipher = ""
account.use_proxy_pool = False
employee_id = getattr(account, "employee_id", "")
if employee_id:
from euniv.proxy_pool import release
release(str(employee_id))
def bind_url(account) -> str | None:
if uses_pool(account):
from euniv.proxy_pool import acquire
return acquire(str(account.employee_id))
return url_of(account)
def is_proxy_failure(exc: BaseException) -> bool:
if isinstance(exc, (httpx.ProxyError, httpx.ConnectError, httpx.ConnectTimeout)):
return True
text = f"{type(exc).__name__} {exc}".lower()
return "proxy" in text or "socks" in text