diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..698cf56 --- /dev/null +++ b/.gitignore @@ -0,0 +1,233 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/python,django,visualstudiocode +# Edit at https://www.toptal.com/developers/gitignore?templates=python,django,visualstudiocode + +### Django ### +*.log +*.pot +*.pyc +__pycache__/ +local_settings.py +db.sqlite3 +db.sqlite3-journal +media + +# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/ +# in your Git repository. Update and uncomment the following line accordingly. +# /staticfiles/ + +### Django.Python Stack ### +# Byte-compiled / optimized / DLL files +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo + +# Django stuff: + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +### Python ### +# Byte-compiled / optimized / DLL files + +# C extensions + +# Distribution / packaging + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. + +# Installer logs + +# Unit test / coverage reports + +# Translations + +# Django stuff: + +# Flask stuff: + +# Scrapy stuff: + +# Sphinx documentation + +# PyBuilder + +# Jupyter Notebook + +# IPython + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow + +# Celery stuff + +# SageMath parsed files + +# Environments + +# Spyder project settings + +# Rope project settings + +# mkdocs documentation + +# mypy + +# Pyre type checker + +# pytype static type analyzer + +# Cython debug symbols + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/python,django,visualstudiocode \ No newline at end of file diff --git a/README.md b/README.md index 06646b5..969fca0 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Usage 1. `pip install django-block-ip` 1. Add `block_ip` to your `INSTALLED_APPS`. 1. Add `block_ip.middleware.BlockIPMiddleware` to your `MIDDLEWARE_CLASSES`. -1. Run `syncdb`. +1. Run `python manage.py migrate`. 1. Add one or more entries to the `BlockIP` list in the admin. You can just enter a single IP or use a network mask, like this: 213.67.43.0/24 diff --git a/block_ip/middleware.py b/block_ip/middleware.py index fd73154..b01464f 100644 --- a/block_ip/middleware.py +++ b/block_ip/middleware.py @@ -1,6 +1,7 @@ from django.http import HttpResponseForbidden from django.conf import settings from django.core.cache import cache +from django.utils.deprecation import MiddlewareMixin from .models import BlockIP @@ -16,7 +17,7 @@ def is_ip_in_nets(ip, nets): return False -class BlockIPMiddleware(object): +class BlockIPMiddleware(MiddlewareMixin): def process_request(self, request): is_banned = False @@ -36,6 +37,6 @@ def process_request(self, request): if is_banned: # delete sessions when denied - for k in request.session.keys(): + for k in list(request.session.keys()): del request.session[k] return HttpResponseForbidden("") diff --git a/block_ip/migrations/0001_initial.py b/block_ip/migrations/0001_initial.py new file mode 100644 index 0000000..a7755f6 --- /dev/null +++ b/block_ip/migrations/0001_initial.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.8 on 2021-10-15 05:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='BlockIP', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('network', models.CharField(max_length=18, verbose_name='IP address or mask')), + ('reason_for_block', models.TextField(blank=True, help_text='Optional reason for block', null=True)), + ], + options={ + 'verbose_name': 'IPs & masks to ban', + 'verbose_name_plural': 'IPs & masks to ban', + }, + ), + ] diff --git a/block_ip/migrations/__init__.py b/block_ip/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/block_ip/models.py b/block_ip/models.py index 1b27d44..8e18364 100644 --- a/block_ip/models.py +++ b/block_ip/models.py @@ -5,7 +5,7 @@ from django.utils.translation import ugettext_lazy as _ from django.core.cache import cache from django.db.models.signals import post_save, post_delete -from django.utils.encoding import python_2_unicode_compatible +from six import python_2_unicode_compatible @python_2_unicode_compatible diff --git a/setup.py b/setup.py index ff69799..3e940f4 100644 --- a/setup.py +++ b/setup.py @@ -45,6 +45,7 @@ def get_package_data(package): package_data=get_package_data('.'), install_requires=[ 'ipcalc', + 'six', ], classifiers=[ 'Development Status :: 5 - Production/Stable',