-1

I have a blog created on django and I need to configure the access using iptables. I do not have any idea how to do it, anybody can indicate some material? I was thinking about creating a function where it is possible to pass a range of ip numbers that are allowed to access, but I don't exactly where to do that in django.

Fabian
  • 11
  • 3

1 Answers1

0

Then the best way is to create a middleware for that, it checks if the IP is in good range,

def checkIP(get_response):
   def middleware(request):
      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')
      if not ip in settings.GOOD_IPS:
           return HttpResponseForbidden("You are not allowed here")
      response = get_response(request)
      return response
return middleware 

But generally, this is NOT a recommended behavior as IPs can change and the user could try to access from another terminal

Mohamed ElKalioby
  • 1,908
  • 1
  • 12
  • 13