Commits

imbolc  committed 1f5a4fc

add rq.ip_list

  • Participants
  • Parent commits dbd219f

Comments (0)

Files changed (2)

File pysi/wsgi.py

     def url(self):
         '''Полный урл'''
         return '%s://%s%s' % (self.scheme, self.host, self.full_path)
-            
+    
     @cache_property
     def ip(self):
-        ret = self.environ.get('HTTP_X_FORWARDED_FOR', self.environ.get(
-            'HTTP_X_REAL_IP', self.environ.get('REMOTE_ADDR')))
-        return ret.split(',')[0].strip() if ret and ',' in ret else ret
+        return self.environ.get('REMOTE_ADDR')
+
+    @cache_property
+    def ip_list(self):
+        '''
+        Список ипов из заголовков проксей за исключением локальных
+        '''
+        ret = []
+        ips = self.environ.get('HTTP_X_FORWARDED_FOR', self.environ.get(
+            'HTTP_X_REAL_IP'))
+        if ips:
+            for ip in ips.split(','):
+                ip = ip.strip()
+                if ip.startswith(('10.', '192.168.')):
+                    continue
+                if ip.startswith('172.'):
+                    try:
+                        x = ip.split('.')[1]
+                    except (ValueError, IndexError):
+                        continue
+                    if 15 < x < 31:
+                        continue
+                ret.append(ip)
+        if self.ip and self.ip not in ret:
+            ret.append(self.ip)
+        return ret
 
     @cache_property
     def referer(self):
 from setuptools import setup
 
 PACKAGE = 'pysi'
-VERSION = '0.2.2'
+VERSION = '0.2.3'
 
 if __name__ == '__main__':
     # Compile the list of packages available, because distutils doesn't have