expose visitor's real ip to websocket

This commit is contained in:
iou1name 2018-08-30 23:33:25 -04:00
parent b5f8f69f69
commit 738591b05e
2 changed files with 24 additions and 1 deletions

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
"""
Some custom asgi middleware for titivillus.
"""
from channels.auth import AuthMiddlewareStack
class XRealIPMiddleware(object):
"""
Attempts to set the get the visitor's real IP by grabbing the
`X-Real-IP` header from the request.
"""
def __init__(self, inner):
self.inner = inner
def __call__(self, scope):
headers = dict(scope['headers'])
if b'x-real-ip' in headers:
scope['client'][0] = headers[b'x-real-ip'].decode('utf-8')
return self.inner(scope)

View File

@ -6,9 +6,14 @@ from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
import quest.routing
from .asgi_middleware import XRealIPMiddleware
CustomMiddlewareStack = lambda inner: XRealIPMiddleware(
AuthMiddlewareStack(inner)
)
application = ProtocolTypeRouter({
'websocket': AuthMiddlewareStack(
'websocket': CustomMiddlewareStack(
URLRouter(
quest.routing.websocket_urlpatterns
)