2019-12-09 08:12:48 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import database
|
|
|
|
import argparse
|
|
|
|
import sys
|
2019-12-13 00:11:21 +01:00
|
|
|
import logging
|
2019-12-09 08:12:48 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# Parsing arguments
|
2019-12-13 00:11:21 +01:00
|
|
|
log = logging.getLogger('feed_dns')
|
2019-12-09 08:12:48 +01:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="TODO")
|
|
|
|
parser.add_argument(
|
2019-12-13 00:11:21 +01:00
|
|
|
# '-i', '--input', type=argparse.FileType('rb'), default=sys.stdin.buffer,
|
|
|
|
'-i', '--input', type=argparse.FileType('r'), default=sys.stdin,
|
2019-12-09 08:12:48 +01:00
|
|
|
help="TODO")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-12-13 12:35:05 +01:00
|
|
|
DB = database.Database(write=True)
|
2019-12-09 08:12:48 +01:00
|
|
|
|
|
|
|
try:
|
2019-12-13 00:11:21 +01:00
|
|
|
DB.enter_step('iowait')
|
|
|
|
# line: bytes
|
|
|
|
line: str
|
2019-12-09 08:12:48 +01:00
|
|
|
for line in args.input:
|
2019-12-13 00:11:21 +01:00
|
|
|
DB.enter_step('feed_json_parse')
|
|
|
|
# split = line.split(b'"')
|
|
|
|
split = line.split('"')
|
|
|
|
try:
|
2019-12-13 13:54:00 +01:00
|
|
|
updated = int(split[3])
|
2019-12-13 00:11:21 +01:00
|
|
|
name = split[7]
|
|
|
|
dtype = split[11]
|
|
|
|
value = split[15]
|
|
|
|
except IndexError:
|
|
|
|
log.error("Invalid JSON: %s", line)
|
|
|
|
continue
|
|
|
|
# DB.enter_step('feed_json_assert')
|
2019-12-09 08:12:48 +01:00
|
|
|
# data = json.loads(line)
|
|
|
|
# assert dtype == data['type']
|
|
|
|
# assert name == data['name']
|
|
|
|
# assert value == data['value']
|
2019-12-13 00:11:21 +01:00
|
|
|
|
|
|
|
DB.enter_step('feed_switch')
|
|
|
|
if dtype == 'a':
|
|
|
|
for rule in DB.get_ip4(value):
|
2019-12-13 13:54:00 +01:00
|
|
|
DB.set_hostname(name, source=rule, updated=updated)
|
2019-12-13 00:11:21 +01:00
|
|
|
elif dtype == 'cname':
|
|
|
|
for rule in DB.get_domain(value):
|
2019-12-13 13:54:00 +01:00
|
|
|
DB.set_hostname(name, source=rule, updated=updated)
|
2019-12-13 00:11:21 +01:00
|
|
|
elif dtype == 'ptr':
|
|
|
|
for rule in DB.get_domain(value):
|
2019-12-13 13:54:00 +01:00
|
|
|
DB.set_ip4address(name, source=rule, updated=updated)
|
2019-12-13 00:11:21 +01:00
|
|
|
DB.enter_step('iowait')
|
2019-12-09 08:12:48 +01:00
|
|
|
except KeyboardInterrupt:
|
2019-12-13 00:11:21 +01:00
|
|
|
log.warning("Interupted.")
|
2019-12-09 08:12:48 +01:00
|
|
|
pass
|
|
|
|
|
2019-12-13 00:11:21 +01:00
|
|
|
DB.close()
|