respodns/respodns/ui.py

73 lines
2.2 KiB
Python
Raw Permalink Normal View History

2020-08-29 06:13:59 -07:00
def ui(program, args):
from .db import RespoDB
from .dns import main
2020-09-03 03:41:47 -07:00
from .ip_info import IpInfoByIpApi
2020-08-29 06:13:59 -07:00
from .structs import Options
from argparse import ArgumentParser
from asyncio import run
import respodns.checks as chk
name = "respodns"
2020-08-29 06:34:46 -07:00
desc = name + ": test and log DNS records"
parser = ArgumentParser(name, description=desc)
2020-08-29 06:13:59 -07:00
2020-08-29 06:34:46 -07:00
desc = "a path to a file containing IPv4 addresses which host DNS servers"
parser.add_argument("path", metavar="file-path", nargs="+", help=desc)
2020-08-29 06:13:59 -07:00
2020-08-29 06:34:46 -07:00
parser.add_argument("--database", help="specify database for logging")
2020-08-29 06:13:59 -07:00
desc = "enable pretty-printing progress to stderr"
parser.add_argument("--progress", action="store_true", help=desc)
chkgrp = parser.add_mutually_exclusive_group()
2020-09-04 07:17:32 -07:00
desc = "enable all checks instead of only the most likely ones"
chkgrp.add_argument("--all", action="store_true", help=desc)
2020-09-04 07:17:32 -07:00
2020-09-08 03:54:13 -07:00
desc = "enable only the first check"
chkgrp.add_argument("--first", action="store_true", help=desc)
2020-09-08 03:54:13 -07:00
desc = "enable debugging verbosity"
parser.add_argument("--debug", action="store_true", help=desc)
2020-08-29 06:13:59 -07:00
a = parser.parse_args(args)
2020-09-04 07:17:32 -07:00
checks = [] + chk.first
if a.all:
2020-09-08 03:54:13 -07:00
checks += chk.checks
elif a.first:
pass
else:
checks += chk.likely
2020-08-29 06:13:59 -07:00
opts = Options()
opts.dry = a.database is None
opts.early_stopping = opts.dry
opts.progress = a.progress
2020-08-29 06:13:59 -07:00
ipinfo = IpInfoByIpApi("ipinfo_cache.csv")
2020-09-03 03:41:47 -07:00
2020-08-29 06:13:59 -07:00
if a.database is not None:
if a.database.startswith("sqlite:"):
uri = a.database
else:
uri = "sqlite:///" + a.database
2020-09-08 03:54:13 -07:00
def runwrap(db):
if a.debug:
2020-08-29 06:13:59 -07:00
import logging
logging.basicConfig(level=logging.DEBUG)
run(main(db, a.path, checks, ipinfo, opts), debug=True)
2020-08-29 06:13:59 -07:00
else:
run(main(db, a.path, checks, ipinfo, opts))
2020-08-29 06:13:59 -07:00
if opts.dry:
runwrap(None)
else:
# log to a database.
db = RespoDB(uri, create=True)
with db: # TODO: .open and .close methods for manual invocation.
with db.execution as execution: # TODO: clean up this interface.
opts.execution = execution
runwrap(db)