def read(self, callback): """Calls callback on each ip range in the file""" for start, end in self.readranges(): try: callback(IP.parse(start), IP.parse(end)) except BadIP, e: log.error("Failed to parse IP: %s", e)
def set_config(self, config): """ Sets the config based on values in 'config' :param config: config to set :type config: dictionary """ needs_blocklist_import = False for key in config.keys(): if key == 'whitelisted': saved = set(self.config[key]) update = set(config[key]) diff = saved.symmetric_difference(update) if diff: log.debug("Whitelist changed. Updating...") added = update.intersection(diff) removed = saved.intersection(diff) if added: for ip in added: try: ip = IP.parse(ip) self.blocklist.add_rule( ip.address, ip.address, ALLOW_RANGE ) saved.add(ip.address) log.debug("Added %s to whitelisted", ip) self.num_whited += 1 except BadIP, e: log.error("Bad IP: %s", e) continue if removed: needs_blocklist_import = True for ip in removed: try: ip = IP.parse(ip) saved.remove(ip.address) log.debug("Removed %s from whitelisted", ip) except BadIP, e: log.error("Bad IP: %s", e) continue self.config[key] = list(saved) continue
def on_finish_read(result): """Add any whitelisted IP's and add the blocklist to session""" # White listing happens last because the last rules added have # priority log.info("Added %d ranges to ipfilter as blocked", self.num_blocked) for ip in self.config["whitelisted"]: ip = IP.parse(ip) self.blocklist.add_rule(ip.address, ip.address, ALLOW_RANGE) self.num_whited += 1 log.trace("Added %s to the ipfiler as white-listed", ip.address) log.info("Added %d ranges to ipfilter as white-listed", self.num_whited) self.core.session.set_ip_filter(self.blocklist) return result