About #10
This is not a correct fix.
First of all:
class RawWebSocket(WebSocketsResource, OldWebSocketsResource):
def _makeFactory(self):
f = PeerOverrideFactory(self.parent._factory)
WebSocketsResource.__init__(self, self.parent._factory)
OldWebSocketsResource.__init__(self, self.parent._factory)
first line in this method is completely unused.
Should be:
class RawWebSocket(WebSocketsResource, OldWebSocketsResource):
def _makeFactory(self):
f = PeerOverrideFactory(self.parent._factory)
WebSocketsResource.__init__(f)
OldWebSocketsResource.__init__(f)
otherwise PeerOverrideFactory is completely unused.
Also, getPeer should probably return port as well and support header in format $remote_addr:$remote_port;
E.g. in nginx:
proxy_set_header X-Real-IP $remote_addr:$remote_port;
In this case PeerOverrideProtocol can look like:
class PeerOverrideProtocol(ProtocolWrapper):
def getPeer(self):
if self.parent._options["proxy_header"] and self.request.requestHeaders.hasHeader(
self.parent._options["proxy_header"]):
print self.request.requestHeaders.getRawHeaders(self.parent._options["proxy_header"])
ip = self.request.requestHeaders.getRawHeaders(self.parent._options["proxy_header"])[0].split(",")[
-1].strip()
regex = re.match(r"^(?P<ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(:(?P<port>\d{1,5}))?$", ip)
ipv4 = True
if not regex:
ipv4 = False
regex = re.match(r"^(?P<ip>.*?)(:(?P<port>\d{1,5}))?$", ip)
ip = regex.group('ip')
port = int(regex.group('port')) or ProtocolWrapper.getPeer(self).port
if ipv4:
return address.IPv4Address("TCP", ip, port)
else:
return address.IPv6Address("TCP", ip, port)
return ProtocolWrapper.getPeer(self)
IPV6 is not completely parsed here but this address doesn't really have much relevance when behind the proxy other than for logging and informative purposes.
About #10
This is not a correct fix.
First of all:
first line in this method is completely unused.
Should be:
otherwise PeerOverrideFactory is completely unused.
Also, getPeer should probably return port as well and support header in format $remote_addr:$remote_port;
E.g. in nginx:
In this case PeerOverrideProtocol can look like:
IPV6 is not completely parsed here but this address doesn't really have much relevance when behind the proxy other than for logging and informative purposes.