From cacf52f97d7b19cb9ae76952bb95aa7839200341 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 7 Sep 2022 14:06:55 +0800 Subject: [PATCH] update: dnsproxy utils --- {Basis => Utils}/DnsProxy.py | 25 +++++++++++++------------ Utils/Logger.py | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) rename {Basis => Utils}/DnsProxy.py (54%) diff --git a/Basis/DnsProxy.py b/Utils/DnsProxy.py similarity index 54% rename from Basis/DnsProxy.py rename to Utils/DnsProxy.py index 276c3a4..970fbad 100644 --- a/Basis/DnsProxy.py +++ b/Utils/DnsProxy.py @@ -3,30 +3,31 @@ import time import subprocess -from Basis.Logger import logging +from Utils.Logger import logger def run(command: list) -> subprocess.Popen: - logging.debug('Start dnsproxy -> %s' % command) - return subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) + logger.debug('Start dnsproxy -> %s' % command) + return subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) # stdout and stderr -def daemon(process: subprocess.Popen, command: list, gap: int = 2) -> None: # daemon dnsproxy process - while True: # start daemon - time.sleep(gap) # check time gap +def daemon(process: subprocess.Popen, command: list, gap: int = 3) -> None: # daemon dnsproxy process + while True: # start daemon process + time.sleep(gap) # check time gap (default 3s) if process.poll() is not None: # unexpected exit - logging.error('DnsProxy unexpected exit\n%s\n%s%s' % ( - '-' * 96, process.stdout.read().decode('utf-8'), '-' * 96) + logger.error('DnsProxy unexpected exit\n%s\n%s%s' % ( + '-' * 96, process.stdout.read().decode(encoding = 'utf-8'), '-' * 96) ) process = run(command) -def start(servers: list or None, port: int = 53, cache: int = 4194304) -> None: # default cache size -> 4MiB +def start(servers: str or list or None, port: int = 53, cache: int = 4194304) -> None: # default cache size -> 4MiB + servers = [servers] if type(servers) == str else servers if servers is not None and type(servers) != list: # invalid server content - logging.error('Invalid DNS server -> %s' % servers) + logger.error('Invalid DNS server -> %s' % servers) return if servers is None or len(servers) == 0: # use origin dns server - logging.info('Skip dnsproxy process') + logger.info('Skip dnsproxy process') return with open('/etc/resolv.conf', 'w') as dnsConfig: dnsConfig.write('nameserver 127.0.0.1\n') # system dns settings @@ -37,5 +38,5 @@ def start(servers: list or None, port: int = 53, cache: int = 4194304) -> None: ] for server in servers: dnsCommand += ['--upstream', server] # upstream dns servers - logging.warning('Start dnsproxy at port %i -> %s' % (port, servers)) + logger.warning('Start dnsproxy at port %i -> %s' % (port, servers)) daemon(run(dnsCommand), dnsCommand) diff --git a/Utils/Logger.py b/Utils/Logger.py index 943a062..d5f7c99 100644 --- a/Utils/Logger.py +++ b/Utils/Logger.py @@ -4,7 +4,7 @@ import sys import logging import colorlog -from Basis.Constant import LogLevel, LogFile +from Utils.Constant import LogLevel, LogFile logColor = { # log color 'DEBUG': 'white',