From 2ba13f5e07899374a7c7358ee50ef95b3bd8a20b Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Mon, 1 Aug 2022 22:46:01 +0100 Subject: [PATCH] serve: Add /close endpoint All client's should implement this and handle the 400 response safely. Under normal circumstances, with good client code, the 400 responses should not happen. --- pywidevine/serve.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pywidevine/serve.py b/pywidevine/serve.py index 1b31c82..53f324e 100644 --- a/pywidevine/serve.py +++ b/pywidevine/serve.py @@ -3,7 +3,7 @@ import sys from pathlib import Path from typing import Optional, Union -from pywidevine.exceptions import TooManySessions +from pywidevine.exceptions import TooManySessions, InvalidSession try: from aiohttp import web @@ -86,6 +86,33 @@ async def open(request: web.Request) -> web.Response: }) +@routes.get("/{device}/close/{session_id}") +async def close(request: web.Request) -> web.Response: + secret_key = request.headers["X-Secret-Key"] + device_name = request.match_info["device"] + session_id = bytes.fromhex(request.match_info["session_id"]) + + cdm = request.app["cdms"].get((secret_key, device_name)) + if not cdm: + return web.json_response({ + "status": 400, + "message": f"No Cdm session for {device_name} has been opened yet. No session to close." + }, status=400) + + try: + cdm.close(session_id) + except InvalidSession as e: + return web.json_response({ + "status": 400, + "message": str(e) + }, status=400) + + return web.json_response({ + "status": 200, + "message": f"Successfully closed Session '{session_id.hex()}'." + }) + + @routes.post("/{device}/challenge/{license_type}") async def challenge(request: web.Request) -> web.Response: secret_key = request.headers["X-Secret-Key"]