@@ -23,13 +23,13 @@ def long2ip(num):
2323 "convert long int to dotted quad string"
2424 iplist = []
2525 for n in range (4 ):
26- num ,mod = divmod (num ,256 )
27- iplist .insert (0 ,str (mod ))
26+ num , mod = divmod (num , 256 )
27+ iplist .insert (0 , str (mod ))
2828 return '.' .join (iplist )
2929
3030
3131class IPPool (object ):
32- def __init__ (self , ipfile , recordfile , monitor_mapping ):
32+ def __init__ (self , ipfile , recordfile ):
3333 if not isfile (ipfile ):
3434 logger .warning ("can't find ip data file: %s" % ipfile )
3535 # 故意返回数据,另程序退出
@@ -40,7 +40,6 @@ def __init__(self, ipfile, recordfile, monitor_mapping):
4040 logger .warning ("can't find A record file: %s" % recordfile )
4141 return 2
4242 self .recordfile = recordfile
43- self .monitor_mapping = monitor_mapping
4443
4544 # 初始化iplist,用来进行2分查找
4645 self .iplist = []
@@ -170,7 +169,8 @@ def JoinIP(self, ip):
170169
171170 def ListIP (self ):
172171 for key in self .iphash :
173- print ("ipstart: %s ipend: %s country: %s province: %s city: %s sp: %s" % (key , self .iphash [key ][1 ], self .iphash [key ][2 ], self .iphash [key ][3 ], self .iphash [key ][4 ], self .iphash [key ][5 ]))
172+ print ("ipstart: %s ipend: %s country: %s province: %s city: %s sp: %s" % (
173+ key , self .iphash [key ][1 ], self .iphash [key ][2 ], self .iphash [key ][3 ], self .iphash [key ][4 ], self .iphash [key ][5 ]))
174174 for i in self .iphash [key ][6 ]:
175175 print ("[domain:%s ip: %s]" % (i , self .iphash [key ][6 ][i ][0 ]))
176176
@@ -197,17 +197,35 @@ def FindIP(self, ip, name):
197197 sp = self .iphash [i ][5 ]
198198 if ipstart <= ipnum <= ipend :
199199 ip_list = [
200- tmp_ip for tmp_ip in re .split (r',|\s+' , self .iphash [i ][6 ][name ][0 ]) \
201- if not re .search (r'[^0-9.]' , tmp_ip ) and self . monitor_mapping . check ( name , tmp_ip )]
200+ tmp_ip for tmp_ip in re .split (r',|\s+' , self .iphash [i ][6 ][name ][0 ])
201+ if not re .search (r'[^0-9.]' , tmp_ip )]
202202 logger .info ("userip:[%s] domain:[%s] section:[%s-%s] location:[%s,%s,%s,%s] ip_list:%s" % (
203203 ip , name , long2ip (ipstart ), long2ip (ipend ), country , province , city , sp , ip_list ))
204204 if not ip_list or len (ip_list ) == 0 :
205205 # maybe something wrong
206- tmp_ip_list = [
207- tmp_ip for tmp_ip in re .split (r',|\s+' , self .record [name ]['default' ]) \
208- if not re .search (r'[^0-9.]' , tmp_ip )]
209- ip_list = [tmp_ip for tmp_ip in tmp_ip_list if self .monitor_mapping .check (name , tmp_ip )]
210- if len (ip_list ) == 0 :
211- logger .warning ("no available ip for %s, use default ip" % name )
212- return tmp_ip_list
206+ ip_list = [
207+ tmp_ip for tmp_ip in re .split (r',|\s+' , self .record [name ]['default' ])
208+ if not re .search (r'[^0-9.]' , tmp_ip )]
209+ return ip_list
210+
211+
212+ class CachedIPPool (object ):
213+
214+ def __init__ (self , ipfile , recordfile , monitor_mapping ):
215+ self .caches = {}
216+ self .monitor_mapping = monitor_mapping
217+ self .finder = IPPool (ipfile , recordfile )
218+
219+ def FindIP (self , ip , name ):
220+ key = "%s-%s" % (ip , name )
221+ if key in self .caches :
222+ tmp_ip_list = self .caches [key ]
223+ else :
224+ tmp_ip_list = self .finder .FindIP (ip , name )
225+ self .caches [key ] = tmp_ip_list
226+ ip_list = [
227+ tmp_ip for tmp_ip in tmp_ip_list if self .monitor_mapping .check (name , tmp_ip )]
228+ if len (ip_list ) == 0 :
229+ logger .warning ("no available ip for %s, use default ip" % name )
230+ return tmp_ip_list
213231 return ip_list
0 commit comments