From 3df89dce437b718d691f08a8e7ec2fda75c9b6c5 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 2 Aug 2021 11:40:44 +0200 Subject: [PATCH] Added config --- config.yml | 4 ++++ daemon.py | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 config.yml diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..b501619 --- /dev/null +++ b/config.yml @@ -0,0 +1,4 @@ + +socket: + address: 0.0.0.0 + port: 53 diff --git a/daemon.py b/daemon.py index 20dae36..29a5a55 100755 --- a/daemon.py +++ b/daemon.py @@ -1,6 +1,7 @@ #!/usr/bin/python3 -import os, subprocess, socket, datetime, getopt, sys, re, atexit, logging as log +import os, subprocess, socket, datetime, getopt, sys, re, atexit, yaml, logging as log +from pathlib import Path import dns.flags import dns.message @@ -15,7 +16,9 @@ def main(args): setupLogging(True) log.debug("Logging started") - s = setupSocket('172.17.0.1', 53) + config = readConfig("config.yml") + + s = setupSocket(config['socket']['address'], config['socket']['port']) startListen(s) @@ -25,7 +28,7 @@ def setupSocket(address, port): return s def startListen(s): - log.debug('Now listening') + log.debug(f'Now listening') while True: (address, dmsg) = receiveFromWire(s) @@ -98,5 +101,17 @@ def setupLogging(verbose: bool): log.basicConfig(stream=sys.stdout, format=format, level=level) +def readConfig(file: str): + log.debug(f"Reading config '{file}'..") + + if not os.path.isfile(file): + raise OSError(2, file) + + return readYamlFile(file) + +def readYamlFile(file: str): + with open(file, "r") as f: + return yaml.load(f, Loader=yaml.FullLoader) + if __name__ == "__main__": sys.exit(main(sys.argv))