Files
remnawave-bedolaga-telegram…/app/utils/proxy.py
T
Fringg 3c5bf4fa22 feat: add SOCKS proxy support for nalogo (tax service) module
Route all nalog.ru API traffic through SOCKS proxy. Uses NALOGO_PROXY_URL
env var (falls back to PROXY_URL if not set). Adds httpx[socks] dependency.

- Thread proxy_url through Client → AuthProviderImpl + AsyncHTTPClient
- Extract mask_proxy_url() and sanitize_proxy_error() utilities
- Add socks5h:// scheme support for remote DNS resolution
- Sanitize proxy credentials in error messages
- Log masked proxy URL at startup and service init
2026-03-21 04:21:11 +03:00

36 lines
1.1 KiB
Python

"""Proxy URL utilities for safe logging and error handling."""
import re
from urllib.parse import urlparse
def mask_proxy_url(proxy_url: str) -> str:
"""Mask credentials in a proxy URL for safe logging.
Handles edge cases:
- No credentials: returns URL as-is
- Username + password: masks both with ***
- Password-only: masks as well
- No explicit port: omits :port part
"""
parsed = urlparse(proxy_url)
if not parsed.username and not parsed.password:
return proxy_url
host = parsed.hostname or 'unknown'
port_part = f':{parsed.port}' if parsed.port else ''
return f'{parsed.scheme}://***@{host}{port_part}'
_PROXY_CRED_RE = re.compile(r'socks[45h]*://[^@\s]+@', re.IGNORECASE)
def sanitize_proxy_error(error: Exception) -> str:
"""Strip proxy credentials from exception messages.
httpx/socksio may include the full proxy URL (with credentials)
in connection error messages and tracebacks. This function removes
credentials from the error string.
"""
msg = str(error)
return _PROXY_CRED_RE.sub('socks5://***@', msg)