fix: 代理分配看延迟,采集失败不再丢掉其余类型

This commit is contained in:
2026-11-16 12:02:10 +08:00
parent 8504ff5e85
commit 05f2396e35
3 changed files with 252 additions and 83 deletions
+58 -15
View File
@@ -26,15 +26,42 @@ def canonical_scheme(scheme: str | None) -> str:
return kind
def _unwrap_host(host: str) -> str:
text = host.strip()
if len(text) >= 2 and text.startswith("[") and text.endswith("]"):
return text[1:-1]
return text
def _host_is_public(ip: ipaddress.IPv4Address | ipaddress.IPv6Address) -> bool:
return not (
ip.is_private
or ip.is_loopback
or ip.is_link_local
or ip.is_reserved
or ip.is_multicast
or ip.is_unspecified
)
def _reject_private(addresses: list[ipaddress.IPv4Address | ipaddress.IPv6Address]) -> None:
for ip in addresses:
if not _host_is_public(ip):
raise ProxyError("代理只允许公网地址")
def assert_public_host(host: str) -> None:
"""手填和采集都只接受公网地址。"""
address = (host or "").strip()
if not address or any(char in address for char in "@/#?"):
raise ProxyError("代理只允许公网地址")
bare = _unwrap_host(address)
if bare != address and (not bare or any(char in bare for char in "[]/@#?")):
raise ProxyError("代理地址不合法")
try:
addresses = [ipaddress.ip_address(address)]
addresses = [ipaddress.ip_address(bare)]
except ValueError:
if not re.fullmatch(r"[A-Za-z0-9.-]+", address) or ".." in address or address.startswith("-"):
if bare != address or not re.fullmatch(r"[A-Za-z0-9.-]+", address) or ".." in address or address.startswith("-"):
raise ProxyError("代理地址不合法")
try:
found = socket.getaddrinfo(address, None)
@@ -48,24 +75,35 @@ def assert_public_host(host: str) -> None:
continue
if not addresses:
raise ProxyError("代理地址无法解析")
for ip in addresses:
if (
ip.is_private
or ip.is_loopback
or ip.is_link_local
or ip.is_reserved
or ip.is_multicast
or ip.is_unspecified
):
raise ProxyError("代理只允许公网地址")
_reject_private(addresses)
def _host_for_url(host: str) -> str:
bare = _unwrap_host(host)
try:
ipaddress.IPv6Address(host)
ipaddress.IPv6Address(bare)
except ValueError:
return host
return f"[{host}]"
return host.strip() or host
return f"[{bare}]"
def _written_host(host: str) -> str:
"""主机名只留字母、数字、点和横线;IPv6 写入时加方括号。"""
address = host.strip()
if not address or any(char in address for char in "@/#?"):
raise ProxyError("代理地址不合法")
bare = _unwrap_host(address)
if bare != address and (not bare or any(char in bare for char in "[]")):
raise ProxyError("代理地址不合法")
try:
parsed = ipaddress.ip_address(bare)
except ValueError:
if bare != address or not re.fullmatch(r"[A-Za-z0-9.-]+", address) or ".." in address or address.startswith("-"):
raise ProxyError("代理地址不合法")
return address
if isinstance(parsed, ipaddress.IPv6Address):
return f"[{bare}]"
return bare
def parse_proxy(value: str | None) -> str | None:
@@ -79,6 +117,8 @@ def parse_proxy(value: str | None) -> str | None:
scheme = canonical_scheme(parsed.scheme)
if scheme not in SCHEMES:
raise ProxyError("代理只支持 HTTP、HTTPS、SOCKS4、SOCKS5")
if parsed.path not in ("", "/") or parsed.query or parsed.fragment:
raise ProxyError("代理地址不合法")
if not parsed.hostname:
raise ProxyError("代理地址不完整")
host = _host_for_url(parsed.hostname)
@@ -115,6 +155,9 @@ def validate_parts(scheme: str, host: str, port: object, username: str = "") ->
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("代理地址过长")
address = _written_host(address)
if len(address) > 255:
raise ProxyError("代理地址过长")
if not isinstance(port, int) or isinstance(port, bool) or not 1 <= port <= 65535: