root: fix IP detection when using multiple reverse proxies

This commit is contained in:
Jens Langhammer 2020-09-20 13:36:23 +02:00
parent 2f4139df65
commit 9f403a71ed
2 changed files with 7 additions and 4 deletions

View File

@ -14,7 +14,7 @@ def _get_client_ip_from_meta(meta: Dict[str, Any]) -> Optional[str]:
) )
for _header in headers: for _header in headers:
if _header in meta: if _header in meta:
return meta.get(_header) return meta.get(_header).split(", ")[0]
return None return None

View File

@ -102,11 +102,14 @@ class ASGILogger:
await self.app(scope, receive, send_hooked) await self.app(scope, receive, send_hooked)
def _get_ip(self) -> str: def _get_ip(self) -> str:
client_ip = None
for header in ASGI_IP_HEADERS: for header in ASGI_IP_HEADERS:
if header in self.headers: if header in self.headers:
return self.headers[header].decode() client_ip = self.headers[header].decode()
if not client_ip:
client_ip, _ = self.scope.get("client", ("", 0)) client_ip, _ = self.scope.get("client", ("", 0))
return client_ip # Check if header has multiple values, and use the first one
return client_ip.split(", ")[0]
def log(self, runtime: float): def log(self, runtime: float):
"""Outpot access logs in a structured format""" """Outpot access logs in a structured format"""