Titivillus/titivillus/asgi_middleware.py

19 lines
497 B
Python

#!/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)