Skip to content

Add archive support, fix #1634 #1744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion interface/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
# Copyright: 2022, ECP, NLnet Labs and the Internet.nl contributors
# SPDX-License-Identifier: Apache-2.0
from dateutil.parser import isoparse

from django.conf import settings
from django.urls import path, re_path
from django.urls import path, re_path, register_converter
from django.conf.urls.static import static

from interface import views
from interface.batch import BATCH_API_MAJOR_VERSION
from interface.batch import views as batch
from interface.views import connection, domain, mail, stats


regex_tld = r"(?:[a-zA-Z]{2,63}|xn--[a-zA-Z0-9]+)"
regex_dname = r"(?P<dname>([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+" + regex_tld + ")"
regex_testid = r"(?P<request_id>[a-zA-Z0-9]{1,35})"
regex_mailaddr = (
r"(?P<mailaddr>([a-zA-Z0-9]{0,61}@)?([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+" r"" + regex_tld + ")"
)


class DateConverter:
# be as lenient as the isoparse library allows
regex = ".+"

def to_python(self, value):
# until python3.10 use dateutil.isoparser
# see: https://stackoverflow.com/questions/127803/how-do-i-parse-an-iso-8601-formatted-date-and-time
return isoparse(value)

def to_url(self, value):
return value


register_converter(DateConverter, "isodatetime")


urlpatterns = [
path("", views.indexpage),
re_path(r"^statistics/(?P<start_date>[0-9]{8})/(?P<end_date>[0-9]{8})/$", stats.statistics),
Expand Down Expand Up @@ -47,6 +67,7 @@
# these url()s should always be the last in the ^domain/ group
re_path(r"^(domain|site)/(?P<dname>.*)/$", domain.validate_domain),
re_path(r"^(domain|site)/(?P<dname>.*)/results$", domain.validate_domain),
path(r"archive/site/<str:domain_name>/<isodatetime:date>/", domain.resultsstored_historic),
path("test-mail/", views.testmailpage),
path("mail/", mail.index),
re_path(rf"^mail/{regex_mailaddr}/$", mail.mailprocess),
Expand All @@ -58,6 +79,7 @@
# these url()s should always be the last in the ^mail/ group
re_path(r"^mail/(?P<mailaddr>.*)/$", mail.validate_domain),
re_path(r"^mail/(?P<mailaddr>.*)/results$", mail.validate_domain),
path(r"archive/mail/<str:domain_name>/<isodatetime:date>/", mail.resultsstored_historic),
re_path(rf"^clear/{regex_dname}/$", views.clear),
path("change_language/", views.change_language, name="change_language"),
path("contact/", views.indexpage),
Expand Down
9 changes: 8 additions & 1 deletion interface/views/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import DisallowedRedirect
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import render
from django.utils.translation import gettext as _

Expand Down Expand Up @@ -34,6 +34,7 @@
redirect_invalid_domain,
update_report_with_registrar_and_score,
SafeHttpResponseRedirect,
latest_report,
)

# Entrance after form submission.
Expand Down Expand Up @@ -188,6 +189,12 @@ def resultscurrent(request, dname):
return HttpResponseRedirect(f"/site/{addr}/{report.id}/")


def resultsstored_historic(request, domain_name, date):
if report_id := latest_report(DomainTestReport, domain_name, date):
return HttpResponseRedirect(f"/site/{domain_name}/{report_id}/")
return HttpResponseNotFound()


# URL: /(site|domain)/<dname>/<reportid>/
@simple_cache_page
def resultsstored(request, dname, id):
Expand Down
9 changes: 8 additions & 1 deletion interface/views/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from django.conf import settings
from django.core.cache import cache
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import render
from django.utils.translation import gettext as _

Expand All @@ -32,6 +32,7 @@
process,
redirect_invalid_domain,
update_report_with_registrar_and_score,
latest_report,
)
from internetnl import log

Expand Down Expand Up @@ -182,6 +183,12 @@ def resultscurrent(request, mailaddr):
return HttpResponseRedirect(f"/mail/{addr}/{report.id}/")


def resultsstored_historic(request, domain_name, date):
if report_id := latest_report(MailTestReport, domain_name, date):
return HttpResponseRedirect(f"/mail/{domain_name}/{report_id}/")
return HttpResponseNotFound()


# URL: /mail/<dname>/<reportid>/
def resultsstored(request, dname, id):
"""
Expand Down
24 changes: 23 additions & 1 deletion interface/views/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import random
import re
from datetime import datetime
from typing import Optional
from urllib.parse import urlparse

import dns
Expand All @@ -27,7 +28,6 @@
from interface import redis_id
from internetnl import log


# See: https://stackoverflow.com/a/53875771 for a good summary of the various
# RFCs and other rulings that combine to define what is a valid domain name.
# Of particular note are xn-- which is used for internationalized TLDs, and
Expand Down Expand Up @@ -482,3 +482,25 @@ def __init__(self, redirect_to, *args, **kwargs):

if not settings.DEBUG and not url_has_allowed_host_and_scheme(redirect_to, allowed_hosts=allowed_hosts):
raise DisallowedRedirect("Unsafe redirect to URL: %s" % redirect_to)


def latest_report(model, domain_name: str, date: datetime) -> Optional[int]:
# probably the default ordering in the database is on -id or -timestamp. Both will work.
# domain does not have an index, timestamp neither. This might be a problem, but it also might just work fine.

# for caching purposes don't allow users to request domains newer than today:
# assume users don't enter timezones, they can, and that will crash here due to non-zone-awareness :)
if date > datetime.now():
log.debug("Will not hand out reports in the future.")
date = datetime.now()

# if no time is given, the first moment of the day is assumed, at that time the report did not exist yet.
# so if people try with just a date, they will not get what they expect: the latest report for that day.
if date.hour == 0 and date.minute == 0 and date.second == 0:
log.debug("Aiding the user to set the date of the date to the latest report of the day.")
date = date.replace(hour=23, minute=59, second=59) # try to get the latest report of this day.

report = (
model.objects.all().filter(domain=domain_name, timestamp__lte=date).order_by("-timestamp").only("id").first()
)
return report.id if report else None
Loading