Files
interchan/board/utils.py
Alek Ratzloff 28ccd7d73b 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>
2022-06-20 15:26:35 -07:00

27 lines
844 B
Python

from board.models import Ban, RangeBan
import ipaddress
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
def get_ip_bans(ip: str) -> list:
bans = list(Ban.objects.filter(ip=ip))
ip_addr = ipaddress.ip_address(ip)
for rangeban in RangeBan.objects.all():
start = ipaddress.ip_address(rangeban.start)
end = ipaddress.ip_address(rangeban.end)
if ip_addr.version != start.version or ip_addr.version != end.version:
continue
if start <= ip_addr <= end: # type: ignore
bans += [rangeban]
return bans