mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-18 05:28:16 -06:00
fixes client ip detection for v6
This commit is contained in:
parent
fe3f21105c
commit
988e86c45b
@ -1,4 +1,4 @@
|
|||||||
from netaddr import IPAddress
|
from netaddr import AddrFormatError, IPAddress
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'get_client_ip',
|
'get_client_ip',
|
||||||
@ -17,10 +17,15 @@ def get_client_ip(request, additional_headers=()):
|
|||||||
)
|
)
|
||||||
for header in HTTP_HEADERS:
|
for header in HTTP_HEADERS:
|
||||||
if header in request.META:
|
if header in request.META:
|
||||||
client_ip = request.META[header].split(',')[0].partition(':')[0]
|
ip = request.META[header].split(',')[0].strip()
|
||||||
|
# Check if the IP address is v6 or v4
|
||||||
|
if ip.count(':') > 1:
|
||||||
|
client_ip = ip
|
||||||
|
else:
|
||||||
|
client_ip = ip.partition(':')[0]
|
||||||
try:
|
try:
|
||||||
return IPAddress(client_ip)
|
return IPAddress(client_ip)
|
||||||
except ValueError:
|
except (AddrFormatError, ValueError):
|
||||||
raise ValueError(f"Invalid IP address set for {header}: {client_ip}")
|
raise ValueError(f"Invalid IP address set for {header}: {client_ip}")
|
||||||
|
|
||||||
# Could not determine the client IP address from request headers
|
# Could not determine the client IP address from request headers
|
||||||
|
Loading…
Reference in New Issue
Block a user