diff --git a/Basis/Compile.py b/Basis/Compile.py deleted file mode 100644 index ad90c81..0000000 --- a/Basis/Compile.py +++ /dev/null @@ -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)) diff --git a/Basis/Constant.py b/Basis/Constant.py index 15d3f0a..b81e32e 100644 --- a/Basis/Constant.py +++ b/Basis/Constant.py @@ -2,7 +2,14 @@ # -*- coding: utf-8 -*- Version = 'dev' +ApiToken = '' +# ApiToken = 'dnomd343' +# LogLevel = 'DEBUG' +LogLevel = 'WARNING' +LogFile = 'runtime.log' 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' # Shadowsocks Info diff --git a/Basis/Logger.py b/Basis/Logger.py index 0069306..cf8f9b8 100644 --- a/Basis/Logger.py +++ b/Basis/Logger.py @@ -4,18 +4,23 @@ import sys import logging 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( level = logLevel, format = logFormat, datefmt = dateFormat, - filename = logFile, + filename = LogFile, ) logHandler = logging.StreamHandler(stream = sys.stdout) logHandler.setFormatter(ColoredFormatter( @@ -30,11 +35,3 @@ logHandler.setFormatter(ColoredFormatter( } )) logging.getLogger().addHandler(logHandler) - - -if __name__ == '__main__': - logging.debug('debug') - logging.info('info') - logging.warning('warn') - logging.error('error') - logging.critical('critical') diff --git a/Basis/Manager.py b/Basis/Manager.py index 691631f..3d272be 100644 --- a/Basis/Manager.py +++ b/Basis/Manager.py @@ -78,7 +78,7 @@ class Task(object): task['status'] = self.__TASK_RUNNING # set task status as running logging.info('Manager pop task [%s] -> %s' % (taskId, 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') def finishTask(self, taskId: str, taskData: dict) -> None: # update task data when completed diff --git a/Basis/Test.py b/Basis/Test.py index e23c8ef..83f4523 100644 --- a/Basis/Test.py +++ b/Basis/Test.py @@ -6,12 +6,12 @@ import time import requests from threading import Thread from Basis.Logger import logging -from Basis.Constant import WorkDir +from Basis.Constant import WorkDir, ObfsSite from Basis.Functions import md5Sum, genFlag, hostFormat, checkPortStatus Settings = { 'workDir': WorkDir, - 'site': 'www.bing.com', + 'site': ObfsSite, 'serverBind': '', 'clientBind': '', 'host': '', diff --git a/main.py b/main.py index 6bdada4..36b3622 100755 --- a/main.py +++ b/main.py @@ -1,41 +1,44 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- + import time import _thread -from Basis import DnsProxy +import compileall +from Basis import Constant from Basis.Check import Check +from Basis import Api, DnsProxy from Basis.Logger import logging 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 -# 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: - checkResult = Check(taskId, taskInfo) + checkResult = Check(taskId, taskInfo) # check by task info logging.warning('[%s] Task finish' % taskId) - Manager.finishTask(taskId, checkResult) + Manager.finishTask(taskId, checkResult) # commit check result def loopCheck(threadNum: int = 16) -> None: - threadPool = ThreadPoolExecutor(max_workers = threadNum) + threadPool = ThreadPoolExecutor(max_workers = threadNum) # init thread pool while True: try: - taskId, taskInfo = Manager.popTask() + taskId, taskInfo = Manager.popTask() # pop a task logging.warning('[%s] Load new task' % taskId) except: # no more task time.sleep(2) continue - threadPool.submit(runCheck, taskId, taskInfo) + threadPool.submit(runCheck, taskId, taskInfo) # submit into thread pool -logging.warning('ProxyC starts running (%s)' % Version) -_thread.start_new_thread(startCompile, ('/usr', )) # python compile (generate .pyc file) -_thread.start_new_thread(DnsProxy.start, (dnsServers, 53)) # start dns server +logging.warning('ProxyC starts running (%s)' % Constant.Version) +_thread.start_new_thread(pythonCompile, ('/usr', )) # python compile (generate .pyc file) +_thread.start_new_thread(DnsProxy.start, (Constant.DnsServer, 53)) # start dns server _thread.start_new_thread(loopCheck, ()) # start loop check -startServer(apiToken = '') # start api server +Api.startServer(apiToken = Constant.ApiToken) # start api server