2019-12-13 00:11:21 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import database
|
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# Parsing arguments
|
|
|
|
parser = argparse.ArgumentParser(
|
2019-12-19 08:05:05 +01:00
|
|
|
description="Export the hostnames rules stored "
|
|
|
|
"in the Database as plain text")
|
2019-12-13 00:11:21 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-o', '--output', type=argparse.FileType('w'), default=sys.stdout,
|
2019-12-19 08:05:05 +01:00
|
|
|
help="Output file, one rule per line")
|
2019-12-13 00:11:21 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-f', '--first-party', action='store_true',
|
2019-12-19 08:05:05 +01:00
|
|
|
help="Only output rules issued from first-party sources")
|
2019-12-13 00:11:21 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-e', '--end-chain', action='store_true',
|
2019-12-19 08:05:05 +01:00
|
|
|
help="Only output rules that are not referenced by any other")
|
2019-12-13 18:36:08 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-r', '--rules', action='store_true',
|
2019-12-19 08:05:05 +01:00
|
|
|
help="Output all kinds of rules, not just hostnames")
|
2019-12-18 01:03:08 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-b', '--base-rules', action='store_true',
|
2019-12-19 08:05:05 +01:00
|
|
|
help="Output base rules "
|
|
|
|
"(the ones added by ./feed_rules.py) "
|
|
|
|
"(implies --rules)")
|
2019-12-17 14:09:06 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--no-dupplicates', action='store_true',
|
2019-12-19 08:05:05 +01:00
|
|
|
help="Do not output rules that already match a zone/network rule "
|
|
|
|
"(e.g. dummy.example.com when there's a zone example.com rule)")
|
2019-12-18 01:03:08 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-x', '--explain', action='store_true',
|
2019-12-19 08:05:05 +01:00
|
|
|
help="Show the chain of rules leading to one "
|
|
|
|
"(and the number of references they have)")
|
2019-12-13 18:36:08 +01:00
|
|
|
parser.add_argument(
|
|
|
|
'-c', '--count', action='store_true',
|
2019-12-19 08:05:05 +01:00
|
|
|
help="Show the number of rules per type instead of listing them")
|
2019-12-13 00:11:21 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
DB = database.Database()
|
|
|
|
|
2019-12-17 13:29:02 +01:00
|
|
|
if args.count:
|
2019-12-18 01:03:08 +01:00
|
|
|
assert not args.explain
|
2019-12-17 13:29:02 +01:00
|
|
|
print(DB.count_records(
|
|
|
|
first_party_only=args.first_party,
|
2019-12-18 01:03:08 +01:00
|
|
|
end_chain_only=args.end_chain,
|
2019-12-17 14:09:06 +01:00
|
|
|
no_dupplicates=args.no_dupplicates,
|
2019-12-18 01:03:08 +01:00
|
|
|
rules_only=args.base_rules,
|
|
|
|
hostnames_only=not (args.rules or args.base_rules),
|
|
|
|
))
|
2019-12-17 13:29:02 +01:00
|
|
|
else:
|
2019-12-18 01:03:08 +01:00
|
|
|
for domain in DB.list_records(
|
2019-12-13 18:36:08 +01:00
|
|
|
first_party_only=args.first_party,
|
|
|
|
end_chain_only=args.end_chain,
|
2019-12-17 14:09:06 +01:00
|
|
|
no_dupplicates=args.no_dupplicates,
|
2019-12-18 01:03:08 +01:00
|
|
|
rules_only=args.base_rules,
|
|
|
|
hostnames_only=not (args.rules or args.base_rules),
|
2019-12-13 18:36:08 +01:00
|
|
|
explain=args.explain,
|
|
|
|
):
|
|
|
|
print(domain, file=args.output)
|