Jonne Haß 5e637b14c9 Handle IPv4-mapped IPv6 addresses
Some reverse proxy configurations pass REMOTE_ADDR
as a IPv4-mapped IPv6 address when listening on a
IPv6 socket. This patch converts such a mapped
address into a IPv4 address at all usages of
REMOTE_ADDR. It handles both, the ::ffff:192.0.2.128
format as well as the deprecated ::192.0.2.128 format.
2014-09-29 06:21:24 +02:00

18 lines
366 B
Python

"""
Misc. IP tools: normalize, handle mapped addresses
"""
from netaddr import IPAddress
def normalize_ip(ipaddr):
ipaddr = normalize_mapped_address(ipaddr)
return ipaddr
def normalize_mapped_address(ipaddr):
ipaddr = IPAddress(ipaddr)
if ipaddr.is_ipv4_compat() or ipaddr.is_ipv4_mapped():
ipaddr = ipaddr.ipv4()
return str(ipaddr)