Skip to content

Commit

Permalink
Take IP from HTTP_X_REAL_IP when behind a proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
daniviga authored and nastasi-oq committed Mar 27, 2024
1 parent b3af4b7 commit d02e08d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion request/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils.deprecation import MiddlewareMixin

from . import settings
from .utils import get_ip_address
from .models import Request
from .router import Patterns
from .utils import request_is_ajax
Expand All @@ -26,7 +27,7 @@ def process_response(self, request, response):
if request_is_ajax(request) and settings.IGNORE_AJAX:
return response

if request.META.get('REMOTE_ADDR') in settings.IGNORE_IP:
if get_ip_address(request) in settings.IGNORE_IP:
return response

ignore = Patterns(False, *settings.IGNORE_USER_AGENTS)
Expand Down
4 changes: 2 additions & 2 deletions request/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from . import settings as request_settings
from .managers import RequestManager
from .utils import HTTP_STATUS_CODES, browsers, engines, request_is_ajax
from .utils import HTTP_STATUS_CODES, browsers, engines, request_is_ajax, get_ip_address

AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')

Expand Down Expand Up @@ -59,7 +59,7 @@ def from_http_request(self, request, response=None, commit=True):
self.is_ajax = request_is_ajax(request)

# User information.
self.ip = request.META.get('REMOTE_ADDR', '')
self.ip = get_ip_address(request)
self.referer = request.META.get('HTTP_REFERER', '')[:255]
self.user_agent = request.META.get('HTTP_USER_AGENT', '')[:255]
self.language = request.META.get('HTTP_ACCEPT_LANGUAGE', '')[:255]
Expand Down
4 changes: 4 additions & 0 deletions request/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,7 @@ def handle_naive_datetime(value):
if settings.USE_TZ and timezone.is_naive(value):
return timezone.make_aware(value)
return value

def get_ip_address(request):
return request.META.get(
'HTTP_X_REAL_IP', request.META.get('REMOTE_ADDR', '127.0.0.1'))

0 comments on commit d02e08d

Please sign in to comment.