proxywhirl.utils¶
Utility functions for logging, validation, and encryption.
Classes¶
Context manager for CLI concurrency locking with Typer-aware error handling. |
Functions¶
|
Write content atomically using temp file + rename. |
|
Write JSON data atomically. |
|
Configure loguru logging with optional JSON formatting and credential redaction. |
|
Create a Proxy instance from a URL string. |
|
Decrypt credentials using Fernet symmetric encryption. |
|
Encrypt credentials using Fernet symmetric encryption. |
Generate a new Fernet encryption key. |
|
|
Validate proxy URL format. |
|
Mask credentials in a proxy URL for safe display in verbose/debug output. |
|
Mask a SecretStr or string value for safe display. |
|
Parse proxy URL into components. |
|
Convert a Proxy instance to a dictionary. |
Recursively scrub credentials from a dictionary for safe output. |
|
|
Validate a Proxy model instance. |
|
Validate a target URL to prevent SSRF attacks (API-safe version). |
Module Contents¶
- class proxywhirl.utils.CLILock(config_dir)[source]¶
Context manager for CLI concurrency locking with Typer-aware error handling.
Initialize lock file manager.
- Parameters:
config_dir (pathlib.Path) – Directory where lock file will be created
- proxywhirl.utils.atomic_write(path, content, encoding='utf-8')[source]¶
Write content atomically using temp file + rename.
This prevents partial writes and corruption if the process is interrupted during the write operation.
- Parameters:
path (pathlib.Path) – Target file path
content (str) – Content to write
encoding (str) – File encoding (default: utf-8)
- Raises:
OSError – If write or rename fails
- Return type:
None
- proxywhirl.utils.atomic_write_json(path, data, **json_kwargs)[source]¶
Write JSON data atomically.
- Parameters:
path (pathlib.Path) – Target file path
data (Any) – Data to serialize as JSON
**json_kwargs (Any) – Arguments to pass to json.dumps()
- Return type:
None
- proxywhirl.utils.configure_logging(level='INFO', format_type='json', redact_credentials=True)[source]¶
Configure loguru logging with optional JSON formatting and credential redaction.
- proxywhirl.utils.create_proxy_from_url(url, source=ProxySource.USER, tags=None)[source]¶
Create a Proxy instance from a URL string.
- Parameters:
- Returns:
Proxy instance
- Raises:
ValueError – If URL is invalid
- Return type:
proxywhirl.models.Proxy
- proxywhirl.utils.decrypt_credentials(ciphertext, key)[source]¶
Decrypt credentials using Fernet symmetric encryption.
- Parameters:
- Returns:
Decrypted plaintext
- Raises:
ImportError – If cryptography package not installed
InvalidToken – If key is incorrect or ciphertext is invalid
- Return type:
- proxywhirl.utils.encrypt_credentials(plaintext, key=None)[source]¶
Encrypt credentials using Fernet symmetric encryption.
- Parameters:
- Returns:
Encrypted text (base64-encoded)
- Raises:
ImportError – If cryptography package not installed
- Return type:
- proxywhirl.utils.generate_encryption_key()[source]¶
Generate a new Fernet encryption key.
- Returns:
Base64-encoded encryption key
- Raises:
ImportError – If cryptography package not installed
- Return type:
- proxywhirl.utils.mask_proxy_url(url)[source]¶
Mask credentials in a proxy URL for safe display in verbose/debug output.
Replaces username:password with *:* to prevent credential exposure.
- Parameters:
url (str) – Proxy URL that may contain credentials
- Returns:
URL with credentials masked
- Return type:
Example
>>> mask_proxy_url("http://user:pass@proxy.com:8080") "http://***:***@proxy.com:8080"
- proxywhirl.utils.mask_secret_str(secret)[source]¶
Mask a SecretStr or string value for safe display.
- Parameters:
secret (Any) – SecretStr instance or string to mask
- Returns:
Masked value (always
"***").- Return type:
- proxywhirl.utils.parse_proxy_url(url)[source]¶
Parse proxy URL into components.
- Parameters:
url (str) – Proxy URL to parse
- Returns:
Parsed URL components with protocol, host, port, username, password.
- Return type:
- Raises:
ValueError – If URL is invalid
- proxywhirl.utils.proxy_to_dict(proxy, include_stats=True)[source]¶
Convert a Proxy instance to a dictionary.
- proxywhirl.utils.scrub_credentials_from_dict(data)[source]¶
Recursively scrub credentials from a dictionary for safe output.
Masks: - Proxy URLs with credentials - SecretStr values - Dict keys containing sensitive terms (password, secret, token, etc.)
- proxywhirl.utils.validate_target_url_safe(url, allow_private=False)[source]¶
Validate a target URL to prevent SSRF attacks (API-safe version).
This function validates URLs to prevent Server-Side Request Forgery (SSRF) attacks by blocking access to: - Localhost and loopback addresses (127.0.0.0/8, ::1) - Private IP ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) - Link-local addresses (169.254.0.0/16) - Internal domain names (.local, .internal, .lan, .corp)
- Parameters:
- Raises:
ValueError – If the URL is invalid or potentially dangerous
- Return type:
None
Example
>>> validate_target_url_safe("https://example.com") # OK >>> validate_target_url_safe("http://localhost:8080") # Raises ValueError >>> validate_target_url_safe("http://192.168.1.1") # Raises ValueError >>> validate_target_url_safe("http://192.168.1.1", allow_private=True) # OK