def analysise_ip_data_from_es(self,url,index,path,ip_viste_total = 5000,rangeMinute=5,sort_type = True,lte = datetime.datetime.now().strftime('%Y.%m.%d %H:%M:%S')): print(url) logging.info(" analysise ip address start ") logging.info(" ip limit value: %s"%ip_viste_total ) elasticsearch_service = elasticsearchLib(url) forbid_ip_address = [] all_ip = elasticsearch_service.getDataByIndex(index,path,rangeMinute,sort_type,lte) if all_ip != "notfound": suspension_ip_model = SuspensionIpModel() forbid_ip_address = [] for i in all_ip: if i[1] > ip_viste_total: logging.info(" forbid ip addrss %s"%i[0]) forbid_ip_address.append(i[0]) for i in forbid_ip_address: i = str(i) ip_exist = suspension_ip_model.check_ip_is_in_db(i) if ip_exist: logging.info(" update exist ip address ") suspension_ip_model.update_ip_status(ip_address =i,Status= 0) else: ip_data = {} ip_data['Status'] = 0; ip_data['IpAddress'] = i ip_data['SuspensionTime'] = str(datetime.datetime.now().strftime('%Y.%m.%d %H:%M:%S')) logging.info(" add ip address to redis") suspension_ip_model.add_ip_to_db(ip_data) self.add_violation_to_redis(self.hash_key,i) logging.info(" analysise ip address end ")
def unblockIp(self): logging.info("start unblock ip ") suspension_ip_model = SuspensionIpModel() all_suspension_ip = suspension_ip_model.get_suspensionIp(0) ip_analysise_service = IPAnalysiseService(self.redis_config) currnet_time = self._fromatTime(time.time()) for i in all_suspension_ip: logging.info("unblock ip address %s"%i['IpAddress']) ip_SuspensionTime = self._fromatTime(time.mktime(time.strptime(i['SuspensionTime'],'%Y.%m.%d %H:%M:%S'))) if currnet_time -ip_SuspensionTime > self.clock_ip_time*60000*60: suspension_ip_model.update_ip_status(ip_address=i['IpAddress'],Status= 1) ip_analysise_service.rem_violation_ip_from_redis(self.suspension_key,i['IpAddress']) else: continue logging.info("end unblock ip ")