From e283d32b56875a15e667e49e29dd3902b7dea4df Mon Sep 17 00:00:00 2001 From: David Fischer Date: Wed, 6 Jun 2018 09:11:08 -0700 Subject: [PATCH] Move IP mask constant --- readthedocs/analytics/utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/readthedocs/analytics/utils.py b/readthedocs/analytics/utils.py index 3f71b0b9791..3e5ea0e4222 100644 --- a/readthedocs/analytics/utils.py +++ b/readthedocs/analytics/utils.py @@ -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""" @@ -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