Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real remote IP shown while using proxy (e.g. nginx/gunicorn configuration) #57

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

gidantribal
Copy link

#54

Added proper depencency
Accept local/non routable IPs and provide a default value in the rare
case the machine has no IP (???), as IP is mandatory in the DB.
tiberius pushed a commit to tiberius/django-request that referenced this pull request Jul 4, 2015
tiberius pushed a commit to tiberius/django-request that referenced this pull request Jul 4, 2015
This reverts commit 31aa4f3.

It has nothing to do with the ongoing pull request, sorry for that
@andhieka
Copy link

This is still open after 2 years... Is there a possibility of merging this? I can help with anything necessary. This is a good library.

@andhieka
Copy link

andhieka commented Nov 25, 2016

self.ip = get_real_ip(request) or ''

Maybe we should read the REQUEST_IP_DUMMY setting instead of using empty string (''). As pointed out in another issue #72, an empty string causes PostgreSQL to throw an error because empty string is not a valid ip address.

@lukeburden
Copy link

+1 for using ipware for reliably extracting the IP correctly across a more diverse range of deployment scenarios.

@ssuchanowski
Copy link

is there any plan of merging this?

@satadev
Copy link

satadev commented Jul 27, 2019

also this should be applied to middleware.py, else the REQUEST_IGNORE_IP param will not function.

@mrtaalebi
Copy link

mrtaalebi commented Jan 12, 2020

Why they don't merge this?

@timjonez
Copy link

timjonez commented Jan 9, 2021

Hey! I literally changed a couple lines and got this working. Is anyone actually maintaining this? This problem should be fixed 6 years ago.

@hvitis
Copy link

hvitis commented Jun 28, 2022

Wow. I just wasted a couple of hours just to get to the core of this.
Even opened a topic here:
#251

My fix for anybody who will need it:

import re
...

def is_valid_ip(ip):
    m = re.match(r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$", ip)
    return bool(m) and all(map(lambda n: 0 <= int(n) <= 255, m.groups()))

class Request(models.Model):
    ...

    def from_http_request(self, request, response=None, commit=True):
        ...
        ip_addr = [
            request.META.get('HTTP_REMOTE_ADDR', None),  
            request.META.get('HTTP_X_REAL_IP', None),
            request.META.get('HTTP_X_FORWARDED_FOR', None),
            request.META.get('REMOTE_ADDR', None),  
            request.META.get('X_REAL_IP', None),
            request.META.get('X_FORWARDED_FOR', None),
            ]
        self.ip = next((item for item in ip_addr if item is not None), None)
        if self.ip is None or self.ip is not is_valid_ip(self.ip):
            self.ip = request_settings.IP_DUMMY
            

No need to use ipware as @andhieka suggested.

Good library. Thanks

@Peskidk
Copy link

Peskidk commented Sep 14, 2022

Wow. I just wasted a couple of hours just to get to the core of this. Even opened a topic here: #251

My fix for anybody who will need it:

import re
...

def is_valid_ip(ip):
    m = re.match(r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$", ip)
    return bool(m) and all(map(lambda n: 0 <= int(n) <= 255, m.groups()))

class Request(models.Model):
    ...

    def from_http_request(self, request, response=None, commit=True):
        ...
        ip_addr = [
            request.META.get('HTTP_REMOTE_ADDR', None),  
            request.META.get('HTTP_X_REAL_IP', None),
            request.META.get('HTTP_X_FORWARDED_FOR', None),
            request.META.get('REMOTE_ADDR', None),  
            request.META.get('X_REAL_IP', None),
            request.META.get('X_FORWARDED_FOR', None),
            ]
        self.ip = next((item for item in ip_addr if item is not None), None)
        if self.ip is None or self.ip is not is_valid_ip(self.ip):
            self.ip = request_settings.IP_DUMMY
            

No need to use ipware as @andhieka suggested.

Good library. Thanks

Great work. This definetly fixed the issue. My only problem now is, that the overview is gone from admin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet