eulaurarien/feed_rules.py
Geoffrey Frogeye dcf39c9582
Put packing in parsing thread
Why did I think this would be a good idea?
- value don't need to be packed most of the time, but we don't know that
early
- packed domain (it's one most of the time) is way larger than its
unpacked counterpart
2019-12-16 10:38:37 +01:00

55 lines
1.6 KiB
Python
Executable file

#!/usr/bin/env python3
import database
import argparse
import sys
import time
import typing
FUNCTION_MAP: typing.Dict[str, typing.Tuple[
typing.Callable[[database.Database, database.Path, int], None],
typing.Callable[[str], database.Path],
]] = {
'hostname': (database.Database.set_hostname,
database.Database.pack_domain),
'zone': (database.Database.set_zone,
database.Database.pack_domain),
'asn': (database.Database.set_asn,
database.Database.pack_asn),
'ip4address': (database.Database.set_ip4address,
database.Database.pack_ip4address),
'ip4network': (database.Database.set_ip4network,
database.Database.pack_ip4network),
}
if __name__ == '__main__':
# Parsing arguments
parser = argparse.ArgumentParser(
description="TODO")
parser.add_argument(
'type',
choices=FUNCTION_MAP.keys(),
help="Type of rule inputed")
parser.add_argument(
'-i', '--input', type=argparse.FileType('r'), default=sys.stdin,
help="List of domains domains to block (with their subdomains)")
parser.add_argument(
'-f', '--first-party', action='store_true',
help="The input only comes from verified first-party sources")
args = parser.parse_args()
DB = database.Database()
fun, packer = FUNCTION_MAP[args.type]
for rule in args.input:
packed = packer(rule.strip())
fun(DB,
packed,
# is_first_party=args.first_party,
updated=int(time.time()),
)
DB.save()