解析代理URL(支持 socks5h://,Playwright 中按 socks5 处理)
(proxy_url: str)
| 243 | # 代理解析工具函数 |
| 244 | # ========================================== |
| 245 | def parse_proxy_url(proxy_url: str) -> Optional[Dict[str, str]]: |
| 246 | """解析代理URL(支持 socks5h://,Playwright 中按 socks5 处理)""" |
| 247 | if not proxy_url: return None |
| 248 | if not re.match(r'^(http|https|socks5h?|socks5)://', proxy_url): proxy_url = f"http://{proxy_url}" |
| 249 | match = re.match(r'^(socks5h?|socks5|http|https)://(?:([^:]+):([^@]+)@)?([^:]+):(\d+)$', proxy_url) |
| 250 | if match: |
| 251 | protocol, username, password, host, port = match.groups() |
| 252 | browser_protocol = "socks5" if protocol.startswith("socks5") else protocol |
| 253 | proxy_config = {'server': f'{browser_protocol}://{host}:{port}'} |
| 254 | if username and password: |
| 255 | proxy_config['username'] = username |
| 256 | proxy_config['password'] = password |
| 257 | return proxy_config |
| 258 | return None |
| 259 | |
| 260 | def normalize_browser_proxy_url(proxy_url: str) -> tuple[Optional[str], Optional[str]]: |
| 261 | """将浏览器代理标准化为 Playwright/Chromium 可接受的格式。 |
no outgoing calls
no test coverage detected