Closing websocket connections to unknown urls

This commit is contained in:
FinnStutzenstein 2019-10-15 10:54:31 +02:00
parent f9cea53659
commit e4c4bc0aa0
2 changed files with 19 additions and 2 deletions

View File

@ -1,13 +1,15 @@
from channels.routing import ProtocolTypeRouter, URLRouter
from django.conf.urls import url
from openslides.utils.consumers import SiteConsumer
from openslides.utils.consumers import CloseConsumer, SiteConsumer
from openslides.utils.middleware import AuthMiddlewareStack
application = ProtocolTypeRouter(
{
# WebSocket chat handler
"websocket": AuthMiddlewareStack(URLRouter([url(r"^ws/$", SiteConsumer)]))
"websocket": AuthMiddlewareStack(
URLRouter([url(r"^ws/$", SiteConsumer), url(".*", CloseConsumer)])
)
}
)

View File

@ -3,6 +3,8 @@ from collections import defaultdict
from typing import Any, Dict, List, Optional
from urllib.parse import parse_qs
from channels.generic.websocket import AsyncWebsocketConsumer
from ..utils.websocket import WEBSOCKET_CHANGE_ID_TOO_HIGH
from . import logging
from .auth import async_anonymous_is_enabled
@ -217,3 +219,16 @@ class SiteConsumer(ProtocollAsyncJsonWebsocketConsumer):
content = {"change_id": change_id, "data": data}
await self.send_json(type="projector", content=content, in_response=in_response)
class CloseConsumer(AsyncWebsocketConsumer):
""" Auto-closes the connection """
groups: List[str] = []
def __init__(self, args: Dict[str, Any], **kwargs: Any) -> None:
logger.info(f'Closing connection to unknown websocket url {args["path"]}')
async def connect(self) -> None:
await self.accept()
await self.close()