Example #1
0
class Worker(object):
    def __init__(self, server_at):
        self.server_at = server_at
        self.regex_worker = Regex()
        self.sock = utils.socket_connect_from_text(self.server_at)
        self.sock_f = self.sock.makefile(mode='r', encoding='UTF-8')

    def run(self):
        keep_running = True
        while keep_running:
            data = self.sock_f.readline()
            if data:
                data = data.strip()
                if data.startswith('FILTER ADD '):
                    data = data[11:].split(' ')
                    grp, regex = data[0], ' '.join(data[1:])
                    self.regex_worker.add_rule(grp, regex)
                elif data.startswith('MATCH '):
                    line = data[6:]
                    logger.debug('Matching: %s' % line)
                    for grp, r in self.regex_worker:
                        m = r.search(line)
                        if m:
                            logger.debug('Line "%s" matches group %s, host: %s' % (line, grp, m.groupdict().get('host', 'UNKNOWN')))
                            self.sock.send(('HIT %s %s\n' % (grp, m.groupdict().get('host', None))).encode('UTF-8'))
            else:
                keep_running = False
Example #2
0
 def setUp(self):
     self.w = Regex()
Example #3
0
class TestWorkerRegex(unittest.TestCase):
    def setUp(self):
        self.w = Regex()

    def test_rules_mng(self):
        self.assertEqual(self.w.get_rules_all(), {})

        self.w.add_rule('test1', '^rule1$')
        self.w.add_rule('test1', '^rule2$')
        self.w.add_rule('test1', '^rule3$')
        self.w.add_rule('test2', '^rule4$')
        self.w.add_rule('test2', '^rule5$')
        self.w.add_rule('test2', '^rule5$')
        self.w.add_rule('test2', '^rule5$')

        self.assertEqual(self.w.get_rules_all(), {
            'test1': ['^rule1$', '^rule2$', '^rule3$'],
            'test2': ['^rule4$', '^rule5$'],
        })

        self.assertEqual(self.w.get_rules('test2'), ['^rule4$', '^rule5$'])
        self.assertEqual(self.w.get_rules('not_defined'), [])

        self.w.remove_rule('test2', '^rule4$')
        self.assertEqual(self.w.get_rules('test2'), ['^rule5$'])

        self.w.remove_rules('test1')
        self.assertEqual(self.w.get_rules_all(), {
            'test2': ['^rule5$', ],
        })

        with self.assertRaises(KeyError):
            self.w.remove_rules('not_defined')

        with self.assertRaises(KeyError):
            self.w.remove_rule('test2', '^rule4$')

        self.w.remove_rules_all()
        self.assertEqual(self.w.get_rules_all(), {})

    def test_compiled_regex_generator(self):
        regexes = [x for x in self.w.compiled_regex_generator()]
        self.assertEqual(regexes, [])

        self.w.add_rule('test1', '^rule1$')
        self.w.add_rule('test2', '^rule2$')
        self.w.add_rule('test2', '^rule3$')

        regexes = [x[1].pattern for x in self.w.compiled_regex_generator()]
        self.assertEqual(sorted(regexes), sorted(['^rule1$', '^rule2$', '^rule3$', ]))

    def test_iter(self):
        self.w.add_rule('test1', '^rule1$')
        self.w.add_rule('test2', '^rule2$')
        self.w.add_rule('test2', '^rule3$')

        patterns = []
        for x in self.w:
            patterns.append(x[1].pattern)

        self.assertEqual(sorted(patterns), sorted(['^rule1$', '^rule2$', '^rule3$', ]))
Example #4
0
 def setUp(self):
     self.w = Regex()
Example #5
0
class TestWorkerRegex(unittest.TestCase):
    def setUp(self):
        self.w = Regex()

    def test_rules_mng(self):
        self.assertEqual(self.w.get_rules_all(), {})

        self.w.add_rule('test1', '^rule1$')
        self.w.add_rule('test1', '^rule2$')
        self.w.add_rule('test1', '^rule3$')
        self.w.add_rule('test2', '^rule4$')
        self.w.add_rule('test2', '^rule5$')
        self.w.add_rule('test2', '^rule5$')
        self.w.add_rule('test2', '^rule5$')

        self.assertEqual(
            self.w.get_rules_all(), {
                'test1': ['^rule1$', '^rule2$', '^rule3$'],
                'test2': ['^rule4$', '^rule5$'],
            })

        self.assertEqual(self.w.get_rules('test2'), ['^rule4$', '^rule5$'])
        self.assertEqual(self.w.get_rules('not_defined'), [])

        self.w.remove_rule('test2', '^rule4$')
        self.assertEqual(self.w.get_rules('test2'), ['^rule5$'])

        self.w.remove_rules('test1')
        self.assertEqual(self.w.get_rules_all(), {
            'test2': [
                '^rule5$',
            ],
        })

        with self.assertRaises(KeyError):
            self.w.remove_rules('not_defined')

        with self.assertRaises(KeyError):
            self.w.remove_rule('test2', '^rule4$')

        self.w.remove_rules_all()
        self.assertEqual(self.w.get_rules_all(), {})

    def test_compiled_regex_generator(self):
        regexes = [x for x in self.w.compiled_regex_generator()]
        self.assertEqual(regexes, [])

        self.w.add_rule('test1', '^rule1$')
        self.w.add_rule('test2', '^rule2$')
        self.w.add_rule('test2', '^rule3$')

        regexes = [x[1].pattern for x in self.w.compiled_regex_generator()]
        self.assertEqual(sorted(regexes),
                         sorted([
                             '^rule1$',
                             '^rule2$',
                             '^rule3$',
                         ]))

    def test_iter(self):
        self.w.add_rule('test1', '^rule1$')
        self.w.add_rule('test2', '^rule2$')
        self.w.add_rule('test2', '^rule3$')

        patterns = []
        for x in self.w:
            patterns.append(x[1].pattern)

        self.assertEqual(sorted(patterns),
                         sorted([
                             '^rule1$',
                             '^rule2$',
                             '^rule3$',
                         ]))
Example #6
0
 def __init__(self, server_at):
     self.server_at = server_at
     self.regex_worker = Regex()
     self.sock = utils.socket_connect_from_text(self.server_at)
     self.sock_f = self.sock.makefile(mode='r', encoding='UTF-8')