Changeset 8689


Ignore:
Timestamp:
21/11/12 11:47:21 (7 years ago)
Author:
mnagni
Message:

corrected how the remote_ip is retrieved from the remote_url

Location:
mauRepo/dj_security/trunk/dj_security
Files:
3 added
1 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/dj_security/trunk/dj_security/middleware.py

    r8688 r8689  
    3333from paste.auth.auth_tkt import AuthTicket 
    3434from django.conf import settings 
     35import socket 
     36import urlparse  
    3537 
    3638import logging 
     
    5456            return response 
    5557                 
    56         remote_ip = _calculate_remote_ip(request) 
     58        remote_ip = _calculate_remote_ip(request.GET.get('r')) 
    5759        LOGGER.debug("responding to remote_ip: %s" % (remote_ip))       
    5860        username = request.POST.get('username') 
     
    6971        return response 
    7072     
    71 def _calculate_remote_ip(request): 
    72     import socket 
    73     import urlparse     
    74     remote_url = urlparse.urlparse(request.GET.get('r', '')) 
    75     LOGGER.debug("remote_url: %s" % (str(remote_url))) 
    76     if remote_url.netloc != '': 
    77         LOGGER.debug("gethostbyname_ex: %s" % (str(socket.gethostbyname_ex(remote_url.netloc.split(':')[0])))) 
    78         ips = socket.gethostbyname_ex(remote_url.netloc.split(':')[0])[2] 
    79         if len(ips) > 1: 
    80             return ips[1]             
    81     return '127.0.0.1' 
     73def _calculate_remote_ip(url_path):    
     74    remote_url = urlparse.urlparse(url_path) 
     75    LOGGER.debug("calculating remote_ip for %s" % (str(remote_url))) 
     76    port = 80 
     77    host = remote_url.netloc 
     78    if remote_url.netloc: 
     79        if ':' in remote_url.netloc: 
     80            host, port = remote_url.netloc.split(':') 
     81        addrinfo = socket.getaddrinfo(host, int(port)) 
     82        LOGGER.debug("%s has remote_ip %s" % (addrinfo[0][-1][0]))                                  
     83        return addrinfo[0][-1][0] 
     84    return None 
Note: See TracChangeset for help on using the changeset viewer.