feat: 账号可配置 HTTP/HTTPS/SOCKS 代理

This commit is contained in:
2026-11-15 01:21:17 +08:00
parent 1141df17f7
commit e9a1e9b315
15 changed files with 848 additions and 55 deletions
+108 -6
View File
@@ -1,10 +1,14 @@
"""账号级 HTTP/HTTPS/SOCKS4/SOCKS5 代理。不处理 VPN 或隧道。"""
"""账号级 HTTP/HTTPS/SOCKS4/SOCKS5 代理。账号密码可选。"""
from __future__ import annotations
from urllib.parse import urlparse
from urllib.parse import quote, urlparse
_SCHEMES = {"http", "https", "socks4", "socks5", "socks5h"}
SCHEMES = ("http", "https", "socks4", "socks5", "socks5h")
class ProxyError(ValueError):
"""代理配置不符合要求。"""
def parse_proxy(value: str | None) -> str | None:
@@ -16,8 +20,106 @@ def parse_proxy(value: str | None) -> str | None:
text = "http://" + text
parsed = urlparse(text)
scheme = (parsed.scheme or "").lower()
if scheme not in _SCHEMES:
raise ValueError("代理只支持 HTTP、HTTPS、SOCKS4、SOCKS5")
if scheme not in SCHEMES:
raise ProxyError("代理只支持 HTTP、HTTPS、SOCKS4、SOCKS5")
if not parsed.hostname:
raise ValueError("代理地址不完整")
raise ProxyError("代理地址不完整")
return text
def build_proxy_url(
scheme: str,
host: str,
port: int,
username: str = "",
password: str = "",
) -> str:
scheme, host, port, username = validate_parts(scheme, host, port, username)
if username or password:
user = quote(username, safe="")
secret = quote(password, safe="")
auth = f"{user}:{secret}@"
else:
auth = ""
return f"{scheme}://{auth}{host}:{port}"
def validate_parts(scheme: str, host: str, port: object, username: str = "") -> tuple[str, str, int, str]:
kind = (scheme or "").strip().lower()
if kind not in SCHEMES:
raise ProxyError("代理只支持 HTTP、HTTPS、SOCKS4、SOCKS5")
address = (host or "").strip()
if not address or "://" in address or any(ch.isspace() for ch in address):
raise ProxyError("请填写代理地址")
if len(address) > 255:
raise ProxyError("代理地址过长")
if not isinstance(port, int) or isinstance(port, bool) or not 1 <= port <= 65535:
raise ProxyError("代理端口须为 1 到 65535 的整数")
user = (username or "").strip()
if len(user) > 128:
raise ProxyError("代理账号过长")
return kind, address, port, user
def enabled(account) -> bool:
return bool(account.proxy_scheme and account.proxy_host and account.proxy_port)
def view_of(account) -> dict:
return {
"enabled": enabled(account),
"scheme": account.proxy_scheme or "http",
"host": account.proxy_host or "",
"port": int(account.proxy_port) if account.proxy_port else None,
"username": account.proxy_username or "",
"has_password": bool(account.proxy_password_cipher),
}
def summary_of(account) -> str:
if not enabled(account):
return ""
return f"{account.proxy_scheme.upper()} {account.proxy_host}:{account.proxy_port}"
def url_of(account) -> str | None:
if not enabled(account):
return None
password = ""
if account.proxy_password_cipher:
from euniv.crypto import decrypt_secret
password = decrypt_secret(account.proxy_password_cipher)
return build_proxy_url(
account.proxy_scheme,
account.proxy_host,
int(account.proxy_port),
account.proxy_username or "",
password,
)
def apply(account, scheme: str, host: str, port: object, username: str = "", password: str | None = None) -> None:
kind, address, number, user = validate_parts(scheme, host, port, username)
if password is not None and len(str(password)) > 256:
raise ProxyError("代理密码过长")
account.proxy_scheme = kind
account.proxy_host = address
account.proxy_port = number
account.proxy_username = user
if password is None:
return
if password == "":
account.proxy_password_cipher = ""
return
from euniv.crypto import encrypt_secret
account.proxy_password_cipher = encrypt_secret(str(password))
def clear(account) -> None:
account.proxy_scheme = ""
account.proxy_host = ""
account.proxy_port = None
account.proxy_username = ""
account.proxy_password_cipher = ""