Browse Source

feat: main process

master
dnomd343 2 years ago
parent
commit
89564f0fc3
  1. 4
      Basis/Api.py
  2. 2
      Basis/Compile.py
  3. 42
      main.py

4
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:

2
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)

42
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')
Loading…
Cancel
Save