anonymize IP if passed as ::ffff.1.2.3.4 correctly

pull/16/head
Martin Zimmermann 11 years ago
parent 88a7d40bea
commit 98ab6d205a

@ -101,9 +101,14 @@ def heading(host, path):
def anonymize(remote_addr):
ip = ipaddress.ip_address(remote_addr)
if ip.version == "4":
try:
ipv4 = ipaddress.IPv4Address(remote_addr)
return ''.join(ip.exploded.rsplit('.', 1)[0]) + '.' + '0'
return ip.exploded.rsplit(':', 5)[0]
except ipaddress.AddressValueError:
ipv6 = ipaddress.IPv6Address(remote_addr)
if ipv6.ipv4_mapped is not None:
return anonymize(ipv6.ipv4_mapped)
return ip.exploded.rsplit(':', 5)[0]
def salt(value, s=u'\x082@t9*\x17\xad\xc1\x1c\xa5\x98'):

@ -57,7 +57,7 @@ def create(app, environ, request, uri):
if "id" in data and not isinstance(data["id"], int):
return Response("Parent ID must be an integer.")
if "email" in data:
if data.get("email"):
hash = data["email"]
else:
hash = utils.salt(utils.anonymize(unicode(request.remote_addr)))

Loading…
Cancel
Save