def get_class(self): store = MagicMock() classes = [] for subcls in Blocklist.__subclasses__(): classes.append(subcls) for klass in classes: result = Blocklist.get_class(klass.__name__, store) self.assertEqual(result.__class__, klass)
def get_class(self): store = MagicMock() classes = [] for subcls in Blocklist.__subclasses__(): classes.append(subcls) for klass in classes: result = Blocklist.get_class(klass.__name__, store) self.assertEqual(result.__class__, klass)
class TestBlocklist(TestBlocklistBase): def setUp(self): super(TestBlocklist, self).setUp() self.bl = Blocklist(self.store, filename=self.filename) def test_init(self): self.assertEqual(self.bl.store, self.store) self.assertEqual(self.bl.filename, self.filename) def test_repr(self): expected = ( "{0}({1}, filename={2})".format( "Blocklist", self.store, self.filename ) ) self.assertEqual(self.bl.__repr__(), expected) def get_class(self): store = MagicMock() classes = [] for subcls in Blocklist.__subclasses__(): classes.append(subcls) for klass in classes: result = Blocklist.get_class(klass.__name__, store) self.assertEqual(result.__class__, klass) def get_class_raises(self): store = MagicMock() with self.assertRaises(ValueError): Blocklist.get_class('nonexisting', store) def test_get_ips_simple(self): contents = dedent( """ Test:1.1.1.1-2.2.2.2 Some other Test:3.3.3.3-3.3.3.3 """ ) self.tempfile.file.write(contents.encode('utf-8')) self.tempfile.file.flush() expected = [ '1.1.1.1-2.2.2.2', '3.3.3.3-3.3.3.3' ] result = self.bl.get_ips() self.assertCountEqual(result, expected)
def get_multiple_lists(): # Get query arguments cidr_notation = request.args.get('cidr', default=False) blocklists = request.args.get('blocklists', default=None) listname = request.args.get("listname", default="blocklist") blists = [] if not blocklists else blocklists.split(',') comment = request.args.get("comment", default="multilist") ips = [] for blist in blists: try: bl = Blocklist.get_class(blist, store) ips.extend(bl.get_ips(cidr_notation=cidr_notation)) except ValueError: # Silently ignore unknown blocklist pass # Make List of ips unique and sorted ips = list(set(ips)) ips.sort() result = render_template( "mikrotik_addresslist.jinja2", ips=ips, listname=listname, comment=comment) response = make_response(result, 200) response.headers['Content-Type'] = "text/plain" return response
def get_list(blacklist): # Get query arguments cidr_notation = request.args.get('cidr', default=False) # First find the right class bl = Blocklist.get_class(blacklist, store) ips = bl.get_ips(cidr_notation=cidr_notation) if not ips: raise EmptyListError("No ips found for %s" % blacklist.title()) # If deduplicating, process if dedupe: smr = Summerizer(ips) ips = smr.summary() # Get User variables if any listname = request.args.get( "listname", default="%s_list" % bl.__class__.__name__.lower()) comment = request.args.get( "comment", default="%s" % bl.__class__.__name__.title()) result = render_template( "mikrotik_addresslist.jinja2", ips=ips, listname=listname, comment=comment ) response = make_response(result, 200) response.headers['Content-Type'] = "text/plain" return response
def get_multiple_lists(): # Get query arguments cidr_notation = request.args.get('cidr', default=False) blocklists = request.args.get('blocklists', default=None) listname = request.args.get("listname", default="blocklist") blists = [] if not blocklists else blocklists.split(',') comment = request.args.get("comment", default="multilist") ips = [] for blist in blists: try: bl = Blocklist.get_class(blist, store) ips.extend(bl.get_ips(cidr_notation=cidr_notation)) except ValueError: # Silently ignore unknown blocklist pass # Make List of ips unique and sorted ips = list(set(ips)) ips.sort() result = render_template("mikrotik_addresslist.jinja2", ips=ips, listname=listname, comment=comment) response = make_response(result, 200) response.headers['Content-Type'] = "text/plain" return response
def get_list(blacklist): # Get query arguments cidr_notation = request.args.get('cidr', default=False) # First find the right class bl = Blocklist.get_class(blacklist, store) ips = bl.get_ips(cidr_notation=cidr_notation) if not ips: raise EmptyListError("No ips found for %s" % blacklist.title()) # If deduplicating, process if dedupe: smr = Summerizer(ips) ips = smr.summary() # Get User variables if any listname = request.args.get("listname", default="%s_list" % bl.__class__.__name__.lower()) comment = request.args.get("comment", default="%s" % bl.__class__.__name__.title()) result = render_template("mikrotik_addresslist.jinja2", ips=ips, listname=listname, comment=comment) response = make_response(result, 200) response.headers['Content-Type'] = "text/plain" return response
def get_list(blacklist): # First find the right class bl = Blocklist.get_class(blacklist, store) ips = bl.get_ips() if not ips: raise EmptyListError( "No ips found for {}".format(blacklist.title()) ) # Get User variables if any listname = request.args.get( "listname", "{}_list".format(bl.__class__.__name__.lower()) ) comment = request.args.get( "comment", "{}".format(bl.__class__.__name__.title()) ) result = render_template( "mikrotik_addresslist.jinja2", ips=ips, listname=listname, comment=comment ) response = make_response(result, 200) response.headers['Content-Type'] = "text/plain" return response
def handle_unknown_blacklist(exc): routes = ["/%s" % x.__name__.lower() for x in Blocklist.__subclasses__()] msg = render_template( 'unknown_blacklist.jinja2', routes=routes, exc=exc, ) response = make_response(msg, 404) response.headers['Content-Type'] = "text/plain" return response
def index(): lists = [] for subcls in Blocklist.__subclasses__(): lists.append(subcls.__name__) result = render_template("welcome.jinja2", lists=sorted(lists), version=__version__) response = make_response(result, 200) response.headers['Content-Type'] = "text/plain" return response
def index(): lists = [] for subcls in Blocklist.__subclasses__(): lists.append(subcls.__name__) result = render_template( "welcome.jinja2", lists=sorted(lists), version=__version__) response = make_response(result, 200) response.headers['Content-Type'] = "text/plain" return response
def handle_unknown_blacklist(exc): routes = [ "/{}".format(x.__name__.lower()) for x in Blocklist.__subclasses__() ] msg = render_template( 'unknown_blacklist.jinja2', routes=routes, exc=exc, ) response = make_response(msg, 404) response.headers['Content-Type'] = "text/plain" return response
def run(self): LOG.info("Start Blocklister-Updater") instances = [] for subcls in Blocklist.__subclasses__(): instances.append(subcls( self.store, refresh_list=timedelta(days=self.refresh_list))) while True: for instance in instances: if instance.fetcher.needs_update: LOG.info( "Updating Blocklister list {}".format( instance.__class__.__name__)) instance.fetcher.update() time.sleep(self.interval)
def run(self): LOG.info("Start Blocklister-Updater") instances = [] for subcls in Blocklist.__subclasses__(): instances.append( subcls(self.store, refresh_list=timedelta(days=self.refresh_list))) while True: for instance in instances: try: if instance.fetcher.needs_update: LOG.info("Updating Blocklister list {}".format( instance.__class__.__name__)) instance.fetcher.update() except FetcherException as exc: LOG.error(exc) time.sleep(self.interval)
def get_multiple_lists(): blocklists = request.args.get('blocklists', None) listname = request.args.get("listname", "blocklist") blists = [] if not blocklists else blocklists.split(',') comment = request.args.get("comment", "multilist") ips = [] for blist in blists: try: bl = Blocklist.get_class(blist, store) ips.extend(bl.get_ips()) except ValueError: # Silently ignore unknown blocklist pass result = render_template( "mikrotik_addresslist.jinja2", ips=ips, listname=listname, comment=comment) response = make_response(result, 200) response.headers['Content-Type'] = "text/plain" return response
class TestBlocklist(TestBlocklistBase): def setUp(self): super(TestBlocklist, self).setUp() self.bl = Blocklist(self.store, filename=self.filename) def test_init(self): self.assertEqual(self.bl.store, self.store) self.assertEqual(self.bl.filename, self.filename) def test_repr(self): expected = ( "{0}({1}, filename={2})".format( "Blocklist", self.store, self.filename ) ) self.assertEqual(self.bl.__repr__(), expected) def get_class(self): store = MagicMock() classes = [] for subcls in Blocklist.__subclasses__(): classes.append(subcls) for klass in classes: result = Blocklist.get_class(klass.__name__, store) self.assertEqual(result.__class__, klass) def get_class_raises(self): store = MagicMock() with self.assertRaises(ValueError): Blocklist.get_class('nonexisting', store) def test_get_ips_simple(self): self.maxDiff = None contents = dedent( """ Test:1.1.1.1-2.2.2.2 Some other Test:3.3.3.3-3.3.3.3 """ ) self.tempfile.file.write(contents.encode('utf-8')) self.tempfile.file.flush() expected = ["1.1.1.1-2.2.2.2", "3.3.3.3-3.3.3.3"] result = self.bl.get_ips() if sys.version_info[0] == 3: # noqa self.assertCountEqual(result, expected) else: self.assertItemsEqual(result, expected) def test_get_ips_cidr(self): self.maxDiff = None contents = dedent( """ Test:1.1.1.1-2.2.2.2 Some other Test:3.3.3.3-3.3.3.3 """ ) self.tempfile.file.write(contents.encode('utf-8')) self.tempfile.file.flush() expected = [ IPv4Network(u'1.1.1.1/32'), IPv4Network(u'1.1.1.2/31'), IPv4Network(u'1.1.1.4/30'), IPv4Network(u'1.1.1.8/29'), IPv4Network(u'1.1.1.16/28'), IPv4Network(u'1.1.1.32/27'), IPv4Network(u'1.1.1.64/26'), IPv4Network(u'1.1.1.128/25'), IPv4Network(u'1.1.2.0/23'), IPv4Network(u'1.1.4.0/22'), IPv4Network(u'1.1.8.0/21'), IPv4Network(u'1.1.16.0/20'), IPv4Network(u'1.1.32.0/19'), IPv4Network(u'1.1.64.0/18'), IPv4Network(u'1.1.128.0/17'), IPv4Network(u'1.2.0.0/15'), IPv4Network(u'1.4.0.0/14'), IPv4Network(u'1.8.0.0/13'), IPv4Network(u'1.16.0.0/12'), IPv4Network(u'1.32.0.0/11'), IPv4Network(u'1.64.0.0/10'), IPv4Network(u'1.128.0.0/9'), IPv4Network(u'2.0.0.0/15'), IPv4Network(u'2.2.0.0/23'), IPv4Network(u'2.2.2.0/31'), IPv4Network(u'2.2.2.2/32'), IPv4Network(u'3.3.3.3/32'), ] result = self.bl.get_ips(cidr_notation=True) if sys.version_info[0] == 3: # noqa self.assertCountEqual(result, expected) else: self.assertItemsEqual(result, expected)
def setUp(self): self.client = app.test_client() self.classes = [] for subcls in Blocklist.__subclasses__(): self.classes.append(subcls)
class TestBlocklist(TestBlocklistBase): def setUp(self): super(TestBlocklist, self).setUp() self.bl = Blocklist(self.store, filename=self.filename) def test_init(self): self.assertEqual(self.bl.store, self.store) self.assertEqual(self.bl.filename, self.filename) def test_repr(self): expected = ("{0}({1}, filename={2})".format("Blocklist", self.store, self.filename)) self.assertEqual(self.bl.__repr__(), expected) def get_class(self): store = MagicMock() classes = [] for subcls in Blocklist.__subclasses__(): classes.append(subcls) for klass in classes: result = Blocklist.get_class(klass.__name__, store) self.assertEqual(result.__class__, klass) def get_class_raises(self): store = MagicMock() with self.assertRaises(ValueError): Blocklist.get_class('nonexisting', store) def test_get_ips_simple(self): self.maxDiff = None contents = dedent(""" Test:1.1.1.1-2.2.2.2 Some other Test:3.3.3.3-3.3.3.3 """) self.tempfile.file.write(contents.encode('utf-8')) self.tempfile.file.flush() expected = ["1.1.1.1-2.2.2.2", "3.3.3.3-3.3.3.3"] result = self.bl.get_ips() if sys.version_info[0] == 3: # noqa self.assertCountEqual(result, expected) else: self.assertItemsEqual(result, expected) def test_get_ips_cidr(self): self.maxDiff = None contents = dedent(""" Test:1.1.1.1-2.2.2.2 Some other Test:3.3.3.3-3.3.3.3 """) self.tempfile.file.write(contents.encode('utf-8')) self.tempfile.file.flush() expected = [ IPv4Network(u'1.1.1.1/32'), IPv4Network(u'1.1.1.2/31'), IPv4Network(u'1.1.1.4/30'), IPv4Network(u'1.1.1.8/29'), IPv4Network(u'1.1.1.16/28'), IPv4Network(u'1.1.1.32/27'), IPv4Network(u'1.1.1.64/26'), IPv4Network(u'1.1.1.128/25'), IPv4Network(u'1.1.2.0/23'), IPv4Network(u'1.1.4.0/22'), IPv4Network(u'1.1.8.0/21'), IPv4Network(u'1.1.16.0/20'), IPv4Network(u'1.1.32.0/19'), IPv4Network(u'1.1.64.0/18'), IPv4Network(u'1.1.128.0/17'), IPv4Network(u'1.2.0.0/15'), IPv4Network(u'1.4.0.0/14'), IPv4Network(u'1.8.0.0/13'), IPv4Network(u'1.16.0.0/12'), IPv4Network(u'1.32.0.0/11'), IPv4Network(u'1.64.0.0/10'), IPv4Network(u'1.128.0.0/9'), IPv4Network(u'2.0.0.0/15'), IPv4Network(u'2.2.0.0/23'), IPv4Network(u'2.2.2.0/31'), IPv4Network(u'2.2.2.2/32'), IPv4Network(u'3.3.3.3/32'), ] result = self.bl.get_ips(cidr_notation=True) if sys.version_info[0] == 3: # noqa self.assertCountEqual(result, expected) else: self.assertItemsEqual(result, expected)
def setUp(self): super(TestBlocklist, self).setUp() self.bl = Blocklist(self.store, filename=self.filename)
def get_class_raises(self): store = MagicMock() with self.assertRaises(ValueError): Blocklist.get_class('nonexisting', store)
def get_class_raises(self): store = MagicMock() with self.assertRaises(ValueError): Blocklist.get_class('nonexisting', store)
def setUp(self): super(TestBlocklist, self).setUp() self.bl = Blocklist(self.store, filename=self.filename)
def setUp(self): self.client = app.test_client() self.classes = [] for subcls in Blocklist.__subclasses__(): self.classes.append(subcls)