Files
wehub-resource-sync 6ede33ccdb
Build and Push Docker Images / create_manifest (web, surfsense-web, , cpu) (push) Has been cancelled
Build and Push Docker Images / finalize_release (push) Has been cancelled
Obsidian Plugin Lint / lint (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-24.04-arm, linux/arm64, arm64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_web, cpu, ./surfsense_web/Dockerfile, web, surfsense-web, ubuntu-latest, linux/amd64, amd64, , runner, false, cpu) (push) Has been cancelled
Build and Push Docker Images / compute_version (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cpu, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, , production, false, cpu) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu126, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda126, production, true, cuda126) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-24.04-arm, linux/arm64, arm64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / build (./surfsense_backend, cu128, ./surfsense_backend/Dockerfile, backend, surfsense-backend, ubuntu-latest, linux/amd64, amd64, -cuda, production, true, cuda) (push) Has been cancelled
Build and Push Docker Images / verify_digests (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, , cpu) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda, cuda) (push) Has been cancelled
Build and Push Docker Images / create_manifest (backend, surfsense-backend, -cuda126, cuda126) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:33:44 +08:00

62 lines
2.1 KiB
Python

"""Bring-your-own (BYO) custom proxy provider.
Reads one or more proxy endpoints from the shared env (``PROXY_URL`` and/or the
comma-separated ``PROXY_URLS``). With a single endpoint it behaves like a static
proxy; with a pool (>1) it rotates client-side via Scrapling's thread-safe
``ProxyRotator`` (cyclic), transparently to every caller of the zero-arg getters.
No vendor-specific auth assumptions: a user who wants a specific vendor points
``PROXY_URLS`` at that vendor's ``http://user:pass@host:port`` endpoints.
"""
import logging
from scrapling.engines.toolbelt import ProxyRotator
from app.config import Config
from app.utils.proxy.base import ProxyProvider
logger = logging.getLogger(__name__)
class CustomProxyProvider(ProxyProvider):
"""BYO provider for a single endpoint or a rotating pool of endpoints."""
name = "custom"
def __init__(self) -> None:
self._urls = self._load_urls()
# Only build a rotator for an actual pool; a single endpoint stays static.
self._rotator = ProxyRotator(self._urls) if len(self._urls) > 1 else None
if not self._urls:
logger.warning(
"PROXY_PROVIDER='custom' selected but neither PROXY_URL nor "
"PROXY_URLS is set; crawls will run without a proxy."
)
@staticmethod
def _load_urls() -> list[str]:
"""Collect proxy URLs from env (pool first, then single), de-duplicated."""
urls: list[str] = []
pool = Config.PROXY_URLS
if pool:
urls.extend(part.strip() for part in pool.split(",") if part.strip())
single = (Config.PROXY_URL or "").strip()
if single and single not in urls:
urls.append(single)
return urls
@property
def is_pool_backed(self) -> bool:
return self._rotator is not None
def get_proxy_url(self) -> str | None:
if not self._urls:
return None
if self._rotator is not None:
# Advances the cyclic index on every call (thread-safe).
return self._rotator.get_proxy()
return self._urls[0]