Package: django-ip-shield
IP Shield is a Django app that analyzes HTTP requests and returns lock pages to IP addresses that perform suspicious actions. IP Shield is a per-IP-address rate limiter that enforces the limit with a lock out period. IP Shield also allows you to write your own analysis functions, so anything in the HttpRequest object (URL variables, POST data, HTTP headers, etc.) could trigger page locking. The functionality is influenced by the program Fail2Ban.Quick start1. Install And UninstallIt is probably easiest to just drop the source folder into your Django project as you would any other Django app. For completeness, the package installation instructions are written below.build the package:python3/path/setup.pysdistinstall the package:pip3install--user/path/django-ip-shield-0.1.6.tar.gzunistall the package:pip3uninstalldjango-ip-shield2. Modify settings.pyAdd “ipshield” to your INSTALLED_APPS setting:INSTALLED_APPS=[...'ipshield',]3. MigrateApply migrations for IP Shield by running:python3manage.pymigrate4. Edit A View FileIn a view file, import the filt_req decorator as shown below.fromipshield.viewsimportfilt_reqAdd the following variables to the file.eventName="example name"# a name for the event which is being monitoredblockTime=2# minutes that an IP will be blockedfindTime=1# number of minutes used to calculate the ratemaxAllowed=5# number of events per findTime that will trigger blockingAs shown below, add the decorator above the view function that you wish to protect.@filt_req(eventName,blockTime,findTime,maxAllowed)defview(request):# function bodyReload the page six times in one minute. The page should now be locked for five minutes, and you should see a page reading “Sorry! This page has been locked.” The page will automatically unlock after two minutes.5. Custom AnalysisYou may analyze URL variables, POST data, IP address, etc. To do this, you must write a custom analysis function which will determine exactly what IP Sheild will consider to be suspicious. This function will be passed to the decorator. It should accept an HttpRequest object (which is typically named “request” in Django’s documentation) as an input, and it should return a boolean value. An example is shown below.myFiltFunc=lambdarequest:request.GET.get('event')=='1'@filt_req(eventName,blockTime,findTime,maxAllowed,filtFunc=myFiltFunc)The above example would block all requests which had the URL GET variable named ‘event’ that held a value of ‘1’. For example, the url below would be counted as an event.a-given-url/?event=1In contrast, the following would NOT be counted as an event.a-given-url/?event=2As another example, say that we want to monitor POST requests, but not GET requests. This could be implemented with the analysis function below.myFiltFunc=lambdarequest:request.method=='POST'6. Custom View FunctionsYou may also use custom view function. This is useful if you want to return some of the request data to the client, or if you simply wish to use a particular HTML template when a particular event occurs. To do this, you need to write a view function and pass it to the decorator. An example is shown below.defview_blocked(request):msg="We're Sorry! You did something that makes us uncomfortable."html="".join(("<html><body><h1><center>",msg,"</center></h1></body></html>"))returnHttpResponse(html,status=429)@filt_req(eventName,blockTime,findTime,maxAllowed,lockPageViewFunc=view_blocked)7. CaveatsIP Shield makes the below function call.request.META.get('REMOTE_ADDR')Between Django and any upstream servers, ensure that the REMOTE_ADDR header is properly set. Often, the HTTP_X_FORWARDED_FOR header is used in place of REMOTE_ADDR.