SurfSense/surfsense_backend/app/utils/proxy/registry.py
DESKTOP-RTLN3BA\$punk 701f888b9e feat(proxy): refactor proxy configuration and add DataImpulse provider
- Updated proxy configuration in `.env.example` files to use `PROXY_URL` and `PROXY_URLS` instead of `CUSTOM_PROXY_URL` and `CUSTOM_PROXY_URLS`.
- Introduced `DataImpulseProvider` for proxy management, replacing the deprecated `AnonymousProxiesProvider`.
- Enhanced documentation to reflect changes in proxy setup and usage.
- Adjusted related code in the proxy registry and configuration files to support the new provider structure.
2026-07-04 17:18:40 -07:00

48 lines
1.6 KiB
Python

"""Proxy provider registry.
Maps the ``PROXY_PROVIDER`` config value to a :class:`ProxyProvider`
implementation. To add a new vendor: implement a provider in ``providers/`` and
add a single entry to ``_PROVIDERS`` below - no caller changes required.
"""
import logging
from app.config import Config
from app.utils.proxy.base import ProxyProvider
from app.utils.proxy.providers.custom import CustomProxyProvider
from app.utils.proxy.providers.dataimpulse import DataImpulseProvider
logger = logging.getLogger(__name__)
# Registered proxy providers, keyed by their ``name``.
_PROVIDERS: dict[str, type[ProxyProvider]] = {
CustomProxyProvider.name: CustomProxyProvider,
DataImpulseProvider.name: DataImpulseProvider,
}
# BYO ``custom`` is the neutral default: it needs no vendor and returns no proxy
# until PROXY_URL(S) is set, so an unconfigured install simply runs direct.
_DEFAULT_PROVIDER = CustomProxyProvider.name
_active_provider: ProxyProvider | None = None
def get_active_provider() -> ProxyProvider:
"""Return the configured proxy provider instance (cached for the process)."""
global _active_provider
if _active_provider is not None:
return _active_provider
key = (Config.PROXY_PROVIDER or _DEFAULT_PROVIDER).strip()
provider_cls = _PROVIDERS.get(key)
if provider_cls is None:
logger.warning(
"Unknown PROXY_PROVIDER '%s'; falling back to '%s'. Available: %s",
key,
_DEFAULT_PROVIDER,
", ".join(sorted(_PROVIDERS)),
)
provider_cls = _PROVIDERS[_DEFAULT_PROVIDER]
_active_provider = provider_cls()
return _active_provider