Skip to content

Commit

Permalink
Move IP mask constant
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfischer committed Jun 6, 2018
1 parent 2c09fda commit e283d32
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions readthedocs/analytics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

log = logging.getLogger(__name__) # noqa

# Used to anonymize an IP by zero-ing out the last 2 bytes
MASK = int('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000', 16)


def get_client_ip(request):
"""Gets the real IP based on a request object"""
Expand All @@ -36,12 +33,15 @@ def get_client_ip(request):

def anonymize_ip_address(ip_address):
"""Anonymizes an IP address by zeroing the last 2 bytes"""
# Used to anonymize an IP by zero-ing out the last 2 bytes
ip_mask = int('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFF0000', 16)

try:
ip_obj = ipaddress.ip_address(force_text(ip_address))
except ValueError:
return None

anonymized_ip = ipaddress.ip_address(int(ip_obj) & MASK)
anonymized_ip = ipaddress.ip_address(int(ip_obj) & ip_mask)
return anonymized_ip.compressed


Expand Down

0 comments on commit e283d32

Please sign in to comment.