def run(self): self.db = DatabaseIp(self.db_file) self.__refresh_buffers(None) self._set_server_available() self.__status = 1 last_resolve_at = 0 last_refresh_at = 0 while not self.isSetStop(): now = _time() if now - last_resolve_at > self.info_duration: self.__status = 2 self.__check_all_ip() gfwlist2pac.main(self.pac_cfg) last_resolve_at = _time() # if now - last_refresh_at > self.__buff_refresh_duration: self.__status = 3 self.__refresh_buffers(None) # last_refresh_at = _time() if _time() - now < 1: _sleep(2) self.log.info('IPScanner thread end') if self.db: self.db.close()
class IPScanner(threadutil.ServeThreadBase): def __init__(self, log=None): threadutil.ServeThreadBase.__init__(self, log=log) self.__status = 0 self.all_ip = {} self.__all_goodip = {} self.__all_goodip_list = [] self.__buff_avg = [] self.__buff_history = [] self.__buff_refresh_duration = 5 self.db_file = './ip.db3' self.db = None self.info_duration = 3600 self.host_format = 'http://{}' self.dnsservers = ['8.8.8.8', '8.8.4.4'] self.site_host = 'www.google.com' self.pac_cfg = 'pac.cfg' def __check_all_ip(self): while True: try: self.allip = get_host_ip() # self.allip['local'] = get_host_ip_local(self.site_host, self.dnsservers) break except Exception as e: self.log.exception(e) break finally: if self.isSetStop(): return [] count1 = count2 = 0 for country, ips in self.allip.items(): if country in coutries_filter: for ip in ips: if self.isSetStop(): break # duration = resolve(self.host_format.format(ip)) duration = resolve(ip) print '{}, {}, {}'.format(duration, ip, country) if duration: tmp = IP(duration, ip, country, _now()) if ip in self.__all_goodip: count1 += 1 else: count2 += 1 self.__all_goodip_list.append(tmp) self.__all_goodip[ip] = tmp self.db.insert(duration, ip, country) self.log.info('update {}, new {}'.format(count1, count2)) def __refresh_buffers(self, begin): if not begin: begin = datetime.now() - timedelta(hours=24) self.__buff_avg = self.db.getAvgDurationEachIp(begin=begin) self.__buff_history = self.db.getIpRecords(begin=begin) self.log.info('update buffers: avg=%d, history=%d', len(self.__buff_avg), len(self.__buff_history)) @property def currBuff(self): return self.__all_goodip_list @property def historyBuff(self): return self.__buff_history @property def avgBuff(self): return self.__buff_avg @property def status(self): return self.__status def run(self): self.db = DatabaseIp(self.db_file) self.__refresh_buffers(None) self._set_server_available() self.__status = 1 last_resolve_at = 0 last_refresh_at = 0 while not self.isSetStop(): now = _time() if now - last_resolve_at > self.info_duration: self.__status = 2 self.__check_all_ip() gfwlist2pac.main(self.pac_cfg) last_resolve_at = _time() # if now - last_refresh_at > self.__buff_refresh_duration: self.__status = 3 self.__refresh_buffers(None) # last_refresh_at = _time() if _time() - now < 1: _sleep(2) self.log.info('IPScanner thread end') if self.db: self.db.close()