expose visitor's real ip to websocket
This commit is contained in:
parent
b5f8f69f69
commit
738591b05e
18
titivillus/asgi_middleware.py
Normal file
18
titivillus/asgi_middleware.py
Normal 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)
|
|
@ -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
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user