From 842864501d9b0f82be896c3b06f9bda7daf6a04b Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 7 Sep 2022 17:26:48 +0800 Subject: [PATCH] fix: proxyc test process --- Checker/Http.py | 12 ++++++------ Checker/__init__.py | 4 ++-- {Basis => Utils}/Check.py | 22 +++++++++++----------- Utils/Logger.py | 2 +- main.py | 5 +++-- 5 files changed, 23 insertions(+), 22 deletions(-) rename {Basis => Utils}/Check.py (70%) diff --git a/Checker/Http.py b/Checker/Http.py index d25f51b..bfffc05 100644 --- a/Checker/Http.py +++ b/Checker/Http.py @@ -3,27 +3,27 @@ import time import requests -from Basis.Logger import logging -from Basis.Functions import hostFormat +from Utils.Logger import logger +from Utils.Common import hostFormat def httpPing(taskId: str, url: str, socksAddr: str, socksPort: int, timeout: int) -> float: try: startTime = time.time_ns() # request start time socksProxy = 'socks5://%s:%i' % (hostFormat(socksAddr, v6Bracket = True), socksPort) - logging.debug('[%s] Http ping -> request %s via %s' % (taskId, url, socksProxy)) + logger.debug('[%s] Http ping -> request %s via %s' % (taskId, url, socksProxy)) httpRequest = requests.get(url, proxies = { # send http request by socks5 proxy 'http': socksProxy, 'https': socksProxy, }, timeout = timeout) except: # something error on request process (timeout or proxy not working) - logging.debug('[%s] Http ping -> request error' % taskId) + logger.debug('[%s] Http ping -> request error' % taskId) return -1 if httpRequest.status_code not in range(200, 300): # http code not 2xx - logging.debug('[%s] Http ping -> status code %i not expected' % (taskId, httpRequest.status_code)) + logger.debug('[%s] Http ping -> status code %i not expected' % (taskId, httpRequest.status_code)) return -1 delay = (time.time_ns() - startTime) / (10 ** 6) # ns -> ms - logging.debug('[%s] Http ping -> delay %f ms' % (taskId, delay)) + logger.debug('[%s] Http ping -> delay %f ms' % (taskId, delay)) return round(delay, 2) # two decimal places diff --git a/Checker/__init__.py b/Checker/__init__.py index 264ebee..72cb729 100644 --- a/Checker/__init__.py +++ b/Checker/__init__.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import logging from Checker import Http +from Utils.Logger import logger checkEntry = { 'http': Http.check @@ -23,7 +23,7 @@ def formatCheck(rawInfo: list) -> dict: def Checker(taskId: str, checkInfo: dict, socksInfo: dict) -> dict: diffItems = {x for x in checkInfo} - {x for x in checkEntry} if len(diffItems) != 0: # include unknown check items - logging.error('[%s] Unknown check items -> %s' % (taskId, diffItems)) + logger.error('[%s] Unknown check items -> %s' % (taskId, diffItems)) raise RuntimeError('Unknown check items') result = {} for checkItem, checkOptions in checkInfo.items(): diff --git a/Basis/Check.py b/Utils/Check.py similarity index 70% rename from Basis/Check.py rename to Utils/Check.py index 597aa6f..4d7cdab 100644 --- a/Basis/Check.py +++ b/Utils/Check.py @@ -4,10 +4,10 @@ import copy import time from Checker import Checker -from Basis.Logger import logging +from Utils.Logger import logger +from Utils.Common import isVacantPort from Builder import Builder, clientEntry -from Basis.Exception import checkException -from Basis.Functions import checkPortStatus +from Utils.Exception import checkException def buildClient(taskId: str, taskInfo: dict) -> Builder: @@ -19,36 +19,36 @@ def buildClient(taskId: str, taskInfo: dict) -> Builder: taskId = taskId, ) except Exception as reason: - logging.error('[%s] Client build error -> %s' % (taskId, reason)) + logger.error('[%s] Client build error -> %s' % (taskId, reason)) raise checkException('Client build error') def waitClient(taskId: str, client: Builder, times: int = 150, delay: int = 100): # wait until client port occupied for i in range(times): - if not checkPortStatus(client.socksPort): # port occupied + if not isVacantPort(client.socksPort): # port occupied break time.sleep(delay / 1000) # wait in default: 100ms * 150 => 15s time.sleep(1) # wait a short time before check process if not client.status(): # client unexpected exit - logging.warning('[%s] Client unexpected exit' % taskId) + logger.warning('[%s] Client unexpected exit' % taskId) client.destroy() # remove file and kill sub process - logging.debug('[%s] Client output\n%s', (taskId, client.output)) + logger.debug('[%s] Client output\n%s', (taskId, client.output)) raise checkException('Client unexpected exit') def Check(taskId: str, taskInfo: dict) -> dict: - logging.info('[%s] Start checking process -> %s' % (taskId, taskInfo)) + logger.info('[%s] Start checking process -> %s' % (taskId, taskInfo)) if taskInfo['type'] not in clientEntry: # unknown proxy type - logging.error('[%s] Unknown proxy type %s' % (taskId, taskInfo['type'])) + logger.error('[%s] Unknown proxy type %s' % (taskId, taskInfo['type'])) raise checkException('Unknown proxy type') client = buildClient(taskId, taskInfo) # build proxy client - logging.info('[%s] Client loaded successfully' % taskId) + logger.info('[%s] Client loaded successfully' % taskId) waitClient(taskId, client) # wait for the client to start checkResult = Checker(taskId, taskInfo['check'], { # start check process 'addr': client.socksAddr, 'port': client.socksPort, }) - logging.info('[%s] Client check result -> %s' % (taskId, checkResult)) + logger.info('[%s] Client check result -> %s' % (taskId, checkResult)) client.destroy() # clean up client taskInfo = copy.deepcopy(taskInfo) taskInfo.pop('check') # remove check items diff --git a/Utils/Logger.py b/Utils/Logger.py index d5f7c99..6ac532d 100644 --- a/Utils/Logger.py +++ b/Utils/Logger.py @@ -42,7 +42,7 @@ stdHandler.setFormatter(colorlog.ColoredFormatter( log_colors = logColor, stream = sys.stderr )) -stdHandler.setLevel(LogLevel) # custom level for stderr +stdHandler.setLevel(logLevel) # custom level for stderr logger = logging.getLogger() logger.addHandler(stdHandler) diff --git a/main.py b/main.py index 3754dd8..c1f7470 100755 --- a/main.py +++ b/main.py @@ -63,7 +63,8 @@ else: from Tester import testEntry from Utils.Check import Check -from Utils import Api, DnsProxy +# from Utils import Api, DnsProxy +from Utils import DnsProxy from Utils.Logger import logger from Utils.Manager import Manager from Utils.Test import Test, loadBind, loadCert @@ -137,4 +138,4 @@ logger.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(loop, (Constant.CheckThread, )) # start check loop -Api.startServer() # start api server +# Api.startServer() # start api server