33from time import sleep
44from logging import getLogger
55from ipaddress import IPv4Address
6- from urllib .error import HTTPError
6+ from urllib .error import HTTPError , URLError
77from geoip2 .database import Reader
88from tarfile import open as taropen
99from urllib3 import disable_warnings
1818
1919
2020class GeoIPHandler (object ):
21- def __init__ (self , data_folder ):
21+ def __init__ (self , data_folder , maxmind_license_key ):
2222 self .data_folder = data_folder
23+ self .maxmind_license_key = maxmind_license_key
2324 self .dbfile = abspath (join (self .data_folder , 'GeoLite2-City.mmdb' ))
2425 self .logger = getLogger ()
2526 self .reader = None
2627 self .reader_manager (action = 'open' )
2728
28- self .logger .info ('Opening persistent connection to GeoLite2 DB...' )
29+ self .logger .info ('Opening persistent connection to the MaxMind DB...' )
2930
3031 def reader_manager (self , action = None ):
3132 if action == 'open' :
3233 try :
3334 self .reader = Reader (self .dbfile )
3435 except FileNotFoundError :
35- self .logger .error ("Could not find GeoLite2 DB! Downloading!" )
36+ self .logger .error ("Could not find MaxMind DB! Downloading!" )
3637 result_status = self .download ()
3738 if result_status :
38- self .logger .error ("Could not download GeoLite2 DB!!!, You may need to manually install it." )
39+ self .logger .error ("Could not download MaxMind DB! You may need to manually install it." )
3940 exit (1 )
4041 else :
4142 self .reader = Reader (self .dbfile )
@@ -53,54 +54,64 @@ def update(self):
5354
5455 try :
5556 dbdate = date .fromtimestamp (stat (self .dbfile ).st_mtime )
56- db_next_update = date .fromtimestamp (stat (self .dbfile ).st_mtime ) + timedelta (days = 60 )
57+ db_next_update = date .fromtimestamp (stat (self .dbfile ).st_mtime ) + timedelta (days = 30 )
5758
5859 except FileNotFoundError :
59- self .logger .error ("Could not find GeoLite2 DB as: %s" , self .dbfile )
60+ self .logger .error ("Could not find MaxMind DB as: %s" , self .dbfile )
6061 self .download ()
6162 dbdate = date .fromtimestamp (stat (self .dbfile ).st_mtime )
62- db_next_update = date .fromtimestamp (stat (self .dbfile ).st_mtime ) + timedelta (days = 60 )
63+ db_next_update = date .fromtimestamp (stat (self .dbfile ).st_mtime ) + timedelta (days = 30 )
6364
6465 if db_next_update < today :
65- self .logger .info ("Newer GeoLite2 DB available, Updating..." )
66- self .logger .debug ("GeoLite2 DB date %s, DB updates after: %s, Today: %s" ,
66+ self .logger .info ("Newer MaxMind DB available, Updating..." )
67+ self .logger .debug ("MaxMind DB date %s, DB updates after: %s, Today: %s" ,
6768 dbdate , db_next_update , today )
6869 self .reader_manager (action = 'close' )
6970 self .download ()
7071 self .reader_manager (action = 'open' )
7172 else :
7273 db_days_update = db_next_update - today
73- self .logger .debug ("Geolite2 DB will update in %s days" , abs (db_days_update .days ))
74- self .logger .debug ("GeoLite2 DB date %s, DB updates after: %s, Today: %s" ,
74+ self .logger .debug ("MaxMind DB will update in %s days" , abs (db_days_update .days ))
75+ self .logger .debug ("MaxMind DB date %s, DB updates after: %s, Today: %s" ,
7576 dbdate , db_next_update , today )
7677
7778 def download (self ):
7879 tar_dbfile = abspath (join (self .data_folder , 'GeoLite2-City.tar.gz' ))
79- url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz'
80+ maxmind_url = ('https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City'
81+ f'&suffix=tar.gz&license_key={ self .maxmind_license_key } ' )
8082 downloaded = False
8183
8284 retry_counter = 0
8385
8486 while not downloaded :
85- self .logger .info ('Downloading GeoLite2 from %s' , url )
87+ self .logger .info ('Downloading GeoLite2 DB from MaxMind...' )
8688 try :
87- urlretrieve (url , tar_dbfile )
89+ urlretrieve (maxmind_url , tar_dbfile )
8890 downloaded = True
91+ except URLError as e :
92+ self .logger .error ("Problem downloading new MaxMind DB: %s" , e )
93+ result_status = 1
94+ return result_status
8995 except HTTPError as e :
90- self .logger .error ("Problem downloading new GeoLite2 DB... Trying again. Error: %s" , e )
91- sleep (2 )
92- retry_counter = (retry_counter + 1 )
96+ if e .code == 401 :
97+ self .logger .error ("Your MaxMind license key is incorect! Check your config: %s" , e )
98+ result_status = 1
99+ return result_status
100+ else :
101+ self .logger .error ("Problem downloading new MaxMind DB... Trying again: %s" , e )
102+ sleep (2 )
103+ retry_counter = (retry_counter + 1 )
93104
94105 if retry_counter >= 3 :
95- self .logger .error ("Retried downloading the new GeoLite2 DB 3 times and failed... Aborting!" )
106+ self .logger .error ("Retried downloading the new MaxMind DB 3 times and failed... Aborting!" )
96107 result_status = 1
97108 return result_status
98109 try :
99110 remove (self .dbfile )
100111 except FileNotFoundError :
101- self .logger .warning ("Cannot remove GeoLite2 DB as it does not exist!" )
112+ self .logger .warning ("Cannot remove MaxMind DB as it does not exist!" )
102113
103- self .logger .debug ("Opening GeoLite2 tar file : %s" , tar_dbfile )
114+ self .logger .debug ("Opening MaxMind tar file : %s" , tar_dbfile )
104115
105116 tar = taropen (tar_dbfile , 'r:gz' )
106117
@@ -113,9 +124,9 @@ def download(self):
113124 tar .close ()
114125 try :
115126 remove (tar_dbfile )
116- self .logger .debug ('Removed the GeoLite2 DB TAR file.' )
127+ self .logger .debug ('Removed the MaxMind DB tar file.' )
117128 except FileNotFoundError :
118- self .logger .warning ("Cannot remove GeoLite2 DB TAR file as it does not exist!" )
129+ self .logger .warning ("Cannot remove MaxMind DB TAR file as it does not exist!" )
119130
120131
121132def hashit (string ):
0 commit comments