Welcome to django-request-filters
django-request-filters is a package developed for django to detect and block the users those are using VPN, Proxy, TOR or Relay.
This package uses vpnapi.io API to detect the status of the user's IP address.
Installation
Use pip to install from PyPI:
pip install django-request-filters
Documentation
Settings:
-
Visit vpnapi.io and create an account and obtain your API Key.
-
In settings.py add the variable
VPNAPI_KEYand set it's value to your API key obtained in step 1.VPNAPI_KEY = 'Your vpnapi API key'
-
Add a view that 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.py from django.shortcuts import render from request_filters.decorators.ip_check import exempt_IPCheckMiddleware @exempt_IPCheckMiddleware def blockView(request): return render(request, 'block_view.html')
As we must need to display the view function to blocked users, so the
exempt_IPCheckMiddlewaremust be used in that view function. -
Add the path of the view function in settings.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." with status code 418.
-
-
Add additional settings in settings.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):
Use MIDDLEWARE to 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).
- Add MIDDLEWARE:
In settings.py add
'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 the middleware will 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 the
exempt_IPCheckMiddlewaredecorator on the view function.from request_filters.decorators.ip_check import exempt_IPCheckMiddleware @exempt_IPCheckMiddleware def home(request): ......... .........
Here, for the above code sample, all the users (including anonymous users) can visit the home 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.
Contributing
To contribute to django-request-filters create a fork on GitHub. Clone your fork, make some changes, and submit a pull request.
Issues
Use the GitHub issue tracker for django-request-filters to submit bugs, issues, and feature requests.
Metadata
Release files for django-request-filters 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django_request_filters-0.0.4.tar.gz | 113.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_request_filters-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 120.6 kB
Release files / django_request_filters-0.0.4.tar.gz
| Download URL | django_request_filters-0.0.4.tar.gz |
|---|---|
| Size | 113.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
af19363d97897cd7b623df309ac26f360ec061e8ea145cf9794e1a92085e5e12
|
|
BLAKE2b-256 checksum How to use checksums |
88582b1169abd376a19f55cc371a14a51c20adb4ce2cb78c0a22bbc73a96c95b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.0
|
Release files / django_request_filters-0.0.4-py3-none-any.whl
| Download URL | django_request_filters-0.0.4-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1d0aaa1f2c7965ee60052f40deab7d7e12e2c6fb4f6d24bedb99b8ac748d7d3a
|
|
BLAKE2b-256 checksum How to use checksums |
a1c48f1eb969ec3b11ee88b510a639fb97e168e9bf924e73b2a802d1eb7b2132
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.0
|
Security for django-request-filters 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.