From cdb5b4fc55a029ff4086e32255e39f6721332b0e Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Thu, 28 Aug 2025 11:11:30 +0200 Subject: [PATCH] fix webhooks errors --- main_app.py | 30 ++++++++++++++++++++++++++++++ utils.py | 9 +++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/main_app.py b/main_app.py index 02dc9ba..e764ade 100644 --- a/main_app.py +++ b/main_app.py @@ -206,6 +206,36 @@ async def fake_claim_exchange(request: Request, _=host_required([Hosts.PLEX])): return await return_edited_response(upstream_response, data_override) +@app.post("/api/v2/user/webhooks", tags=[Hosts.CLIENTS.value]) +async def fake_get_user_webhooks(request: Request, _=host_required([Hosts.CLIENTS])): + # FIXME: this doesnt really work because webhooks are sent from Plex themselves so unless we implement a complete + # webhook emitter this is kinda useless but at least removes errors from UI... + + upstream_response = await call_official(request, request.url.path.lstrip("/")) + + # body = await request.body() + # raw_text = body.decode("utf-8") + # parsed_urls = parse_qs(raw_text) + # data_override = [ + # {"url": url} for url in parsed_urls.get("urls[]", []) + # ] + + data_override = [{"url": "https://webhooks.are.not.supported.because.plex.sends.them/not/your/server"}] + + return await return_edited_response(upstream_response, data_override) + + +@app.get("/api/v2/user/webhooks", tags=[Hosts.CLIENTS.value]) +async def fake_list_user_webhooks(request: Request, _=host_required([Hosts.CLIENTS])): + # See note in POST /api/v2/user/webhooks + + upstream_response = await call_official(request, request.url.path.lstrip("/")) + + data_override = [] + + return await return_edited_response(upstream_response, data_override) + + @app.get("/favicon.ico") async def favicon(): return FileResponse("favicon.ico") diff --git a/utils.py b/utils.py index afc43ad..b3e9b19 100644 --- a/utils.py +++ b/utils.py @@ -18,9 +18,10 @@ official_client = httpx.AsyncClient( def host_required(allowed_hosts: list[Hosts]): async def dependency(request: Request): host = request.url.hostname - if host not in [h.value for h in allowed_hosts]: - logger.warning(f"Host '{host}' not allowed for url '{request.url}'") - raise HTTPException(status_code=403, detail=f"Host '{host}' not allowed") + if host != "localhost" and host != "127.0.0.1": + if host not in [h.value for h in allowed_hosts]: + logger.warning(f"Host '{host}' not allowed for url '{request.url}'") + raise HTTPException(status_code=403, detail=f"Host '{host}' not allowed") return Depends(dependency) @@ -64,7 +65,7 @@ async def call_official(request: Request, path: str) -> httpx.Response: async def return_edited_response( response: httpx.Response, - new_content: dict | bytes | None = None + new_content: dict | bytes | list | str | None = None ) -> Response: """ Return possibly modified response.