Adds ip block middleware

This commit is contained in:
Mouse Reeve 2021-09-17 11:43:47 -07:00
parent 252fff8bfe
commit 16854a52bc
3 changed files with 19 additions and 0 deletions

View file

@ -1 +1,3 @@
""" look at all this nice middleware! """
from timezone_middleware import TimezoneMiddleware
from ip_middleware import IPBlocklistMiddleware

View file

@ -0,0 +1,16 @@
""" Block IP addresses """
from django.http import Http404
from bookwyrm import models
class IPBlocklistMiddleware:
"""check incoming traffic against an IP block-list"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
address = request.META.get("REMOTE_ADDR")
if models.IPBlockList.objects.filter(address=address).exists():
raise Http404()
return self.get_response(request)

View file

@ -78,6 +78,7 @@ MIDDLEWARE = [
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"bookwyrm.middleware.TimezoneMiddleware",
"bookwyrm.middleware.IPBlocklistMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]