├── README.rst ├── setup.py └── x_forwarded_for ├── __init__.py └── middleware.py /README.rst: -------------------------------------------------------------------------------- 1 | =============================== 2 | django-xforwardedfor-middleware 3 | =============================== 4 | 5 | Simple Django middleware to use the ``X-Forwarded-For`` header as the request IP. 6 | 7 | Installation 8 | ------------ 9 | 10 | ``pip install django-xforwardedfor-middleware==2.0`` 11 | 12 | django-xforwardedfor-middleware version 2.0 supports django 1.10 and newer. 13 | You can install version 1.1 if you're still using older django. 14 | 15 | 16 | Usage 17 | ----- 18 | 19 | Just add ``x_forwarded_for.middleware.XForwardedForMiddleware`` to your middleware. 20 | 21 | Warning 22 | ------- 23 | 24 | This middleware is intended to get the user ip behind a trusted reverse proxy 25 | using the ``X-Forwarded-For`` header, which is set by the proxy. 26 | If your app does not run behind such an proxy, the middleware is a security problem, 27 | as clients can spoof the header. 28 | 29 | For more information see the Django changelog: 30 | https://docs.djangoproject.com/en/dev/releases/1.1/#removed-setremoteaddrfromforwardedfor-middleware 31 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from setuptools import setup 3 | 4 | 5 | setup(name='django-xforwardedfor-middleware', 6 | description='Django X-Forwarded-For Middleware', 7 | long_description='Use the X-Forwarded-For header to get the real ip of a request', 8 | author='Alexander Schier', 9 | author_email='allo@laxu.de', 10 | url='https://github.com/allo-/django-xforwardedfor-middleware', 11 | version='2.0', 12 | packages=['x_forwarded_for'], 13 | classifiers=[ 14 | 'Framework :: Django', 15 | 'License :: Public Domain', 16 | 'Operating System :: OS Independent', 17 | 'Programming Language :: Python' 18 | ], 19 | install_requires=['django>=1.10'] 20 | ) 21 | 22 | -------------------------------------------------------------------------------- /x_forwarded_for/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/allo-/django-xforwardedfor-middleware/18706fb51dd9e1f04636b97e10e59cb4da324af5/x_forwarded_for/__init__.py -------------------------------------------------------------------------------- /x_forwarded_for/middleware.py: -------------------------------------------------------------------------------- 1 | from django.utils.deprecation import MiddlewareMixin 2 | 3 | class XForwardedForMiddleware(MiddlewareMixin): 4 | def process_request(self, request): 5 | if 'HTTP_X_FORWARDED_FOR' in request.META: 6 | request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR'].split(",")[0].strip() 7 | return None 8 | --------------------------------------------------------------------------------