fix(remote): load the server's first-listed cdm for a server-device option

This commit is contained in:
imSp4rky
2026-11-26 11:20:22 -07:00
parent cca547eba6
commit 251d1e779a
9 changed files with 109 additions and 12 deletions
+3 -1
View File
@@ -282,7 +282,9 @@ There are two ways DRM keys get resolved, chosen by the client's `server_cdm` fl
`server_cdm: false` when it creates the remote session, and the server builds the
service on your device even when the API key has the server CDM.
- A service option that the service lists in `SERVER_DEVICE_OPTIONS` keeps the server
CDM. The server reads the option from `service_params`, or from the top level of
CDM, and the service gets the server's device whatever device you have. From a server
`cdm` entry with a device for both DRM systems, it gets the device that the entry
lists first. The server reads the option from `service_params`, or from the top level of
a request that has no `service_params`. The create response gives the result in
`server_cdm`, and the client shows a warning when the server did not apply its choice.
- The service on the server sees your device, not the server's: its DRM system,
+2 -1
View File
@@ -57,7 +57,8 @@ does not set the order.
On a server, an entry that holds `widevine` and `playready` gives a remote session the server CDM
of the DRM system of the client's local CDM. The service then makes its manifest for that system,
so the client's vault keys and its own device apply to the tracks. A client with no local CDM gets
the device that the entry lists first.
the device that the entry lists first. A service option that runs the remote session on the server's
device, such as a server-identity flag, always gets the device that the entry lists first.
A quality key can also hold a dict of `widevine` / `playready` devices, and so can the
top-level `default` entry:
@@ -349,6 +349,81 @@ def test_a_lent_device_gives_the_service_the_server_config_back(monkeypatch: pyt
assert service_config == {"esn": "SERVER", "esn_map": {7000: "s"}, "other": 1}
@click.command()
@click.option("--server-side", is_flag=True, default=False)
def server_side_cli(server_side: bool) -> None: ...
class ServerSideService:
SERVER_DEVICE_OPTIONS = ("server_side",)
cli = server_side_cli
@pytest.mark.parametrize(
("entry", "client_type", "server_side", "client_device", "loaded"),
[
pytest.param({"playready": "pr", "widevine": "wv"}, "widevine", True, False, "pr", id="server-cdm-session"),
pytest.param({"widevine": "wv", "playready": "pr"}, "playready", True, False, "wv", id="first-listed-wins"),
pytest.param({"playready": "pr", "widevine": "wv"}, "widevine", False, False, "wv", id="follows-the-client"),
pytest.param({"playready": "pr", "widevine": "wv"}, "widevine", True, True, "pr", id="lent-device"),
],
)
def test_a_server_device_option_takes_the_first_listed_device_of_a_dual_entry(
monkeypatch: pytest.MonkeyPatch, entry: dict, client_type: str, server_side: bool, client_device: bool, loaded: str
) -> None:
built: dict = {}
devices: list = []
monkeypatch.setattr(handlers.config, "cdm", {"EXAMPLE1": entry}, raising=False)
monkeypatch.setattr(handlers, "device_system", lambda name, cfg: {"wv": "widevine", "pr": "playready"}[name])
monkeypatch.setattr("unshackle.core.cdm.load_cdm", lambda name, **k: devices.append(name))
monkeypatch.setattr(handlers, "served_context", lambda obj, *a: built.update(cdm=obj.cdm))
monkeypatch.setattr(handlers.Services, "load", lambda tag: ServerSideService)
monkeypatch.setattr(handlers, "instantiate_service", lambda *a, **k: "service")
data = {"cdm_type": client_type, "cdm_relay": True, "service_params": {"server_side": server_side}}
handlers.create_service_instance(
"EXAMPLE1",
"t",
data,
None,
[],
None,
server_cdm=not client_device,
client_device=client_device,
input_bridge=InputBridge(),
)
if client_device:
built["cdm"].lend_server_device()
assert devices == [loaded]
@pytest.mark.parametrize(
("value", "requested"), [(True, True), ("false", False), ("0", False), ("yes", True), (False, False)]
)
def test_a_server_device_option_counts_as_the_service_casts_it(value: Any, requested: bool) -> None:
assert handlers.server_device_requested(ServerSideService, {"service_params": {"server_side": value}}) is requested
@pytest.mark.parametrize(("server_side", "cdm_type"), [(True, None), (False, "widevine")])
def test_a_track_listing_with_a_server_device_option_ignores_the_client_system(
monkeypatch: pytest.MonkeyPatch, server_side: bool, cdm_type: str | None
) -> None:
loaded: list = []
service = SimpleNamespace(authenticate=lambda cookies, credential: None)
monkeypatch.setattr(key_policy, "load_service_config", lambda tag: {})
monkeypatch.setattr(handlers, "resolve_handler_proxy", lambda *a: (None, []))
monkeypatch.setattr(handlers, "resolve_server_account", lambda *a: None)
monkeypatch.setattr(handlers, "server_login_material", lambda *a: (None, None))
monkeypatch.setattr(handlers, "load_full_cdm", lambda service, profile, cdm_type, cfg: loaded.append(cdm_type))
monkeypatch.setattr(handlers, "served_context", lambda *a: None)
monkeypatch.setattr(handlers.Services, "load", lambda tag: ServerSideService)
monkeypatch.setattr(handlers, "instantiate_service", lambda *a, **k: service)
data = {"cdm_type": "widevine", "service_params": {"server_side": server_side}}
handlers.setup_list_service(data, "EXAMPLE1", None, "t")
assert loaded == [cdm_type]
def test_a_client_map_entry_replaces_the_server_entry_of_the_same_number() -> None:
merged, _ = handlers.client_config_overlay(
{"esn_map": {4464: "server-template", 7000: "other"}}, ("esn_map",), {"esn_map": {"4464": "client-template"}}
@@ -224,7 +224,7 @@ def test_a_capped_track_without_a_local_device_fails_clearly():
def test_a_capped_track_without_the_local_drm_system_fails_clearly():
fails_at_the_track_licence(
PR_DEVICE,
r"1080p.*the 2160p track has only Widevine DRM for your PlayReady device; pass -q 2160 ",
r"1080p.*the 2160p track has only Widevine DRM, which your local PlayReady CDM cannot license; pass -q 2160 ",
)
@@ -132,7 +132,7 @@ def test_a_capped_track_takes_a_local_vault_key_before_the_refusal():
def test_a_capped_track_no_vault_holds_raises_the_refusal():
import click
with pytest.raises(click.ClickException, match="only Widevine DRM for your PlayReady device"):
with pytest.raises(click.ClickException, match="only Widevine DRM, which your local PlayReady CDM cannot license"):
capped_run(FakeVaults())
+20 -5
View File
@@ -718,7 +718,9 @@ def setup_list_service(
if account:
profile = None if account == "default" else account
cdm = load_full_cdm(normalized_service, profile, data.get("cdm_type"), policy.cdm_config())
service_module = Services.load(normalized_service)
cdm_type = None if server_device_requested(service_module, data) else data.get("cdm_type")
cdm = load_full_cdm(normalized_service, profile, cdm_type, policy.cdm_config())
cookies, credential = server_login_material(data, normalized_service, account, profile)
parent_ctx = served_context(
@@ -726,7 +728,6 @@ def setup_list_service(
{"proxy": proxy_param, "no_proxy": no_proxy, **forwarded_dl_params(data)},
{"cookies_supplied": cookies is not None},
)
service_module = Services.load(normalized_service)
service_instance = instantiate_service(parent_ctx, service_module, title_id, data, LIST_HANDLER_TRANSPORT_KEYS)
if account:
@@ -891,6 +892,16 @@ def requested_height(quality: Any) -> int | None:
return max(heights) if heights else None
def server_device_requested(service_module: Any, data: dict[str, Any]) -> bool:
"""True when the request sets one of the service's ``SERVER_DEVICE_OPTIONS``, cast as the service's own options cast it."""
options = service_options(data, getattr(service_module, "SERVER_DEVICE_OPTIONS", ()))
params = getattr(getattr(service_module, "cli", None), "params", ())
try:
return any(cast_service_options(params, None, options).values())
except click.BadParameter:
return False
def choose_session_cdm(policy: KeyPolicy, service: str, data: dict[str, Any]) -> tuple[bool, int | None]:
"""``(server_cdm, max_height)`` for a new remote session.
@@ -902,8 +913,7 @@ def choose_session_cdm(policy: KeyPolicy, service: str, data: dict[str, Any]) ->
if not policy.allows("server_cdm", 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", ())
if not any(service_options(data, server_device_options).values()):
if not server_device_requested(Services.load(service), data):
return False, None
cap = policy.max_height(service)
wanted = requested_height(data.get("quality"))
@@ -2729,6 +2739,9 @@ def create_service_instance(
With ``client_device`` the service sees a stand-in for the client's device, which relays CDM calls
through ``input_bridge`` when the client can answer them, and a config holding the client's own identity.
A service option in ``SERVER_DEVICE_OPTIONS`` gets the server's device whatever device the client has: from an
entry with a device for both systems, the one the entry lists first.
With ``server_account`` the service takes the server's own credential and cookie file for
``profile`` and drops anything the client sent. Otherwise only client-sent data counts: a
client that sends nothing authenticates with nothing, because the server never lends its
@@ -2758,7 +2771,9 @@ def create_service_instance(
cdm = client_device_cdm(data, input_bridge, lend_server_device)
else:
follows_client = not server_cdm or choose_cdm(key_config.cdm, normalized_service).both
follows_client = not server_cdm or (
choose_cdm(key_config.cdm, normalized_service).both and not server_device_requested(service_module, data)
)
cdm = load_full_cdm(normalized_service, profile, data.get("cdm_type") if follows_client else None, key_config)
# Reconstruct enum track-selection params from client data so service code that reads
+2 -1
View File
@@ -2048,7 +2048,8 @@ async def session_create(request: web.Request) -> web.Response:
description: |
DRM system of the client's local CDM; absent when the client has none.
A server CDM session loads the device of this system when the server's
cdm entry for the service names both systems
cdm entry for the service names both systems, unless the request sets one
of the service's SERVER_DEVICE_OPTIONS: then the entry's first-listed device
cdm_security_level:
type: integer
description: |
+3 -1
View File
@@ -1641,7 +1641,9 @@ class RemoteService(TitleSource):
label = f"the {height}p track" if height else str(track)
hint = f"; pass -q {height} to start the session on your device" if cap and height else ""
have = " and ".join(sorted(systems))
raise click.ClickException(f"{why}, and {label} has only {have} DRM for your {drm_class} device{hint}")
raise click.ClickException(
f"{why}, and {label} has only {have} DRM, which your local {drm_class} CDM cannot license{hint}"
)
drm_class = other
track.drm_preference = drm_class.lower()
self.client_licensed.add(str(track.id))
+2 -1
View File
@@ -167,7 +167,8 @@ class Service(TitleSource):
# 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.
# one of them set keeps the server CDM even when the remote client picked its own device, and the server
# loads the first-listed device of its cdm entry, whatever system the client's device is.
SERVER_DEVICE_OPTIONS: tuple[str, ...] = ()
# Cache key prefixes that hold state built from the device identity (a device ID, device session keys), never
# login state. A remote session on the server's device keeps them on the server, per API key.