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.
This commit is contained in:
rlaphoenix 2022-08-01 22:46:01 +01:00
parent a4d8be683b
commit 2ba13f5e07
1 changed files with 28 additions and 1 deletions

View File

@ -3,7 +3,7 @@ import sys
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Optional, Union
from pywidevine.exceptions import TooManySessions from pywidevine.exceptions import TooManySessions, InvalidSession
try: try:
from aiohttp import web 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}") @routes.post("/{device}/challenge/{license_type}")
async def challenge(request: web.Request) -> web.Response: async def challenge(request: web.Request) -> web.Response:
secret_key = request.headers["X-Secret-Key"] secret_key = request.headers["X-Secret-Key"]