mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-25 10:25:09 +00:00
feat(remote): decrypt service-owned cbcs samples on the server
The client downloads the file and sends binary sample frames to a new samples route.
This commit is contained in:
@@ -28,6 +28,7 @@ from unshackle.core.cacher import Cacher
|
||||
from unshackle.core.cdm.detect import cdm_type_stub
|
||||
from unshackle.core.config import config
|
||||
from unshackle.core.constants import AUDIO_CODEC_MAP, DYNAMIC_RANGE_MAP, VIDEO_CODEC_MAP
|
||||
from unshackle.core.drm import cbcs
|
||||
from unshackle.core.providers.anilist import parse_anilist_ref
|
||||
from unshackle.core.proxies.resolve import initialize_proxy_providers, resolve_proxy
|
||||
from unshackle.core.services import Services
|
||||
@@ -3834,6 +3835,9 @@ async def session_tracks_handler(
|
||||
"session_cookies": session_cookies,
|
||||
"server_cdm": session.server_cdm,
|
||||
"server_cdm_type": server_cdm_type,
|
||||
"sample_decrypt_tracks": [
|
||||
str(t.id) for t in [*video_tracks, *audio_tracks] if service_instance.decrypts_samples(t)
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
@@ -4826,6 +4830,59 @@ async def session_segment_filter_handler(
|
||||
return web.json_response({"unwanted": unwanted})
|
||||
|
||||
|
||||
async def session_samples_handler(
|
||||
session_id: str, track_id: str, entry: str, frame: bytes, request: Optional[web.Request] = None
|
||||
) -> web.Response:
|
||||
"""Run the service's sample decrypter for one `stsd` entry of a track that `decrypts_samples` names.
|
||||
|
||||
An empty `frame` asks how the entry decrypts: its content key, or `decrypter`. A `cbcs.pack_samples`
|
||||
frame of block-aligned ciphertexts gets a frame of their plaintexts back. The decrypter can use a
|
||||
process or an account of the server, so the `server_cdm` grant gates it.
|
||||
"""
|
||||
session = await get_validated_session(session_id, request)
|
||||
require_authenticated(session)
|
||||
|
||||
index = int(entry) if entry.isascii() and entry.isdigit() else 0
|
||||
if not track_id or index < 1:
|
||||
raise APIError(APIErrorCode.INVALID_INPUT, "Required: track_id, and entry as a 1-based stsd index")
|
||||
track = session.tracks.get(track_id)
|
||||
if not track:
|
||||
raise APIError(
|
||||
APIErrorCode.TRACK_NOT_FOUND,
|
||||
f"Track not found in session: {track_id}",
|
||||
details={"track_id": track_id, "session_id": session_id},
|
||||
)
|
||||
service = session.service_instance
|
||||
if not service.decrypts_samples(track):
|
||||
raise APIError(APIErrorCode.INVALID_INPUT, f"The service does not decrypt the samples of track {track_id}")
|
||||
if not server_cdm_allowed(request, session.service_tag):
|
||||
raise APIError(
|
||||
APIErrorCode.FORBIDDEN, f"This API key may not decrypt {session.service_tag} samples on the server"
|
||||
)
|
||||
|
||||
def run() -> web.Response:
|
||||
decrypter = service.get_sample_decrypter(track, index)
|
||||
if isinstance(decrypter, (bytes, bytearray)):
|
||||
return web.json_response({"key": bytes(decrypter).hex()})
|
||||
if not frame:
|
||||
return web.json_response({"decrypter": True})
|
||||
try:
|
||||
samples = cbcs.unpack_samples(frame)
|
||||
except ValueError as e:
|
||||
raise APIError(APIErrorCode.INVALID_INPUT, str(e)) from None
|
||||
return web.Response(body=cbcs.pack_samples(decrypter(samples)), content_type="application/octet-stream")
|
||||
|
||||
try:
|
||||
async with session.lock:
|
||||
return await asyncio.to_thread(run)
|
||||
except APIError:
|
||||
raise
|
||||
except (Exception, SystemExit):
|
||||
# `from None`: a debug-mode traceback would otherwise carry the service's exception chain
|
||||
log.exception(f"Error decrypting samples for track {sanitize_log(str(track_id))}")
|
||||
raise APIError(APIErrorCode.SERVICE_ERROR, "Could not decrypt the samples of this track") from None
|
||||
|
||||
|
||||
async def session_license_handler(
|
||||
data: Dict[str, Any], session_id: str, request: Optional[web.Request] = None
|
||||
) -> web.Response:
|
||||
|
||||
Reference in New Issue
Block a user