def block(self): block_pending = self.model.get_block_pending() if not block_pending: return c = self.get_connection() current = set(c.nullroute_list()) to_block = {} for b in block_pending: if IP(b.ip) in current: logger.warning("already blocked %s" % b.ip) else: logger.info("blocking %s (%s)" % (b.ip, b.who)) to_block[IP(b.ip)] = b notify_block(b) if to_block: for batch in util.window(to_block.keys(), 250): c.nullroute_add_many(batch) current = set(c.nullroute_list()) for b in block_pending: if IP(b.ip) in current: b.set_blocked() else: logger.error("error blocking %s" % b.ip)
def unblock(self): unblock_pending = self.model.get_unblock_pending() if not unblock_pending: return c = self.get_connection() current = set(c.nullroute_list()) to_unblock = {} for b in unblock_pending: if IP(b.ip) not in current: logger.warning("already unblocked %s" % b.ip) else: logger.info("unblocking %s (%s)" % (b.ip, b.who)) to_unblock[IP(b.ip)] = b if to_unblock: for batch in util.window(to_unblock.keys(), 250): c.nullroute_remove_many(batch) current = set(c.nullroute_list()) for b in unblock_pending: if IP(b.ip) not in current: b.set_unblocked() else: logger.error("error unblocking %s" % b.ip) out = c.write_mem() out = ' '.join(out) logger.info("write mem: %s" % out)