Merge pull request #2014 from GulliblePsychologist/websocket

Allow X-Forwarded-For to be accepted for the WebSocket connections.
This commit is contained in:
Griatch 2020-01-19 12:56:19 +01:00 committed by GitHub
commit 3496ffb602

View file

@ -29,6 +29,7 @@ _RE_SCREENREADER_REGEX = re.compile(
r"%s" % settings.SCREENREADER_REGEX_STRIP, re.DOTALL + re.MULTILINE
)
_CLIENT_SESSIONS = mod_import(settings.SESSION_ENGINE).SessionStore
_UPSTREAM_IPS = settings.UPSTREAM_IPS
CLOSE_NORMAL = WebSocketServerProtocol.CLOSE_STATUS_CODE_NORMAL
@ -75,6 +76,16 @@ class WebSocketClient(WebSocketServerProtocol, Session):
"""
client_address = self.transport.client
client_address = client_address[0] if client_address else None
if client_address in _UPSTREAM_IPS and 'x-forwarded-for' in self.http_headers:
addresses = [x.strip() for x in self.http_headers['x-forwarded-for'].split(',')]
addresses.reverse()
for addr in addresses:
if addr not in _UPSTREAM_IPS:
client_address = addr
break
self.init_session("websocket", client_address, self.factory.sessionhandler)
csession = self.get_client_session() # this sets self.csessid