ken cochrane avatar ken cochrane committed bad081b

added VISITOR_IGNORE_IP_LIST custom setting to help fix issue with apps behind load balancers

Comments (0)

Files changed (4)

 # This is how long the cookie will live in your visitors browser before it expires on it's own
 COOKIE_MAX_AGE = 31536000 # 1 year
+# if your application is behind a load balancer, add the load balancer IP
+# address to the list so that it doesn't use that for logging the visitor
+# it will then use the x_forward_for or one of the other headers if they 
+# are present in the header
 Change Log
+0.1.5 - added VISITOR_IGNORE_IP_LIST custom setting to help with issues behind a load balancer.
 0.1.4 - Merged in changes from pydanny (cleanup and better docs)
 0.1.3 - Added missing constant "REQUEST_LOG_ID" used by other applications.
-    version='0.1.4',
+    version='0.1.5',
     description='Track visitors across your site using a cookie',
     maintainer='Ken Cochrane',


 # pull settings from settings file, if not there, use reasonable defaults
 COOKIE_DOMAIN = getattr(settings, 'COOKIE_DOMAIN', None) # local domain only 
 COOKIE_MAX_AGE = getattr(settings, 'COOKIE_MAX_AGE', 31536000) # 1 year
+VISITOR_IGNORE_IP_LIST = getattr(settings, 'VISITOR_IGNORE_IP_LIST', ('',))
 def create_uuid(*parts):
     name = '-'.join([str(counter())] + [str(time.time())] + [str(p) for p in parts])
     if 'HTTP_TRUE_CLIENT_IP' in meta:
         # Akamai's Site accelorator's proxy header for real IP
         ip_address = meta.get('HTTP_TRUE_CLIENT_IP', '')
-    elif 'REMOTE_ADDR' in meta and meta.get('REMOTE_ADDR', '') != '':
+    elif 'REMOTE_ADDR' in meta and meta.get('REMOTE_ADDR', '') not in VISITOR_IGNORE_IP_LIST:
+        # use the remote address unless it is one of the ones in the ignore list
+        # then keep looking to see if we find a better one.
         ip_address = meta.get('REMOTE_ADDR', '')
     elif 'HTTP_X_REAL_IP' in meta:
         ip_address = meta.get('HTTP_X_REAL_IP', '')
     elif 'HTTP_X_FORWARDED_FOR' in meta:
-        ip_address = meta.get('HTTP_X_FORWARDED_FOR', '')
+        forwarded_list = meta.get('HTTP_X_FORWARDED_FOR', '')
+        # forwarded for can have multiple IP's comma seperated
+        # the newest is far left, oldest appended to the right
+        # X-Forwarded-For: client1, proxy1, proxy2
+        #
+        #
+        # we want the first one since this is the client
+        ip_address = forwarded_list.split(",")[0]
         ip_address = meta.get('REMOTE_ADDR', None)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.