2019-12-09 08:12:48 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
2019-12-14 17:27:46 +01:00
|
|
|
import database
|
2019-12-13 00:11:21 +01:00
|
|
|
import logging
|
2019-12-14 17:27:46 +01:00
|
|
|
import sys
|
2019-12-13 12:36:11 +01:00
|
|
|
import typing
|
2019-12-15 17:05:41 +01:00
|
|
|
import multiprocessing
|
2019-12-14 23:59:50 +01:00
|
|
|
import enum
|
2019-12-14 17:27:46 +01:00
|
|
|
|
2019-12-15 17:12:44 +01:00
|
|
|
Record = typing.Tuple[typing.Callable, typing.Callable, int, str, str]
|
2019-12-14 17:27:46 +01:00
|
|
|
|
2019-12-15 15:56:26 +01:00
|
|
|
# select, write
|
2019-12-14 17:27:46 +01:00
|
|
|
FUNCTION_MAP: typing.Any = {
|
2019-12-15 17:12:44 +01:00
|
|
|
'a': (
|
2019-12-14 17:27:46 +01:00
|
|
|
database.Database.get_ip4,
|
|
|
|
database.Database.set_hostname,
|
|
|
|
),
|
2019-12-15 17:12:44 +01:00
|
|
|
'cname': (
|
2019-12-14 17:27:46 +01:00
|
|
|
database.Database.get_domain,
|
|
|
|
database.Database.set_hostname,
|
|
|
|
),
|
2019-12-15 17:12:44 +01:00
|
|
|
'ptr': (
|
2019-12-14 17:27:46 +01:00
|
|
|
database.Database.get_domain,
|
|
|
|
database.Database.set_ip4address,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-15 17:05:41 +01:00
|
|
|
class Writer(multiprocessing.Process):
|
|
|
|
def __init__(self,
|
|
|
|
recs_queue: multiprocessing.Queue,
|
|
|
|
index: int = 0):
|
|
|
|
super(Writer, self).__init__()
|
|
|
|
self.log = logging.getLogger(f'wr')
|
|
|
|
self.recs_queue = recs_queue
|
|
|
|
|
|
|
|
def run(self) -> None:
|
2019-12-15 15:56:26 +01:00
|
|
|
self.db = database.Database()
|
2019-12-15 17:05:41 +01:00
|
|
|
self.db.log = logging.getLogger(f'wr')
|
|
|
|
|
|
|
|
self.db.enter_step('block_wait')
|
|
|
|
block: typing.List[Record]
|
|
|
|
for block in iter(self.recs_queue.get, None):
|
|
|
|
|
|
|
|
record: Record
|
|
|
|
for record in block:
|
2019-12-14 23:59:50 +01:00
|
|
|
|
2019-12-15 17:12:44 +01:00
|
|
|
select, write, updated, name, value = record
|
2019-12-15 17:05:41 +01:00
|
|
|
self.db.enter_step('feed_switch')
|
|
|
|
|
2019-12-15 22:21:05 +01:00
|
|
|
try:
|
|
|
|
for source in select(self.db, value):
|
|
|
|
# write(self.db, name, updated, source=source)
|
|
|
|
write(self.db, name, updated)
|
|
|
|
except ValueError:
|
|
|
|
self.log.exception("Cannot execute: %s", record)
|
2019-12-15 17:05:41 +01:00
|
|
|
|
|
|
|
self.db.enter_step('block_wait')
|
|
|
|
|
|
|
|
self.db.enter_step('end')
|
2019-12-15 15:56:26 +01:00
|
|
|
self.db.save()
|
|
|
|
|
2019-12-14 23:59:50 +01:00
|
|
|
|
2019-12-15 17:05:41 +01:00
|
|
|
class Parser():
|
|
|
|
def __init__(self,
|
|
|
|
buf: typing.Any,
|
|
|
|
recs_queue: multiprocessing.Queue,
|
|
|
|
block_size: int,
|
|
|
|
):
|
|
|
|
super(Parser, self).__init__()
|
|
|
|
self.buf = buf
|
|
|
|
self.log = logging.getLogger('pr')
|
|
|
|
self.recs_queue = recs_queue
|
|
|
|
self.block: typing.List[Record] = list()
|
|
|
|
self.block_size = block_size
|
|
|
|
self.prof = database.Profiler()
|
|
|
|
self.prof.log = logging.getLogger('pr')
|
|
|
|
|
|
|
|
def register(self, record: Record) -> None:
|
|
|
|
self.prof.enter_step('register')
|
|
|
|
self.block.append(record)
|
|
|
|
if len(self.block) >= self.block_size:
|
|
|
|
self.prof.enter_step('put_block')
|
|
|
|
self.recs_queue.put(self.block)
|
|
|
|
self.block = list()
|
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
self.consume()
|
|
|
|
self.recs_queue.put(self.block)
|
|
|
|
self.prof.profile()
|
2019-12-14 23:59:50 +01:00
|
|
|
|
|
|
|
def consume(self) -> None:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class Rapid7Parser(Parser):
|
|
|
|
def consume(self) -> None:
|
2019-12-15 16:38:01 +01:00
|
|
|
data = dict()
|
2019-12-14 23:59:50 +01:00
|
|
|
for line in self.buf:
|
2019-12-15 17:05:41 +01:00
|
|
|
self.prof.enter_step('parse_rapid7')
|
2019-12-15 16:38:01 +01:00
|
|
|
split = line.split('"')
|
|
|
|
|
2019-12-15 22:21:05 +01:00
|
|
|
try:
|
|
|
|
for k in range(1, 14, 4):
|
|
|
|
key = split[k]
|
|
|
|
val = split[k+2]
|
|
|
|
data[key] = val
|
|
|
|
|
|
|
|
select, writer = FUNCTION_MAP[data['type']]
|
|
|
|
record = (
|
|
|
|
select,
|
|
|
|
writer,
|
|
|
|
int(data['timestamp']),
|
|
|
|
data['name'],
|
|
|
|
data['value']
|
|
|
|
)
|
|
|
|
except IndexError:
|
|
|
|
self.log.exception("Cannot parse: %s", line)
|
2019-12-15 17:05:41 +01:00
|
|
|
self.register(record)
|
2019-12-14 23:59:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DnsMassParser(Parser):
|
|
|
|
# dnsmass --output Snrql
|
|
|
|
# --retry REFUSED,SERVFAIL --resolvers nameservers-ipv4
|
|
|
|
TYPES = {
|
2019-12-15 17:12:44 +01:00
|
|
|
'A': (FUNCTION_MAP['a'][0], FUNCTION_MAP['a'][1], -1, None),
|
|
|
|
# 'AAAA': (FUNCTION_MAP['aaaa'][0], FUNCTION_MAP['aaaa'][1], -1, None),
|
|
|
|
'CNAME': (FUNCTION_MAP['cname'][0], FUNCTION_MAP['cname'][1], -1, -1),
|
2019-12-14 23:59:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def consume(self) -> None:
|
2019-12-15 17:05:41 +01:00
|
|
|
self.prof.enter_step('parse_dnsmass')
|
2019-12-14 23:59:50 +01:00
|
|
|
timestamp = 0
|
|
|
|
header = True
|
|
|
|
for line in self.buf:
|
|
|
|
line = line[:-1]
|
|
|
|
if not line:
|
|
|
|
header = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
split = line.split(' ')
|
|
|
|
try:
|
|
|
|
if header:
|
|
|
|
timestamp = int(split[1])
|
|
|
|
header = False
|
|
|
|
else:
|
2019-12-15 17:12:44 +01:00
|
|
|
select, write, name_offset, value_offset = \
|
2019-12-14 23:59:50 +01:00
|
|
|
DnsMassParser.TYPES[split[1]]
|
2019-12-15 17:05:41 +01:00
|
|
|
record = (
|
2019-12-15 17:12:44 +01:00
|
|
|
select,
|
|
|
|
write,
|
2019-12-14 23:59:50 +01:00
|
|
|
timestamp,
|
|
|
|
split[0][:name_offset],
|
|
|
|
split[2][:value_offset],
|
|
|
|
)
|
2019-12-15 17:05:41 +01:00
|
|
|
self.register(record)
|
|
|
|
self.prof.enter_step('parse_dnsmass')
|
2019-12-14 23:59:50 +01:00
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
PARSERS = {
|
|
|
|
'rapid7': Rapid7Parser,
|
|
|
|
'dnsmass': DnsMassParser,
|
|
|
|
}
|
|
|
|
|
2019-12-13 12:36:11 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# Parsing arguments
|
|
|
|
log = logging.getLogger('feed_dns')
|
2019-12-14 23:59:50 +01:00
|
|
|
args_parser = argparse.ArgumentParser(
|
2019-12-13 12:36:11 +01:00
|
|
|
description="TODO")
|
2019-12-14 23:59:50 +01:00
|
|
|
args_parser.add_argument(
|
|
|
|
'parser',
|
|
|
|
choices=PARSERS.keys(),
|
|
|
|
help="TODO")
|
|
|
|
args_parser.add_argument(
|
2019-12-13 12:36:11 +01:00
|
|
|
'-i', '--input', type=argparse.FileType('r'), default=sys.stdin,
|
|
|
|
help="TODO")
|
2019-12-15 17:05:41 +01:00
|
|
|
args_parser.add_argument(
|
|
|
|
'-j', '--workers', type=int, default=4,
|
|
|
|
help="TODO")
|
|
|
|
args_parser.add_argument(
|
|
|
|
'-b', '--block-size', type=int, default=100,
|
|
|
|
help="TODO")
|
|
|
|
args_parser.add_argument(
|
|
|
|
'-q', '--queue-size', type=int, default=10,
|
|
|
|
help="TODO")
|
2019-12-14 23:59:50 +01:00
|
|
|
args = args_parser.parse_args()
|
2019-12-13 12:36:11 +01:00
|
|
|
|
2019-12-15 17:05:41 +01:00
|
|
|
recs_queue: multiprocessing.Queue = multiprocessing.Queue(
|
|
|
|
maxsize=args.queue_size)
|
|
|
|
|
|
|
|
writer = Writer(recs_queue)
|
|
|
|
writer.start()
|
|
|
|
|
|
|
|
parser = PARSERS[args.parser](args.input, recs_queue, args.block_size)
|
|
|
|
parser.run()
|
2019-12-13 12:36:11 +01:00
|
|
|
|
2019-12-15 17:05:41 +01:00
|
|
|
recs_queue.put(None)
|
|
|
|
writer.join()
|