feat(dl): add --proxy-download for a separate download proxy

Manifest, licence and auth stay on --proxy; track, subtitle and attachment downloads use the second proxy. Also exposed as proxy_download on the REST download job with the same server_proxy gate. The download session now ignores HTTP(S)_PROXY and copies rnet cookies set by name.
This commit is contained in:
imSp4rky
2026-09-13 14:41:01 -06:00
parent e40421a855
commit a0515f8d57
12 changed files with 156 additions and 15 deletions
+6 -3
View File
@@ -37,9 +37,10 @@ def redact_parameters(parameters: Dict[str, Any]) -> Dict[str, Any]:
for key in SENSITIVE_PARAM_KEYS:
if redacted.get(key):
redacted[key] = REDACTED
proxy = redacted.get("proxy")
if isinstance(proxy, str) and "@" in proxy:
redacted["proxy"] = URL_USERINFO_RE.sub(f"{REDACTED}@", proxy)
for key in ("proxy", "proxy_download"):
proxy = redacted.get(key)
if isinstance(proxy, str) and "@" in proxy:
redacted[key] = URL_USERINFO_RE.sub(f"{REDACTED}@", proxy)
return redacted
@@ -455,6 +456,7 @@ def perform_download(
"proxy": params.get("proxy"),
"no_proxy": params.get("no_proxy", False),
"no_proxy_download": params.get("no_proxy_download", False),
"proxy_download": params.get("proxy_download"),
"profile": params.get("profile"),
"cdm_name": params.get("cdm"),
"repack": params.get("repack", False),
@@ -608,6 +610,7 @@ def perform_download(
cdm_only=params.get("cdm_only"),
no_proxy=params.get("no_proxy", False),
no_proxy_download=params.get("no_proxy_download", False),
proxy_download=params.get("proxy_download"),
no_folder=params.get("no_folder", False),
no_source=params.get("no_source", False),
no_mux=params.get("no_mux", False),
+11
View File
@@ -82,6 +82,7 @@ DEFAULT_DOWNLOAD_PARAMS = {
"proxy": None,
"no_proxy": False,
"no_proxy_download": False,
"proxy_download": None,
"no_folder": False,
"no_source": False,
"no_mux": False,
@@ -1696,8 +1697,18 @@ def enforce_download_gates(params: Dict[str, Any], request: Optional[web.Request
"Download jobs license with the server CDM, which is not enabled for this key on this service.",
)
if params.get("proxy_download") is not None and not isinstance(params["proxy_download"], str):
raise APIError(APIErrorCode.INVALID_INPUT, "proxy_download must be a string.")
if not server_proxy_allowed(request):
resolve_handler_proxy(params, params.get("service") or "", request)
if params.get("proxy_download"):
# the download proxy is gated like the main one: a full URI or nothing, never a server provider
resolve_handler_proxy(
{**params, "proxy": params["proxy_download"], "client_region": None},
params.get("service") or "",
request,
)
requested_cdm = params.get("cdm")
if requested_cdm:
+3
View File
@@ -810,6 +810,9 @@ async def download(request: web.Request) -> web.Response:
no_proxy_download:
type: boolean
description: Bypass proxy for all downloads. Manifest, license, and auth still use proxy (default - false)
proxy_download:
type: string
description: Proxy for the downloads only, in the same form as proxy. Manifest, license, and auth use proxy (default - None)
tag:
type: string
description: Set the group tag (default - None)
+6 -2
View File
@@ -102,6 +102,7 @@ class Attachment:
session: Optional[AnySession] = None,
*,
no_proxy_download: bool = False,
proxy_download: Optional[str] = None,
) -> None:
"""Download a URL-backed attachment to the temp directory."""
if self.path is not None or not self.url or DOWNLOAD_LICENCE_ONLY.is_set():
@@ -110,8 +111,11 @@ class Attachment:
from unshackle.core.tracks.track import direct_session
session = session or self.session or requests.Session()
if no_proxy_download and any(session.proxies.values()):
session = direct_session(session)
if no_proxy_download:
if any(session.proxies.values()):
session = direct_session(session)
elif proxy_download:
session = direct_session(session, proxy_download)
download_path = config.directories.temp / (self.file_name or "attachment")
try:
+2
View File
@@ -247,6 +247,7 @@ class Subtitle(Track):
*,
cdm: Optional[object] = None,
no_proxy_download: bool = False,
proxy_download: Optional[str] = None,
adaptive_workers: bool = False,
download_processes: int = 1,
):
@@ -257,6 +258,7 @@ class Subtitle(Track):
progress,
cdm=cdm,
no_proxy_download=no_proxy_download,
proxy_download=proxy_download,
adaptive_workers=adaptive_workers,
download_processes=download_processes,
)
+25 -5
View File
@@ -39,9 +39,13 @@ DRM_PREFERENCE_TYPES: dict[str, Union[type[Widevine], type[PlayReady]]] = {
}
def direct_session(session: Union[Session, "RnetSession"]) -> Session:
"""requests.Session with copied headers/cookies and no proxy."""
def direct_session(session: Union[Session, "RnetSession"], proxy: Optional[str] = None) -> Session:
"""requests.Session with copied headers/cookies and no proxy, or only ``proxy`` when given."""
new = Session()
if proxy:
# requests lets HTTP(S)_PROXY override session proxies, so the env must be ignored for a chosen proxy
new.trust_env = False
new.proxies = {"http": proxy, "https": proxy}
headers = getattr(session, "headers", None)
if headers is not None:
try:
@@ -55,6 +59,17 @@ def direct_session(session: Union[Session, "RnetSession"]) -> Session:
new.cookies.update(jar if jar is not None else cookies)
except Exception:
pass
# RnetCookieAdapter.jar holds only cookies that arrived as a CookieJar; the ones set by name or
# received from a server live in its domain map, so copy those across too
by_domain = getattr(cookies, "get_dict_by_domain", None)
if by_domain is not None:
try:
for domain, named in by_domain().items():
for name, value in named.items():
if name not in new.cookies:
new.cookies.set(name, value, **({"domain": domain} if domain else {}))
except Exception:
pass
new.mount(
"https://",
HTTPAdapter(
@@ -563,6 +578,7 @@ class Track:
*,
cdm: Optional[object] = None,
no_proxy_download: bool = False,
proxy_download: Optional[str] = None,
adaptive_workers: bool = False,
download_processes: int = 1,
):
@@ -585,9 +601,13 @@ class Track:
proxy = next(iter(session.proxies.values()), None)
dl_session = session
if no_proxy_download and proxy:
dl_session = direct_session(session)
proxy = None
if no_proxy_download:
if proxy:
dl_session = direct_session(session)
proxy = None
elif proxy_download:
dl_session = direct_session(session, proxy_download)
proxy = proxy_download
track_type = self.__class__.__name__
save_path = config.directories.temp / f"{track_type}_{self.id}.mp4"