N
N
Nikita Chukov2015-05-02 02:18:34
Django
Nikita Chukov, 2015-05-02 02:18:34

How to determine where the request came from lan or because of nat?

Good afternoon!
You need to make sure that the application behavior is separated for users who logged in through the local network and users who logged in through nat.
python 3, Django 1.8, apache 2.4.
virtual host configuration:

<VirtualHost *:8080>
WSGIScriptAlias / /www/pi/pi/wsgi.py
#WSGIPythonPath /www/pi/
<Directory /www/pi>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

I came up with 1 idea, to determine the request's ip:
def get_client_ip(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip

Everything works fine when the user comes from the local network, but if the user connects through a proxy, then I get the ip of his machine, not the proxy server, not his external ip.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question