Initial commit of require_ipv6 Django app
Base files generated using: ```sh django-admin startapp require_ipv6 ```
This commit is contained in:
parent
50139cea19
commit
0262782844
8 changed files with 70 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
||||||
DJANGO_ALLOWED_HOSTS=
|
DJANGO_ALLOWED_HOSTS=
|
||||||
DJANGO_DEBUG=True
|
DJANGO_DEBUG=True
|
||||||
DJANGO_SECRET_KEY=example-secret-key
|
DJANGO_SECRET_KEY=example-secret-key
|
||||||
|
REQUIRE_IPV6_DISABLE=
|
||||||
|
|
0
require_ipv6/__init__.py
Normal file
0
require_ipv6/__init__.py
Normal file
10
require_ipv6/apps.py
Normal file
10
require_ipv6/apps.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
"""
|
||||||
|
Configuration of the require_ipv6 Django app
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class RequireIpv6Config(AppConfig):
|
||||||
|
name = "require_ipv6"
|
||||||
|
verbose_name = "Require IPv6"
|
46
require_ipv6/middleware.py
Normal file
46
require_ipv6/middleware.py
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
"""
|
||||||
|
Middlewares of the require_ipv6 Django app
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.core.exceptions import MiddlewareNotUsed, SuspiciousOperation
|
||||||
|
from django.shortcuts import render
|
||||||
|
from ipware import get_client_ip
|
||||||
|
import ipaddress
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class RequireIPv6Middleware:
|
||||||
|
"""
|
||||||
|
A middleware that responds with an error if request is not done via IPv6
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, get_response):
|
||||||
|
# Requirement on IPv6 can be disabled by explicitly using a `True` value
|
||||||
|
# in setting `REQUIRE_IPV6_DISABLE`
|
||||||
|
should_disable = getattr(settings, "REQUIRE_IPV6_DISABLE", False) is True
|
||||||
|
|
||||||
|
if should_disable:
|
||||||
|
logger.warning("RequireIPv6 Middleware is disabled per settings.")
|
||||||
|
raise MiddlewareNotUsed
|
||||||
|
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
client_ip_address_as_string = get_client_ip(request)[0]
|
||||||
|
|
||||||
|
try:
|
||||||
|
client_ip_address = ipaddress.ip_address(client_ip_address_as_string)
|
||||||
|
except ValueError:
|
||||||
|
raise SuspiciousOperation(
|
||||||
|
"Invalid client IP address: %s" % client_ip_address_as_string
|
||||||
|
)
|
||||||
|
|
||||||
|
if not isinstance(client_ip_address, ipaddress.IPv6Address):
|
||||||
|
# Client does not use IPv6, render appropriate error page
|
||||||
|
return render(request, "require_ipv6/error.html", status=400)
|
||||||
|
|
||||||
|
# Client uses IPv6, continue processing request
|
||||||
|
return self.get_response(request)
|
6
require_ipv6/templates/require_ipv6/error.html
Normal file
6
require_ipv6/templates/require_ipv6/error.html
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<body>
|
||||||
|
<em>Sorry, only reachable by IPv6</em>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
asgiref==3.2.7 # via django
|
asgiref==3.2.7 # via django
|
||||||
django-environ==0.4.5 # via ungleich_screening_task (setup.py)
|
django-environ==0.4.5 # via ungleich_screening_task (setup.py)
|
||||||
|
django-ipware==2.1.0 # via ungleich_screening_task (setup.py)
|
||||||
django==3.0.6 # via ungleich_screening_task (setup.py)
|
django==3.0.6 # via ungleich_screening_task (setup.py)
|
||||||
pytz==2020.1 # via django
|
pytz==2020.1 # via django
|
||||||
sqlparse==0.3.1 # via django
|
sqlparse==0.3.1 # via django
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -3,6 +3,7 @@ from setuptools import setup
|
||||||
requirements = [
|
requirements = [
|
||||||
"django",
|
"django",
|
||||||
"django-environ",
|
"django-environ",
|
||||||
|
"django-ipware",
|
||||||
]
|
]
|
||||||
|
|
||||||
dev_requirements = [
|
dev_requirements = [
|
||||||
|
|
|
@ -49,6 +49,7 @@ INSTALLED_APPS = [
|
||||||
"django.contrib.messages",
|
"django.contrib.messages",
|
||||||
"django.contrib.staticfiles",
|
"django.contrib.staticfiles",
|
||||||
"friendly_cat",
|
"friendly_cat",
|
||||||
|
"require_ipv6",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
@ -56,6 +57,7 @@ MIDDLEWARE = [
|
||||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||||
"django.middleware.common.CommonMiddleware",
|
"django.middleware.common.CommonMiddleware",
|
||||||
"django.middleware.csrf.CsrfViewMiddleware",
|
"django.middleware.csrf.CsrfViewMiddleware",
|
||||||
|
"require_ipv6.middleware.RequireIPv6Middleware",
|
||||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||||
"django.contrib.messages.middleware.MessageMiddleware",
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
|
@ -124,3 +126,6 @@ USE_TZ = True
|
||||||
# https://docs.djangoproject.com/en/3.0/howto/static-files/
|
# https://docs.djangoproject.com/en/3.0/howto/static-files/
|
||||||
|
|
||||||
STATIC_URL = "/static/"
|
STATIC_URL = "/static/"
|
||||||
|
|
||||||
|
# Allow settings of the “Require IPv6” application to be set as environment variables
|
||||||
|
REQUIRE_IPV6_DISABLE = env.bool("REQUIRE_IPV6_DISABLE", False)
|
||||||
|
|
Loading…
Reference in a new issue