Package: django-request-filters
Welcome to django-request-filtersdjango-request-filtersis a package developed for django to detect and block the users those are using VPN, Proxy, TOR or Relay.This package usesvpnapi.ioAPI to detect the status of the user's IP address.InstallationUse pip to install from PyPI:pip install django-request-filtersDocumentationSettings:Visitvpnapi.ioand create an account and obtain your API Key.Insettings.pyadd the variableVPNAPI_KEYand set it's value to your API key obtained in step 1.VPNAPI_KEY='Your vpnapi API key'Add aviewthat will display to the users when they have blocked (optional setup):
If you want to show your designed page to blocked users, you can follow this step.Define a vew function:# For example we defined a view function in views.pyfromdjango.shortcutsimportrenderfromrequest_filters.decorators.ip_checkimportexempt_IPCheckMiddleware@exempt_IPCheckMiddlewaredefblockView(request):returnrender(request,'block_view.html')As we must need to display the view function to blocked users, so theexempt_IPCheckMiddlewaremust be used in that view function.Add the path of the view function insettings.py:IP_BLOCK_VIEW="app_name.views.blockView"If you do not defined any view function for blocked users, then by default it will show a simple HTML page contains a text "We can't allow your request, because you are using VPN or Proxy or Tor or Relay." withstatus code418.Add additional settings insettings.py(optional):BLOCK_VPN (optional default: True)If set toTrueall the users want accessing the site with VPN will be disallowed.
If set toFalse, users can access the site using VPN.BLOCK_PROXY (optional default: True)If set toTrueall the users want accessing the site with Proxy will be disallowed.
If set toFalse, users can access the site using Proxy.BLOCK_TOR (optional default: True)If set toTrueall the users want accessing the site with TOR will be disallowed.
If set toFalse, users can access the site using TOR.BLOCK_RELAY (optional default: True)If set toTrueall the users want accessing the site with Relay will be disallowed.
If set toFalse, users can access the site using Relay.Using Middleware (Use case 1):UseMIDDLEWAREto block the anonymous users (i.e. those are using VPN, Proxy, TOR or Relay) to access all the view function (i.e. all the requests).AddMIDDLEWARE:
Insettings.pyadd'request_filters.middlewares.ip_check.IPCheckMiddleware'into MIDDLEWARE.MIDDLEWARE=['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware',..........................................'request_filters.middlewares.ip_check.IPCheckMiddleware'# We have added this new middleware]Exempt from some view functions
Adding themiddlewarewill block the anonymous users, so that those users can visit any views.
If you want to allow anonymous users to visit only some specific views, you can do that by using theexempt_IPCheckMiddlewaredecorator on the view function.fromrequest_filters.decorators.ip_checkimportexempt_IPCheckMiddleware@exempt_IPCheckMiddlewaredefhome(request):..................Here, for the above code sample, all the users (including anonymous users) can visit thehome view.You can use this method, when you want to block the anonymous users for almost all the view functions.Using only Decorator (Use case 2):You can also controll uses' request using decorator and without using Middleware.prevent_anonymous_ipImport this decorator by -from request_filters.decorators.ip_check import prevent_anonymous_ipIf you add this decorator to a view function, this view function will not acessable to anonymous users.
This decorator has some optional parameter -block_vpn (optional default: settings.BLOCK_VPN)If set toTrueVPN users can't visit the view function.
If set toFalse, VPN users can visit the view.block_proxy (optional default: settings.BLOCK_PROXY)If set toTrueProxy users can't visit the view function.
If set toFalse, Proxy users can visit the view.block_tor (optional default: settings.BLOCK_TOR)If set toTrueTOR users can't visit the view function.
If set toFalse, TOR users can visit the view.block_relay (optional default: settings.BLOCK_RELAY)If set toTrueRelay users can't visit the view function.
If set toFalse, Relay users can visit the view.You can use this method, when you want to block the anonymous users only for few view functions.ContributingTo contribute to django-request-filterscreate a forkon GitHub. Clone your fork, make some changes, and submit a pull request.IssuesUse the GitHubissue trackerfor django-request-filters to submit bugs, issues, and feature requests.