1717import sys
1818import struct
1919import socket
20+ import ipaddress
2021
2122if sys .version < '3' :
2223 def u (x ):
@@ -50,7 +51,7 @@ def inet_pton(t, addr):
5051 return out_addr_p .raw
5152 socket .inet_pton = inet_pton
5253
53- _VERSION = '2.2 .0'
54+ _VERSION = '3.0 .0'
5455_NO_IP = 'MISSING IP ADDRESS'
5556_FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
5657_INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
@@ -71,23 +72,25 @@ class IP2ProxyRecord:
7172 asn = _FIELD_NOT_SUPPORTED
7273 last_seen = _FIELD_NOT_SUPPORTED
7374 domain = _FIELD_NOT_SUPPORTED
75+ threat = _FIELD_NOT_SUPPORTED
7476
7577 def __str__ (self ):
7678 return str (self .__dict__ )
7779
7880 def __repr__ (self ):
7981 return repr (self .__dict__ )
8082
81- _COUNTRY_POSITION = (0 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 3 )
82- _REGION_POSITION = (0 , 0 , 0 , 4 , 4 , 4 , 4 , 4 , 4 )
83- _CITY_POSITION = (0 , 0 , 0 , 5 , 5 , 5 , 5 , 5 , 5 )
84- _ISP_POSITION = (0 , 0 , 0 , 0 , 6 , 6 , 6 , 6 , 6 )
85- _PROXYTYPE_POSITION = (0 , 0 , 2 , 2 , 2 , 2 , 2 , 2 , 2 )
86- _DOMAIN_POSITION = (0 , 0 , 0 , 0 , 0 , 7 , 7 , 7 , 7 )
87- _USAGETYPE_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 8 , 8 , 8 )
88- _ASN_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 0 , 9 , 9 )
89- _AS_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 0 , 10 , 10 )
90- _LASTSEEN_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 11 )
83+ _COUNTRY_POSITION = (0 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 )
84+ _REGION_POSITION = (0 , 0 , 0 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 )
85+ _CITY_POSITION = (0 , 0 , 0 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 )
86+ _ISP_POSITION = (0 , 0 , 0 , 0 , 6 , 6 , 6 , 6 , 6 , 6 , 6 )
87+ _PROXYTYPE_POSITION = (0 , 0 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 )
88+ _DOMAIN_POSITION = (0 , 0 , 0 , 0 , 0 , 7 , 7 , 7 , 7 , 7 , 7 )
89+ _USAGETYPE_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 8 , 8 , 8 , 8 , 8 )
90+ _ASN_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 0 , 9 , 9 , 9 , 9 )
91+ _AS_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 0 , 10 , 10 , 10 , 10 )
92+ _LASTSEEN_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 11 , 11 , 11 )
93+ _THREAT_POSITION = (0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 12 , 12 )
9194
9295class IP2Proxy (object ):
9396 ''' IP2Proxy database '''
@@ -196,10 +199,17 @@ def is_proxy(self, ip):
196199 ''' Determine whether is a proxy '''
197200 try :
198201 rec = self ._get_record (ip )
199- if self ._dbtype == 1 :
200- is_proxy = 0 if (rec .country_short == '-' ) else ( 2 if ((rec .proxy_type == 'DCH' ) | (rec .proxy_type == 'SES' )) else 1 )
202+ # if self._dbtype == 1:
203+ # is_proxy = 0 if (rec.country_short == '-') else ( 2 if ((rec.proxy_type == 'DCH') | (rec.proxy_type == 'SES')) else 1)
204+ # else:
205+ # is_proxy = 0 if (rec.proxy_type == '-') else ( 2 if ((rec.proxy_type == 'DCH') | (rec.proxy_type == 'SES')) else 1)
206+ if rec .country_short != _INVALID_IP_ADDRESS :
207+ if self ._dbtype == 1 :
208+ is_proxy = 0 if (rec .country_short == '-' ) else ( 2 if ((rec .proxy_type == 'DCH' ) | (rec .proxy_type == 'SES' )) else 1 )
209+ else :
210+ is_proxy = 0 if (rec .proxy_type == '-' ) else ( 2 if ((rec .proxy_type == 'DCH' ) | (rec .proxy_type == 'SES' )) else 1 )
201211 else :
202- is_proxy = 0 if ( rec . proxy_type == '-' ) else ( 2 if (( rec . proxy_type == 'DCH' ) | ( rec . proxy_type == 'SES' )) else 1 )
212+ is_proxy = - 1
203213 except :
204214 is_proxy = - 1
205215 return is_proxy
@@ -265,10 +275,13 @@ def get_all(self, ip):
265275 as_name = rec .as_name
266276 last_seen = rec .last_seen
267277
268- if self ._dbtype == 1 :
269- is_proxy = 0 if (rec .country_short == '-' ) else ( 2 if ((rec .proxy_type == 'DCH' ) | (rec .proxy_type == 'SES' )) else 1 )
278+ if rec .country_short != _INVALID_IP_ADDRESS :
279+ if self ._dbtype == 1 :
280+ is_proxy = 0 if (rec .country_short == '-' ) else ( 2 if ((rec .proxy_type == 'DCH' ) | (rec .proxy_type == 'SES' )) else 1 )
281+ else :
282+ is_proxy = 0 if (rec .proxy_type == '-' ) else ( 2 if ((rec .proxy_type == 'DCH' ) | (rec .proxy_type == 'SES' )) else 1 )
270283 else :
271- is_proxy = 0 if ( rec . proxy_type == '-' ) else ( 2 if (( rec . proxy_type == 'DCH' ) | ( rec . proxy_type == 'SES' )) else 1 )
284+ is_proxy = - 1
272285 except :
273286 country_short = _INVALID_IP_ADDRESS
274287 country_long = _INVALID_IP_ADDRESS
@@ -410,9 +423,23 @@ def __iter__(self):
410423 yield self ._read_record (low , 6 )
411424 low += 1
412425
426+ def _validate_addr (self , addr ):
427+ ''' Validate IP address '''
428+ try :
429+ # ip = ipaddress.ip_address(addr)
430+ ip = ipaddress .ip_address (u (addr ))
431+ return True
432+ except ValueError :
433+ return False
434+
413435 def _parse_addr (self , addr ):
414436 ''' Parses address and returns IP version. Raises exception on invalid argument '''
415437 ipv = 0
438+ ipnum = - 1
439+ ipvalidateresult = self ._validate_addr (addr )
440+ # print ("IP " + str(addr) + " is " + str(ipvalidateresult) + ".")
441+ if (ipvalidateresult == False ):
442+ return ipv , ipnum
416443 try :
417444 # socket.inet_pton(socket.AF_INET6, addr)
418445 a , b = struct .unpack ('!QQ' , socket .inet_pton (socket .AF_INET6 , addr ))
@@ -496,6 +523,20 @@ def _get_record(self, ip):
496523 rec .as_name = _NO_IP
497524 rec .last_seen = _NO_IP
498525 return rec
526+ else :
527+ rec = IP2ProxyRecord ()
528+ rec .country_short = _INVALID_IP_ADDRESS
529+ rec .country_long = _INVALID_IP_ADDRESS
530+ rec .region = _INVALID_IP_ADDRESS
531+ rec .city = _INVALID_IP_ADDRESS
532+ rec .isp = _INVALID_IP_ADDRESS
533+ rec .proxy_type = _INVALID_IP_ADDRESS
534+ rec .domain = _INVALID_IP_ADDRESS
535+ rec .usage_type = _INVALID_IP_ADDRESS
536+ rec .asn = _INVALID_IP_ADDRESS
537+ rec .as_name = _INVALID_IP_ADDRESS
538+ rec .last_seen = _INVALID_IP_ADDRESS
539+ return rec
499540
500541 while low <= high :
501542 # mid = int((low + high) / 2)
0 commit comments