Browse Source

update: global options

master^2
dnomd343 2 years ago
parent
commit
720dbdfd3c
  1. 11
      Basis/Compile.py
  2. 7
      Basis/Constant.py
  3. 27
      Basis/Logger.py
  4. 2
      Basis/Manager.py
  5. 4
      Basis/Test.py
  6. 33
      main.py

11
Basis/Compile.py

@ -1,11 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import compileall
from Basis.Logger import logging
def startCompile(dirRange: str = '/') -> None:
for optimize in [-1, 1, 2]:
compileall.compile_dir(dirRange, quiet = 1, maxlevels = 256, optimize = optimize)
logging.warning('Python optimize compile -> %s (level = %i)' % (dirRange, optimize))

7
Basis/Constant.py

@ -2,7 +2,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
Version = 'dev' Version = 'dev'
ApiToken = ''
# ApiToken = 'dnomd343'
# LogLevel = 'DEBUG'
LogLevel = 'WARNING'
LogFile = 'runtime.log'
WorkDir = '/tmp/ProxyC' WorkDir = '/tmp/ProxyC'
ObfsSite = 'www.bing.com'
DnsServer = ['223.5.5.5', '119.29.29.29']
PathEnv = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin' PathEnv = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'
# Shadowsocks Info # Shadowsocks Info

27
Basis/Logger.py

@ -4,18 +4,23 @@
import sys import sys
import logging import logging
from colorlog import ColoredFormatter from colorlog import ColoredFormatter
from Basis.Constant import LogLevel, LogFile
logLevel = { # log level
'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL
}[LogLevel.lower()]
dateFormat = '%Y-%m-%d %H:%M:%S' # log date format
logFormat = '[%(asctime)s] [%(levelname)s] %(message)s (%(module)s.%(funcName)s)' # log format
logFile = 'runtime.log'
# logLevel = logging.INFO
# logLevel = logging.DEBUG
logLevel = logging.WARNING
dateFormat = '%Y-%m-%d %H:%M:%S'
logFormat = '[%(asctime)s] [%(levelname)s] %(message)s (%(module)s.%(funcName)s)'
logging.basicConfig( logging.basicConfig(
level = logLevel, level = logLevel,
format = logFormat, format = logFormat,
datefmt = dateFormat, datefmt = dateFormat,
filename = logFile, filename = LogFile,
) )
logHandler = logging.StreamHandler(stream = sys.stdout) logHandler = logging.StreamHandler(stream = sys.stdout)
logHandler.setFormatter(ColoredFormatter( logHandler.setFormatter(ColoredFormatter(
@ -30,11 +35,3 @@ logHandler.setFormatter(ColoredFormatter(
} }
)) ))
logging.getLogger().addHandler(logHandler) logging.getLogger().addHandler(logHandler)
if __name__ == '__main__':
logging.debug('debug')
logging.info('info')
logging.warning('warn')
logging.error('error')
logging.critical('critical')

2
Basis/Manager.py

@ -78,7 +78,7 @@ class Task(object):
task['status'] = self.__TASK_RUNNING # set task status as running task['status'] = self.__TASK_RUNNING # set task status as running
logging.info('Manager pop task [%s] -> %s' % (taskId, task['data'])) logging.info('Manager pop task [%s] -> %s' % (taskId, task['data']))
return taskId, copy.deepcopy(task['data']) return taskId, copy.deepcopy(task['data'])
logging.debug('Manager has no more loaded tasks') logging.debug('Manager has no more task')
raise RuntimeError('No more tasks') raise RuntimeError('No more tasks')
def finishTask(self, taskId: str, taskData: dict) -> None: # update task data when completed def finishTask(self, taskId: str, taskData: dict) -> None: # update task data when completed

4
Basis/Test.py

@ -6,12 +6,12 @@ import time
import requests import requests
from threading import Thread from threading import Thread
from Basis.Logger import logging from Basis.Logger import logging
from Basis.Constant import WorkDir from Basis.Constant import WorkDir, ObfsSite
from Basis.Functions import md5Sum, genFlag, hostFormat, checkPortStatus from Basis.Functions import md5Sum, genFlag, hostFormat, checkPortStatus
Settings = { Settings = {
'workDir': WorkDir, 'workDir': WorkDir,
'site': 'www.bing.com', 'site': ObfsSite,
'serverBind': '', 'serverBind': '',
'clientBind': '', 'clientBind': '',
'host': '', 'host': '',

33
main.py

@ -1,41 +1,44 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import time import time
import _thread import _thread
from Basis import DnsProxy import compileall
from Basis import Constant
from Basis.Check import Check from Basis.Check import Check
from Basis import Api, DnsProxy
from Basis.Logger import logging from Basis.Logger import logging
from Basis.Manager import Manager from Basis.Manager import Manager
from Basis.Api import startServer
from Basis.Constant import Version
from Basis.Compile import startCompile
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
# dnsServers = None
dnsServers = ['223.5.5.5', '119.28.28.28'] def pythonCompile(dirRange: str = '/') -> None: # python optimize compile
for optimize in [-1, 1, 2]:
compileall.compile_dir(dirRange, quiet = 1, optimize = optimize)
logging.warning('Python optimize compile -> %s (level = %i)' % (dirRange, optimize))
def runCheck(taskId: str, taskInfo: dict) -> None: def runCheck(taskId: str, taskInfo: dict) -> None:
checkResult = Check(taskId, taskInfo) checkResult = Check(taskId, taskInfo) # check by task info
logging.warning('[%s] Task finish' % taskId) logging.warning('[%s] Task finish' % taskId)
Manager.finishTask(taskId, checkResult) Manager.finishTask(taskId, checkResult) # commit check result
def loopCheck(threadNum: int = 16) -> None: def loopCheck(threadNum: int = 16) -> None:
threadPool = ThreadPoolExecutor(max_workers = threadNum) threadPool = ThreadPoolExecutor(max_workers = threadNum) # init thread pool
while True: while True:
try: try:
taskId, taskInfo = Manager.popTask() taskId, taskInfo = Manager.popTask() # pop a task
logging.warning('[%s] Load new task' % taskId) logging.warning('[%s] Load new task' % taskId)
except: # no more task except: # no more task
time.sleep(2) time.sleep(2)
continue continue
threadPool.submit(runCheck, taskId, taskInfo) threadPool.submit(runCheck, taskId, taskInfo) # submit into thread pool
logging.warning('ProxyC starts running (%s)' % Version) logging.warning('ProxyC starts running (%s)' % Constant.Version)
_thread.start_new_thread(startCompile, ('/usr', )) # python compile (generate .pyc file) _thread.start_new_thread(pythonCompile, ('/usr', )) # python compile (generate .pyc file)
_thread.start_new_thread(DnsProxy.start, (dnsServers, 53)) # start dns server _thread.start_new_thread(DnsProxy.start, (Constant.DnsServer, 53)) # start dns server
_thread.start_new_thread(loopCheck, ()) # start loop check _thread.start_new_thread(loopCheck, ()) # start loop check
startServer(apiToken = '') # start api server Api.startServer(apiToken = Constant.ApiToken) # start api server

Loading…
Cancel
Save