Add IP bans

Users can be banned by IP address now, either by singular IP or in an IP
range. If they are banned and attempt to post, they will be met with a
"you are banned until X date" screen.

There are a few loose threads with this, and IP bans may be obsolete if
I decide to go the accounts-required-for-posting route. But I think this
is a good start for 4chan style posting.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-06-20 15:26:35 -07:00
parent b4df8b9756
commit 28ccd7d73b
7 changed files with 190 additions and 30 deletions

View File

@@ -1,23 +1,27 @@
from django.conf import settings
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.shortcuts import get_object_or_404
from django.views.generic import DetailView
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView
from django.urls import reverse, reverse_lazy
from board.models import Post, Board, Report, ReportRecord
from board.forms import PostForm, ReplyForm, ReportForm
from board.models import Ban, Post, Board, Report
from board.utils import get_client_ip, get_ip_bans
__all__ = ("BoardView", "PostView", "ReportView")
__all__ = ("BannedView", "BoardView", "PostView", "ReportView")
def get_client_ip(request):
"Get the IP address of a client-side request. Shamelessly copy/pasted from StackOverflow."
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
if x_forwarded_for:
ip = x_forwarded_for.split(",")[0]
else:
ip = request.META.get("REMOTE_ADDR")
return ip
class BannedView(TemplateView):
template_name = "board/banned.html"
def get_context_data(self, **kwargs):
context = super(TemplateView, self).get_context_data(**kwargs)
ip = get_client_ip(self.request)
bans = get_ip_bans(ip)
context["bans"] = bans
context["ip"] = ip
return context
class CreatePostView(CreateView):
@@ -46,6 +50,20 @@ class CreatePostView(CreateView):
kwargs["ip"] = get_client_ip(self.request)
return kwargs
def dispatch(self, request, *args, **kwargs):
self._set_board(kwargs["url"])
if request.method == "POST":
ip = get_client_ip(request)
bans = [
ban
for ban in get_ip_bans(ip)
if ban.board == self.board or not ban.board
]
if bans:
return HttpResponseRedirect(reverse("board:banned"))
return super(CreatePostView, self).dispatch(request, *args, **kwargs)
class BoardView(CreatePostView):
model = Post