serve: Set `Server` response header with pywidevine version

This allows clients to test with a HEAD request to / to see what version the API is running and test if it's actually a pywidevine serve API.
This commit is contained in:
rlaphoenix 2022-08-02 00:18:08 +01:00
parent 4f96ee402b
commit 4bc0edcca9
1 changed files with 31 additions and 23 deletions

View File

@ -3,8 +3,6 @@ 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, InvalidSession
try: try:
from aiohttp import web from aiohttp import web
except ImportError: except ImportError:
@ -15,8 +13,10 @@ except ImportError:
) )
sys.exit(1) sys.exit(1)
from pywidevine import __version__
from pywidevine.cdm import Cdm from pywidevine.cdm import Cdm
from pywidevine.device import Device from pywidevine.device import Device
from pywidevine.exceptions import TooManySessions, InvalidSession
from pywidevine.license_protocol_pb2 import LicenseType, License from pywidevine.license_protocol_pb2 import LicenseType, License
routes = web.RouteTableDef() routes = web.RouteTableDef()
@ -256,29 +256,37 @@ async def keys(request: web.Request) -> web.Response:
@web.middleware @web.middleware
async def authentication(request: web.Request, handler) -> web.Response: async def authentication(request: web.Request, handler) -> web.Response:
secret_key = request.headers.get("X-Secret-Key") response = None
if not secret_key: if request.path != "/":
request.app.logger.debug(f"{request.remote} did not provide authorization.") secret_key = request.headers.get("X-Secret-Key")
return web.json_response({ if not secret_key:
"status": "401", request.app.logger.debug(f"{request.remote} did not provide authorization.")
"message": "Secret Key is Empty." response = web.json_response({
}, status=401) "status": "401",
"message": "Secret Key is Empty."
}, status=401)
elif secret_key not in request.app["config"]["users"]:
request.app.logger.debug(f"{request.remote} failed authentication with '{secret_key}'.")
response = web.json_response({
"status": "401",
"message": "Secret Key is Invalid, the Key is case-sensitive."
}, status=401)
if secret_key not in request.app["config"]["users"]: if response is None:
request.app.logger.debug(f"{request.remote} failed authentication with '{secret_key}'.") try:
return web.json_response({ response = await handler(request)
"status": "401", except web.HTTPException as e:
"message": "Secret Key is Invalid, the Key is case-sensitive." request.app.logger.error(f"An unexpected error has occurred, {e}")
}, status=401) response = web.json_response({
"status": 500,
"message": e.reason
}, status=500)
try: response.headers.update({
return await handler(request) "Server": f"https://github.com/rlaphoenix/pywidevine serve v{__version__}"
except web.HTTPException as e: })
request.app.logger.error(f"An unexpected error has occurred, {e}")
return web.json_response({ return response
"status": 500,
"message": e.reason
}, status=500)
def run(config: dict, host: Optional[Union[str, web.HostSequence]] = None, port: Optional[int] = None): def run(config: dict, host: Optional[Union[str, web.HostSequence]] = None, port: Optional[int] = None):