diff --git a/Basis/Api.py b/Basis/Api.py index 213576b..a508701 100644 --- a/Basis/Api.py +++ b/Basis/Api.py @@ -26,9 +26,9 @@ def getVersion() -> Response: def startServer(apiPort: int = 7839, apiToken: str = '', isWsgi: bool = True) -> None: global webApi, webPath logging.warning( - 'start api server at http://:%i/' % apiPort + (' (enable WSGI)' if isWsgi else '') + 'API server at http://:%i/' % apiPort + (' (enable WSGI)' if isWsgi else '') ) - logging.warning('server ' + ('without token' if apiToken == '' else 'api token -> %s' % apiToken)) + logging.warning('API ' + ('without token' if apiToken == '' else 'token -> %s' % apiToken)) if not isWsgi: webApi.run(host = '0.0.0.0', port = apiPort, debug = True, threaded = True) # ordinary server (for debug) else: diff --git a/Basis/Compile.py b/Basis/Compile.py index c599130..e084edf 100644 --- a/Basis/Compile.py +++ b/Basis/Compile.py @@ -7,5 +7,5 @@ from Basis.Logger import logging def startCompile(dirRange: str = '/') -> None: for optimize in [-1, 1, 2]: - logging.warning('start python compile -> %s (optimize = %i)' % (dirRange, optimize)) + logging.warning('python optimize compile -> %s (level = %i)' % (dirRange, optimize)) compileall.compile_dir(dirRange, quiet = 1, maxlevels = 256, optimize = optimize) diff --git a/main.py b/main.py new file mode 100755 index 0000000..d1eb411 --- /dev/null +++ b/main.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import time +import _thread +import subprocess +from Basis.Logger import logging +from Basis.Api import startServer +from Basis.Constant import Version +from Basis.Compile import startCompile + + +def startDnsproxy(command: list) -> subprocess.Popen: + logging.debug('start dnsproxy -> %s' % command) + return subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.STDOUT) + + +def daemonDnsproxy(dnsServers: list, localPort: int = 53, cacheSize: int = 4194304) -> None: # default cache -> 4MiB + logging.info('start dnsproxy at port %i -> %s' % (localPort, dnsServers)) + dnsCommand = [ + 'dnsproxy', '--all-servers', + '--port', str(localPort), + '--cache', '--cache-size', str(cacheSize) + ] + for dnsServer in dnsServers: + dnsCommand += ['--upstream', dnsServer] + dnsproxy = startDnsproxy(dnsCommand) + while True: + time.sleep(1) # daemon time gap + if dnsproxy.poll() is not None: # unexpected exit + logging.warning('dnsproxy unexpected exit') + logging.debug('output of dnsproxy\n%s' % dnsproxy.stdout.read().decode('UTF-8')) + dnsproxy = startDnsproxy(dnsCommand) + + +logging.warning('ProxyC starts running (%s)' % Version) + +_thread.start_new_thread(startCompile, ('/usr', )) + +_thread.start_new_thread(daemonDnsproxy, (['223.5.5.5', '119.28.28.28'], 53)) + +startServer(apiToken = 'dnomd343')