changeset 3669:b66fd6de093c beta

fixed multiple IP addresses in each of extracted IP. - different setup uses different proxy emthods. We make sure we always select the first IP
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 04 Apr 2013 16:38:33 +0200
parents a3c1de44991d
children c67132dc74b6
files rhodecode/lib/base.py
diffstat 1 files changed, 19 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/lib/base.py	Thu Apr 04 14:36:30 2013 +0200
+++ b/rhodecode/lib/base.py	Thu Apr 04 16:38:33 2013 +0200
@@ -32,6 +32,22 @@
 log = logging.getLogger(__name__)
 
 
+def _filter_proxy(ip):
+    """
+    HEADERS can have mutliple ips inside the left-most being the original
+    client, and each successive proxy that passed the request adding the IP
+    address where it received the request from.
+
+    :param ip:
+    """
+    if ',' in ip:
+        _ips = ip.split(',')
+        _first_ip = _ips[0].strip()
+        log.debug('Got multiple IPs %s, using %s' % (','.join(_ips), _first_ip))
+        return _first_ip
+    return ip
+
+
 def _get_ip_addr(environ):
     proxy_key = 'HTTP_X_REAL_IP'
     proxy_key2 = 'HTTP_X_FORWARDED_FOR'
@@ -39,22 +55,14 @@
 
     ip = environ.get(proxy_key)
     if ip:
-        return ip
+        return _filter_proxy(ip)
 
     ip = environ.get(proxy_key2)
     if ip:
-        return ip
+        return _filter_proxy(ip)
 
     ip = environ.get(def_key, '0.0.0.0')
-
-    # HEADERS can have mutliple ips inside
-    # the left-most being the original client, and each successive proxy
-    # that passed the request adding the IP address where it received the
-    # request from.
-    if ',' in ip:
-        ip = ip.split(',')[0].strip()
-
-    return ip
+    return _filter_proxy(ip)
 
 
 def _get_access_path(environ):