mirror of
https://github.com/zc-zhangchen/any-auto-register.git
synced 2026-05-08 00:04:07 +08:00
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Optional
|
|
from urllib.parse import unquote, urlsplit, urlunsplit
|
|
|
|
|
|
def normalize_proxy_url(proxy_url: Optional[str]) -> Optional[str]:
|
|
"""将 socks5:// 规范化为 socks5h://,避免本地 DNS 泄漏。"""
|
|
if proxy_url is None:
|
|
return None
|
|
|
|
value = str(proxy_url).strip()
|
|
if not value:
|
|
return None
|
|
|
|
parts = urlsplit(value)
|
|
if (parts.scheme or "").lower() == "socks5":
|
|
parts = parts._replace(scheme="socks5h")
|
|
return urlunsplit(parts)
|
|
return value
|
|
|
|
|
|
def build_requests_proxy_config(proxy_url: Optional[str]) -> Optional[dict[str, str]]:
|
|
if not proxy_url:
|
|
return None
|
|
return {"http": proxy_url, "https": proxy_url}
|
|
|
|
|
|
def build_playwright_proxy_config(proxy_url: Optional[str]) -> Optional[dict[str, str]]:
|
|
if not proxy_url:
|
|
return None
|
|
|
|
parts = urlsplit(proxy_url)
|
|
if not parts.scheme or not parts.hostname or parts.port is None:
|
|
return {"server": proxy_url}
|
|
|
|
config = {"server": f"{parts.scheme}://{parts.hostname}:{parts.port}"}
|
|
if parts.username:
|
|
config["username"] = unquote(parts.username)
|
|
if parts.password:
|
|
config["password"] = unquote(parts.password)
|
|
return config
|