Format code

This commit is contained in:
R. Miles McCain 2020-11-26 20:09:55 +00:00
parent a42455c9dc
commit ede06900e5
No known key found for this signature in database
GPG Key ID: F1053629E2905557
6 changed files with 52 additions and 29 deletions

View File

@ -5,7 +5,12 @@ from urllib.parse import urlparse
from django.conf import settings from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.core.exceptions import ValidationError 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.shortcuts import render, reverse
from django.utils import timezone from django.utils import timezone
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
@ -53,7 +58,10 @@ class ValidateServiceOriginsMixin:
if origins != "*": if origins != "*":
remote_origin = request.META.get("HTTP_ORIGIN") 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")) parsed = urlparse(request.META.get("HTTP_REFERER"))
remote_origin = f"{parsed.scheme}://{parsed.netloc}".lower() remote_origin = f"{parsed.scheme}://{parsed.netloc}".lower()
origins = [origin.strip().lower() for origin in origins.split(",")] origins = [origin.strip().lower() for origin in origins.split(",")]

View File

@ -37,7 +37,7 @@ REFERRERS = [
"", "",
"", "",
"", "",
"" "",
] ]
USER_AGENTS = [ 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 (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 (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)", "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")) owner = User.objects.get(email=options.get("owner_email"))
service = Service.objects.create(name=options.get("name"), owner=owner) 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 # Go through each day requested, creating sessions and hits
for days in range(options.get("days")): for days in range(options.get("days")):
@ -78,12 +80,13 @@ class Command(BaseCommand):
print(f"Populating info for {day}...") print(f"Populating info for {day}...")
avg = options.get("avg") avg = options.get("avg")
deviation = options.get("deviation") 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) n = avg + random.randrange(-1 * deviation * avg, deviation * avg)
for _ in range( for _ in range(n):
n
):
time = day + timedelta( time = day + timedelta(
hours=random.randrange(0, 23), hours=random.randrange(0, 23),
minutes=random.randrange(0, 59), minutes=random.randrange(0, 59),
@ -92,14 +95,20 @@ class Command(BaseCommand):
ip = random.choice(ips) ip = random.choice(ips)
load_time = random.normalvariate(options.get("load_time"), 500) load_time = random.normalvariate(options.get("load_time"), 500)
referrer = random.choice(REFERRERS) 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) 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}!") print(f"Created {n} demo hits on {day}!")
self.stdout.write( self.stdout.write(self.style.SUCCESS(f"Successfully created demo data!"))
self.style.SUCCESS(
f"Successfully created demo data!"
)
)

View File

@ -6,18 +6,20 @@ from django.db import migrations, models
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('core', '0007_service_ignore_robots'), ("core", "0007_service_ignore_robots"),
] ]
operations = [ operations = [
migrations.AddField( migrations.AddField(
model_name='service', model_name="service",
name='script_inject', name="script_inject",
field=models.TextField(blank=True, default=''), field=models.TextField(blank=True, default=""),
), ),
migrations.AlterField( migrations.AlterField(
model_name='user', model_name="user",
name='first_name', name="first_name",
field=models.CharField(blank=True, max_length=150, verbose_name='first name'), field=models.CharField(
blank=True, max_length=150, verbose_name="first name"
),
), ),
] ]

View File

@ -18,7 +18,7 @@ class ServiceForm(forms.ModelForm):
"hide_referrer_regex", "hide_referrer_regex",
"origins", "origins",
"collaborators", "collaborators",
"script_inject" "script_inject",
] ]
widgets = { widgets = {
"name": forms.TextInput(), "name": forms.TextInput(),
@ -28,7 +28,7 @@ class ServiceForm(forms.ModelForm):
"collect_ips": forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]), "collect_ips": forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]),
"ignore_robots": forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]), "ignore_robots": forms.RadioSelect(choices=[(True, "Yes"), (False, "No")]),
"hide_referrer_regex": forms.TextInput(), "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 = { labels = {
"origins": "Allowed origins", "origins": "Allowed origins",
@ -60,7 +60,9 @@ class ServiceForm(forms.ModelForm):
def clean_collaborators(self): def clean_collaborators(self):
collaborators = [] 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(","): for collaborator_email in self.cleaned_data["collaborators"].split(","):
email = collaborator_email.strip() email = collaborator_email.strip()
if email == "": if email == "":
@ -72,7 +74,9 @@ class ServiceForm(forms.ModelForm):
raise forms.ValidationError(f"Email '{email}' is not registered") raise forms.ValidationError(f"Email '{email}' is not registered")
user = collaborator_email_linked.user user = collaborator_email_linked.user
if user in collaborators: 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 users_to_emails[user] = email
collaborators.append(collaborator_email_linked.user) collaborators.append(collaborator_email_linked.user)
return collaborators return collaborators

View File

@ -146,7 +146,7 @@ def iconify(text):
"chrome mobile webview": "chrome.com", "chrome mobile webview": "chrome.com",
"firefox mobile": "firefox.com", "firefox mobile": "firefox.com",
"edge mobile": "microsoft.com", "edge mobile": "microsoft.com",
"chromium": "chromium.org" "chromium": "chromium.org",
} }
domain = None domain = None

View File

@ -320,4 +320,4 @@ SESSION_MEMORY_TIMEOUT = int(os.getenv("SESSION_MEMORY_TIMEOUT", "1800"))
SHOW_SHYNET_VERSION = os.getenv("SHOW_SHYNET_VERSION", "True") == "True" SHOW_SHYNET_VERSION = os.getenv("SHOW_SHYNET_VERSION", "True") == "True"
# Should Shynet show third-party icons in the dashboard? # Should Shynet show third-party icons in the dashboard?
SHOW_THIRD_PARTY_ICONS = os.getenv("SHOW_THIRD_PARTY_ICONS", "True") == "True" SHOW_THIRD_PARTY_ICONS = os.getenv("SHOW_THIRD_PARTY_ICONS", "True") == "True"