diff --git a/shynet/analytics/views/ingress.py b/shynet/analytics/views/ingress.py index 51e2343..0c1ded2 100644 --- a/shynet/analytics/views/ingress.py +++ b/shynet/analytics/views/ingress.py @@ -5,7 +5,12 @@ from urllib.parse import urlparse from django.conf import settings from django.core.cache import cache from django.core.exceptions import ValidationError -from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseForbidden +from django.http import ( + Http404, + HttpResponse, + HttpResponseBadRequest, + HttpResponseForbidden, +) from django.shortcuts import render, reverse from django.utils import timezone from django.utils.decorators import method_decorator @@ -53,7 +58,10 @@ class ValidateServiceOriginsMixin: if origins != "*": remote_origin = request.META.get("HTTP_ORIGIN") - if remote_origin is None and request.META.get("HTTP_REFERER") is not None: + if ( + remote_origin is None + and request.META.get("HTTP_REFERER") is not None + ): parsed = urlparse(request.META.get("HTTP_REFERER")) remote_origin = f"{parsed.scheme}://{parsed.netloc}".lower() origins = [origin.strip().lower() for origin in origins.split(",")] diff --git a/shynet/core/management/commands/demo.py b/shynet/core/management/commands/demo.py index deaf905..f66a2f3 100644 --- a/shynet/core/management/commands/demo.py +++ b/shynet/core/management/commands/demo.py @@ -37,7 +37,7 @@ REFERRERS = [ "", "", "", - "" + "", ] USER_AGENTS = [ @@ -45,7 +45,7 @@ USER_AGENTS = [ "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/43.4", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko)", - "Version/10.0 Mobile/14E304 Safari/602.1" + "Version/10.0 Mobile/14E304 Safari/602.1", ] @@ -70,7 +70,9 @@ class Command(BaseCommand): owner = User.objects.get(email=options.get("owner_email")) service = Service.objects.create(name=options.get("name"), owner=owner) - print(f"Created demo service `{service.name}` (uuid: `{service.uuid}`, owner: {owner})") + print( + f"Created demo service `{service.name}` (uuid: `{service.uuid}`, owner: {owner})" + ) # Go through each day requested, creating sessions and hits for days in range(options.get("days")): @@ -78,12 +80,13 @@ class Command(BaseCommand): print(f"Populating info for {day}...") avg = options.get("avg") deviation = options.get("deviation") - ips = [".".join(map(str, (random.randint(0, 255) for _ in range(4)))) for _ in range(avg)] + ips = [ + ".".join(map(str, (random.randint(0, 255) for _ in range(4)))) + for _ in range(avg) + ] n = avg + random.randrange(-1 * deviation * avg, deviation * avg) - for _ in range( - n - ): + for _ in range(n): time = day + timedelta( hours=random.randrange(0, 23), minutes=random.randrange(0, 59), @@ -92,14 +95,20 @@ class Command(BaseCommand): ip = random.choice(ips) load_time = random.normalvariate(options.get("load_time"), 500) referrer = random.choice(REFERRERS) - location = "https://example.com" + random.choice(LOCATIONS).replace("{rand}", str(random.randint(0, n))) + location = "https://example.com" + random.choice(LOCATIONS).replace( + "{rand}", str(random.randint(0, n)) + ) user_agent = random.choice(USER_AGENTS) - ingress_request(service.uuid, "JS", time, {"loadTime": load_time, "referrer": referrer}, ip, location, user_agent) + ingress_request( + service.uuid, + "JS", + time, + {"loadTime": load_time, "referrer": referrer}, + ip, + location, + user_agent, + ) print(f"Created {n} demo hits on {day}!") - self.stdout.write( - self.style.SUCCESS( - f"Successfully created demo data!" - ) - ) + self.stdout.write(self.style.SUCCESS(f"Successfully created demo data!")) diff --git a/shynet/core/migrations/0008_auto_20200628_1403.py b/shynet/core/migrations/0008_auto_20200628_1403.py index c7d0917..b01064d 100644 --- a/shynet/core/migrations/0008_auto_20200628_1403.py +++ b/shynet/core/migrations/0008_auto_20200628_1403.py @@ -6,18 +6,20 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('core', '0007_service_ignore_robots'), + ("core", "0007_service_ignore_robots"), ] operations = [ migrations.AddField( - model_name='service', - name='script_inject', - field=models.TextField(blank=True, default=''), + model_name="service", + name="script_inject", + field=models.TextField(blank=True, default=""), ), migrations.AlterField( - model_name='user', - name='first_name', - field=models.CharField(blank=True, max_length=150, verbose_name='first name'), + model_name="user", + name="first_name", + field=models.CharField( + blank=True, max_length=150, verbose_name="first name" + ), ), ] diff --git a/shynet/dashboard/forms.py b/shynet/dashboard/forms.py index 29965ab..9a926e0 100644 --- a/shynet/dashboard/forms.py +++ b/shynet/dashboard/forms.py @@ -18,7 +18,7 @@ class ServiceForm(forms.ModelForm): "hide_referrer_regex", "origins", "collaborators", - "script_inject" + "script_inject", ] widgets = { "name": forms.TextInput(), @@ -28,7 +28,7 @@ class ServiceForm(forms.ModelForm): "collect_ips": forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]), "ignore_robots": forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]), "hide_referrer_regex": forms.TextInput(), - "script_inject": forms.Textarea(attrs={'class':'font-mono', 'rows': 5}) + "script_inject": forms.Textarea(attrs={"class": "font-mono", "rows": 5}), } labels = { "origins": "Allowed origins", @@ -60,7 +60,9 @@ class ServiceForm(forms.ModelForm): def clean_collaborators(self): collaborators = [] - users_to_emails = {} # maps users to the email they are listed under as a collaborator + users_to_emails = ( + {} + ) # maps users to the email they are listed under as a collaborator for collaborator_email in self.cleaned_data["collaborators"].split(","): email = collaborator_email.strip() if email == "": @@ -72,7 +74,9 @@ class ServiceForm(forms.ModelForm): raise forms.ValidationError(f"Email '{email}' is not registered") user = collaborator_email_linked.user if user in collaborators: - raise forms.ValidationError(f"The emails '{email}' and '{users_to_emails[user]}' both correspond to the same user") + raise forms.ValidationError( + f"The emails '{email}' and '{users_to_emails[user]}' both correspond to the same user" + ) users_to_emails[user] = email collaborators.append(collaborator_email_linked.user) return collaborators diff --git a/shynet/dashboard/templatetags/helpers.py b/shynet/dashboard/templatetags/helpers.py index 5bbdfed..76cc36e 100644 --- a/shynet/dashboard/templatetags/helpers.py +++ b/shynet/dashboard/templatetags/helpers.py @@ -146,7 +146,7 @@ def iconify(text): "chrome mobile webview": "chrome.com", "firefox mobile": "firefox.com", "edge mobile": "microsoft.com", - "chromium": "chromium.org" + "chromium": "chromium.org", } domain = None diff --git a/shynet/shynet/settings.py b/shynet/shynet/settings.py index 3661120..95f333e 100644 --- a/shynet/shynet/settings.py +++ b/shynet/shynet/settings.py @@ -320,4 +320,4 @@ SESSION_MEMORY_TIMEOUT = int(os.getenv("SESSION_MEMORY_TIMEOUT", "1800")) SHOW_SHYNET_VERSION = os.getenv("SHOW_SHYNET_VERSION", "True") == "True" # Should Shynet show third-party icons in the dashboard? -SHOW_THIRD_PARTY_ICONS = os.getenv("SHOW_THIRD_PARTY_ICONS", "True") == "True" \ No newline at end of file +SHOW_THIRD_PARTY_ICONS = os.getenv("SHOW_THIRD_PARTY_ICONS", "True") == "True"