Browse Source

update: dnsproxy utils

dev
dnomd343 2 years ago
parent
commit
cacf52f97d
  1. 25
      Utils/DnsProxy.py
  2. 2
      Utils/Logger.py

25
Basis/DnsProxy.py → 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)

2
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',

Loading…
Cancel
Save