A Django app that implements IP-based blocklisting.
Project description
Django-blocklist
This is a Django app that implements IP-based blocklisting. Its BlocklistMiddleware performs the blocking, and its clean_blocklist management command deletes entries which have satisfied the cooldown period. Entries also have a reason field, used in reporting. There are utility functions to add/remove IPs, an admin, and several management commands.
This app is primarily for situations where server-level blocking is not available, e.g. on platform-as-a-service hosts like PythonAnywhere or Heroku. Being an application-layer solution, it's not as performant as blocking via firewall or web server process, but is suitable for moderate traffic sites. It also offers better integration with the application stack, for easier management.
Quick start
-
The PyPI package name is
django-blocklist; add that to yourrequirements.txtor otherwise install it into your project's Python environment. -
Add "django_blocklist" to your INSTALLED_APPS setting like this:
INSTALLED_APPS = [ ... "django_blocklist" ] -
Add the middleware like this:
MIDDLEWARE = [ ... "django_blocklist.middleware.BlocklistMiddleware" ] -
Run
python manage.py migrateto create thedjango_blocklist_blockediptable. -
Add IPs to the list (via management commands,
utils.add_to_blocklist, or the admin). -
Set up a
cronjob or equivalent to runmanage.py clean_blocklistdaily.
Management commands
Django-blocklist includes several management commands:
add_to_blocklist— (one or more IPs)remove_from_blocklist— (one or more IPs)search_blocklist— look for an IP in the list; in addition to info on stdout, returns an exit code of 0 if successfulupdate_blocklist— change thereasonorcooldownvalues for existing entriesimport_blocklist— convenience command for importing IPs from a filereport_blocklist— information on the current entriesclean_blocklist— remove entries that have fulfilled their cooldown period
The --help for each of these details its available options.
For exporting or importing BlockedIP entries, use Django's built-in dumpdata and loaddata management commands.
Configuration
You can customize the following settings via a BLOCKLIST_CONFIG dict in your project settings:
cooldown— Days to expire, for new entries; default 7cache-ttl— Seconds that utils functions cache the full list; default 60denial-template— For the denial response; an f-string with{ip}and{cooldown}placeholders
Reporting
The report_blocklist command gives information about the current collection of IPs, including:
- Number of listed IPs
- Total number of blocked requests from listed IPs
- Number of IPs active in last 24 hours
- Number of stale IPs (added over 24h ago and not seen since)
- Five IPs with highest block count
- Five IPs most recently blocked
- Longest running entry
- IP counts by reason
Utility methods
The utils module defines two convenience functions for updating the list from your application code:
add_to_blocklist(ips: set, reason="")adds IPs to the blocklistremove_from_blocklist(ip: str)removes an entry, returningTrueif successful
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django-blocklist-1.2.2.tar.gz.
File metadata
- Download URL: django-blocklist-1.2.2.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.10.4 Linux/5.15.0-41-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e79fc2e437ac8a9bf3d6c8fa54f88b195651502e7ceef70fcb35bce2eecc0062
|
|
| MD5 |
0e44fbb2a0a8997eb55605a1acb30bdd
|
|
| BLAKE2b-256 |
4a1ac74ce00aecfc2e0519e3803ca2c4214e855684018d898794c726c5fcecc6
|
File details
Details for the file django_blocklist-1.2.2-py3-none-any.whl.
File metadata
- Download URL: django_blocklist-1.2.2-py3-none-any.whl
- Upload date:
- Size: 19.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.14 CPython/3.10.4 Linux/5.15.0-41-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
93ba6fec3885fd8763aff867fc056c253a3bfe9da33a6a0e87f12b381c4d052f
|
|
| MD5 |
dba9458f63904062311a9c4af1001957
|
|
| BLAKE2b-256 |
25d6eb721913798acc93739b00d8b1f54f81456815d3357297a33ec7077d5369
|