diff --git a/Pipfile b/Pipfile index b2422b8..f7468f9 100644 --- a/Pipfile +++ b/Pipfile @@ -16,6 +16,7 @@ pyyaml = "*" ua-parser = "*" user-agents = "*" django-humanize = "*" +anonymizeip = "*" [requires] python_version = "3.6" diff --git a/Pipfile.lock b/Pipfile.lock index 4cca2e3..2d9ba1f 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "13b07e6285b739fe6f4987482aa448f1773d051de2de4806126a0e4bded150cf" + "sha256": "5c4a63923138fea970c8851ac72d6d6a391db28090a02d738e675775d2c6f26b" }, "pipfile-spec": 6, "requires": { @@ -23,6 +23,14 @@ ], "version": "==2.5.2" }, + "anonymizeip": { + "hashes": [ + "sha256:491cb94a31bae23294c5b93a13dd5c9ed55be98003c622e76e2fe64d6a4f3e91", + "sha256:e0d446b06b2bbf236394a90b971de403f90f805d14db3a405f8731716acad1fe" + ], + "index": "pypi", + "version": "==1.0.0" + }, "asgiref": { "hashes": [ "sha256:8036f90603c54e93521e5777b2b9a39ba1bad05773fcf2d208f0299d1df58ce5", diff --git a/shynet/analytics/tasks.py b/shynet/analytics/tasks.py index a210900..be39cd4 100644 --- a/shynet/analytics/tasks.py +++ b/shynet/analytics/tasks.py @@ -7,6 +7,7 @@ from celery import shared_task from django.conf import settings from django.core.cache import cache from django.utils import timezone +from anonymizeip import anonymize_ip from core.models import Service @@ -44,11 +45,15 @@ def ingress_request( service_uuid, tracker, time, payload, ip, location, user_agent, identifier="" ): try: - ip_data = _geoip2_lookup(ip) - service = Service.objects.get(uuid=service_uuid, status=Service.ACTIVE) log.debug(f"Linked to service {service}") + ip_data = _geoip2_lookup(ip) + log.debug(f"Found geoip2 data") + + if service.anonymize_ips: + ip = anonymize_ip(ip) + # Create or update session session = Session.objects.filter( service=service, diff --git a/shynet/core/models.py b/shynet/core/models.py index 5e5105e..52ccb59 100644 --- a/shynet/core/models.py +++ b/shynet/core/models.py @@ -39,6 +39,9 @@ class Service(models.Model): max_length=2, choices=SERVICE_STATUSES, default=ACTIVE, db_index=True ) + # Analytics settings + anonymize_ips = models.BooleanField(default=False) + def __str__(self): return self.name