feat(remote): license with the client's device when the user picked one

A remote client that passes --cdm, has a cdm entry for the service that named the loaded device, or sets server_cdm: false now declines the server CDM at session create, and warns when the server keeps it. A service option listed in SERVER_DEVICE_OPTIONS keeps the server CDM.
This commit is contained in:
imSp4rky
2026-10-01 13:21:53 -06:00
parent af17bad010
commit 9a1abd23b6
14 changed files with 292 additions and 20 deletions
+9
View File
@@ -682,10 +682,18 @@ def choose_session_cdm(
"""``(server_cdm, max_height)`` for a new remote session.
The service picks its manifest from the CDM it sees, so it decides once, before any tracks.
A client that sends ``server_cdm: false`` with a device licenses with that device, unless it also set
one of the service's ``SERVER_DEVICE_OPTIONS``.
When ``-q`` asks for more than the cap, the client's own device licenses, and a client with no device gets a refusal.
"""
if not server_cdm_allowed(request, service):
return False, None
if data.get("server_cdm") is False and data.get("cdm_type"):
server_device_options = getattr(Services.load(service), "SERVER_DEVICE_OPTIONS", ())
service_params = data.get("service_params")
options = {**data, **service_params} if isinstance(service_params, dict) else data
if not any(options.get(option) for option in server_device_options):
return False, None
cap = server_cdm_max_height(request, service)
wanted = requested_height(data.get("quality"))
if cap is None or wanted is None or wanted <= cap:
@@ -3172,6 +3180,7 @@ SESSION_TRANSPORT_KEYS = {
"cache",
"client_region",
"proxy_region",
"server_cdm",
"cdm_type",
"cdm_security_level",
"cdm_relay",
+7
View File
@@ -1811,6 +1811,13 @@ async def session_create(request: web.Request) -> web.Response:
forced_subs:
type: boolean
default: false
server_cdm:
type: boolean
description: |
False when the user picked the local CDM. With `cdm_type`, the server then builds
the service on the client's device, also for an API key with the server CDM.
A request that sets one of the service's `SERVER_DEVICE_OPTIONS`, in
`service_params` or at the top level, keeps the server CDM
cdm_type:
type: string
enum: [widevine, playready]
+11
View File
@@ -70,6 +70,17 @@ def resolve_cdm_name(cdm: dict, service: str, override: Any = None) -> Any:
return override or ci_get(cdm, service) or ci_get(cdm, "default")
def cdm_entry_names_device(entry: Any, profile: Optional[str] = None) -> bool:
"""Whether a per-service ``cdm`` entry names a device when no quality is known.
An entry that selects only by quality, or by a different profile, names none.
"""
if not isinstance(entry, dict):
return bool(entry)
systems = {"widevine", "playready"} & {str(key).lower() for key in entry}
return bool(systems or entry.get(profile) or entry.get("default"))
class Config:
class _Directories:
# default directories, do not modify here, set via config
+23 -3
View File
@@ -45,6 +45,7 @@ from unshackle.core.titles.music import Album, Song
from unshackle.core.tracks import Audio, Chapter, Chapters, Subtitle, Tracks, Video
from unshackle.core.tracks.attachment import Attachment
from unshackle.core.tracks.track import Track
from unshackle.core.utils.collections import ci_get
from unshackle.core.utils.redact import redact_path, redact_text, safe_display_url
from unshackle.core.vault import Vault
@@ -1064,9 +1065,20 @@ class RemoteService:
# sent with the server CDM too: a capped server hands the tracks above its cap to this device
cdm = self.local_cdm
cdm_name = self.ctx.parent and self.ctx.parent.params.get("cdm_name")
cdm_entry = cdm is not None and self.ctx.obj.cdm_entry and self._server_cdm is not False
if not (cdm_entry or cdm_name or self._server_cdm is False) and ci_get(config.cdm, self.service_tag):
self.log.warning(
f"The cdm entry for {self.service_tag} did not load a device for this remote session, "
"so the server decides who licenses it"
)
if cdm is not None:
from unshackle.core.cdm.detect import is_playready_cdm
if cdm_entry or cdm_name:
self._server_cdm = False
if self._server_cdm is False:
create_data["server_cdm"] = False
create_data["cdm_type"] = "playready" if is_playready_cdm(cdm) else "widevine"
level = getattr(cdm, "security_level", None)
if isinstance(level, int):
@@ -1103,6 +1115,11 @@ class RemoteService:
if "server_cdm" in result:
self.adopt_session_cdm(bool(result["server_cdm"]), result.get("server_cdm_max_height"))
self._server_device = bool(result.get("server_device"))
if cdm_entry and self._server_cdm is False and not self._server_device:
self.log.info(
f"The cdm config has an entry for {self.service_tag}, so your own device licenses "
"this remote session and not the server CDM"
)
vault = bool(result.get("server_vault"))
if self._server_device:
self._server_cdm = True
@@ -1134,11 +1151,16 @@ class RemoteService:
"""Take the server's choice of who licenses this remote session, and say why when a cap made it.
A remote session on the server's lent device keeps the server-CDM flow: dl checks the local vaults, then asks the
server, which answers only from its vault.
server, which answers only from its vault. ``_server_cdm`` is False on entry when the user picked the local
device, so a server that keeps its own CDM gets a warning.
"""
if getattr(self, "_server_device", False):
self._server_cdm = True
return
if self._server_cdm is False and server_cdm:
self._server_cdm = True
self.log.warning("The server CDM licenses this remote session, so your own device is not used")
return
if self._server_cdm and not server_cdm and max_height is None:
self.log.warning(
f"{self.service_tag} is not available for server CDM licensing with this API key, "
@@ -1146,8 +1168,6 @@ class RemoteService:
)
self._server_cdm = server_cdm
if max_height is None:
if server_cdm and self.ctx.parent and self.ctx.parent.params.get("cdm_name"):
self.log.warning("--cdm is ignored: the server CDM licenses this remote session")
return
message = f"The server CDM licenses up to {max_height}p for this API key"
if not server_cdm:
+3
View File
@@ -162,6 +162,9 @@ class Service(metaclass=ABCMeta):
# Config keys a remote client may supply for a session its own device licenses (a device identity
# such as an ESN); the server's own values for these keys are withheld from that session.
CLIENT_CONFIG: tuple[str, ...] = ()
# Every service option that makes the service call ``cdm.lend_server_device()``. A remote session with
# one of them set keeps the server CDM even when the remote client picked its own device.
SERVER_DEVICE_OPTIONS: tuple[str, ...] = ()
def __init__(self, ctx: click.Context):
console.print(Padding(Rule(f"[rule.text]Service: {self.__class__.__name__}"), (1, 2)))
+10 -1
View File
@@ -156,11 +156,20 @@ class SubtitleCodecChoice(click.Choice):
class ContextData:
def __init__(self, config: dict, cdm: WidevineCdm, proxy_providers: list, profile: Optional[str] = None):
def __init__(
self,
config: dict,
cdm: WidevineCdm,
proxy_providers: list,
profile: Optional[str] = None,
cdm_entry: bool = False,
):
self.config = config
self.cdm = cdm
self.proxy_providers = proxy_providers
self.profile = profile
# True when the service's own entry in the cdm config named the loaded device
self.cdm_entry = cdm_entry
class SeasonRange(click.ParamType):