From 4a76fb023e2b4d2256c68d95d5086ce7b5f41c98 Mon Sep 17 00:00:00 2001 From: n9k Date: Wed, 22 Jun 2022 08:10:42 +0000 Subject: [PATCH] Access captcha: special case for websocket There doesn't seem to be a way to catch a 403 Forbidden error opening a websocket with JavaScript, so this commit changes the behaviour to this: open the websocket normally, send one "kick" message, close the websocket. --- anonstream/routes/websocket.py | 29 +++++++++++++++++------------ anonstream/static/anonstream.js | 5 +++++ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/anonstream/routes/websocket.py b/anonstream/routes/websocket.py index 96dbb84..95048c7 100644 --- a/anonstream/routes/websocket.py +++ b/anonstream/routes/websocket.py @@ -9,16 +9,21 @@ from anonstream.websocket import websocket_outbound, websocket_inbound from anonstream.routes.wrappers import with_user_from @current_app.websocket('/live') -@with_user_from(websocket) -async def live(timestamp, user): - queue = asyncio.Queue() - user['websockets'][queue] = timestamp - reading(user, timestamp=timestamp) +@with_user_from(websocket, fallback_to_token=True) +async def live(timestamp, user_or_token): + match user_or_token: + case str() | None: + await websocket.send_json({'type': 'kick'}) + await websocket.close(1001) + case dict() as user: + queue = asyncio.Queue() + user['websockets'][queue] = timestamp + reading(user, timestamp=timestamp) - producer = websocket_outbound(queue, user) - consumer = websocket_inbound(queue, user) - try: - await asyncio.gather(producer, consumer) - finally: - see(user) - user['websockets'].pop(queue) + producer = websocket_outbound(queue, user) + consumer = websocket_inbound(queue, user) + try: + await asyncio.gather(producer, consumer) + finally: + see(user) + user['websockets'].pop(queue) diff --git a/anonstream/static/anonstream.js b/anonstream/static/anonstream.js index 3873e6f..4b071ff 100644 --- a/anonstream/static/anonstream.js +++ b/anonstream/static/anonstream.js @@ -831,6 +831,11 @@ const on_websocket_message = (event) => { ws.send(JSON.stringify(payload)); break; + case "kick": + console.log("ws kick"); + window.location.reload(); + break; + default: console.log("incomprehensible websocket message", receipt); }