2022-04-04 01:13:48 +02:00
|
|
|
import os
|
2023-07-14 05:27:20 +02:00
|
|
|
from contextlib import suppress
|
2022-04-04 01:13:48 +02:00
|
|
|
|
|
|
|
from channels.auth import AuthMiddlewareStack
|
|
|
|
from channels.routing import ProtocolTypeRouter, URLRouter
|
2024-03-29 12:24:45 +01:00
|
|
|
from channels.security.websocket import OriginValidator
|
2022-04-04 01:13:48 +02:00
|
|
|
from django.core.asgi import get_asgi_application
|
|
|
|
|
2023-07-14 05:27:20 +02:00
|
|
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "c3nav.settings")
|
2023-12-08 12:58:44 +01:00
|
|
|
os.environ.setdefault("C3NAV_CONN_MAX_AGE", "0")
|
2023-07-14 05:27:20 +02:00
|
|
|
django_asgi = get_asgi_application()
|
2022-04-04 01:13:48 +02:00
|
|
|
|
2023-12-11 19:02:19 +01:00
|
|
|
from c3nav.control.middleware import UserPermissionsChannelMiddleware # noqa
|
|
|
|
from c3nav.urls import websocket_urlpatterns # noqa
|
2023-12-02 02:57:41 +01:00
|
|
|
|
2024-03-29 12:26:47 +01:00
|
|
|
from c3nav import settings
|
|
|
|
|
2024-03-29 12:24:45 +01:00
|
|
|
|
|
|
|
class OriginValidatorWithAllowNone(OriginValidator):
|
|
|
|
def valid_origin(self, parsed_origin):
|
|
|
|
"""
|
|
|
|
Checks parsed origin is None.
|
|
|
|
We want to allow None because browsers always send the Origin header and non-browser clients do not need CORS
|
|
|
|
|
|
|
|
Pass control to the validate_origin function.
|
|
|
|
|
|
|
|
Returns ``True`` if validation function was successful, ``False`` otherwise.
|
|
|
|
"""
|
|
|
|
# None is not allowed unless all hosts are allowed
|
|
|
|
if parsed_origin is None:
|
|
|
|
return True
|
|
|
|
return self.validate_origin(parsed_origin)
|
|
|
|
|
|
|
|
|
|
|
|
def AllowedHostsOriginValidatorWithAllowNone(app):
|
|
|
|
"""
|
|
|
|
Factory function which returns an OriginValidatorWithAllowNone configured to use
|
|
|
|
settings.ALLOWED_HOSTS.
|
|
|
|
"""
|
|
|
|
allowed_hosts = settings.ALLOWED_HOSTS
|
|
|
|
if settings.DEBUG and not allowed_hosts:
|
|
|
|
allowed_hosts = ["localhost", "127.0.0.1", "[::1]"]
|
|
|
|
return OriginValidatorWithAllowNone(app, allowed_hosts)
|
|
|
|
|
|
|
|
|
2022-04-04 01:13:48 +02:00
|
|
|
application = ProtocolTypeRouter({
|
2023-07-14 05:27:20 +02:00
|
|
|
"http": django_asgi,
|
2024-03-29 12:24:45 +01:00
|
|
|
"websocket": AllowedHostsOriginValidatorWithAllowNone(
|
2023-11-09 17:04:55 +01:00
|
|
|
AuthMiddlewareStack(
|
|
|
|
UserPermissionsChannelMiddleware(
|
|
|
|
URLRouter(websocket_urlpatterns),
|
|
|
|
),
|
|
|
|
),
|
2023-07-14 05:27:20 +02:00
|
|
|
),
|
2022-04-04 01:13:48 +02:00
|
|
|
})
|
2023-07-14 05:27:20 +02:00
|
|
|
|
|
|
|
# optional support for static files via starlette
|
|
|
|
with suppress(ImportError):
|
|
|
|
# settings need to be loaded after django init via get_asgi_application
|
|
|
|
from django.conf import settings
|
|
|
|
from starlette.applications import Starlette
|
|
|
|
from starlette.routing import Mount
|
|
|
|
from starlette.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
static_app = ProtocolTypeRouter({
|
|
|
|
"http": Starlette(routes=[
|
2023-12-08 00:15:55 +01:00
|
|
|
Mount(
|
|
|
|
path=settings.STATIC_URL,
|
|
|
|
app=StaticFiles(directory=settings.STATIC_ROOT, follow_symlink=True),
|
|
|
|
name='static',
|
|
|
|
),
|
2023-12-11 19:02:19 +01:00
|
|
|
Mount(path='/', app=django_asgi),
|
2023-07-14 05:27:20 +02:00
|
|
|
]),
|
2024-03-29 14:46:54 +01:00
|
|
|
"websocket": AllowedHostsOriginValidatorWithAllowNone(
|
2024-03-28 19:34:34 +01:00
|
|
|
AuthMiddlewareStack(
|
|
|
|
UserPermissionsChannelMiddleware(
|
|
|
|
URLRouter(websocket_urlpatterns),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-07-14 05:27:20 +02:00
|
|
|
})
|