From 357db78dd06072def7bcd2e59c41285c9c166bfc Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 15 Feb 2022 20:25:06 +0800 Subject: [PATCH] update: new check workflow --- check.py => Checker.py | 73 +++++++++----------------------------- ProxyChecker/Http.py | 2 +- Run.py | 80 ++++++++++++++++++++++++++++++++++++++++++ decode.py | 12 ------- demo.py | 28 --------------- 5 files changed, 98 insertions(+), 97 deletions(-) rename check.py => Checker.py (55%) create mode 100644 Run.py delete mode 100644 decode.py delete mode 100644 demo.py diff --git a/check.py b/Checker.py similarity index 55% rename from check.py rename to Checker.py index 8f46bc8..a12d91a 100644 --- a/check.py +++ b/Checker.py @@ -3,18 +3,11 @@ import os import time -import json -import redis import ProxyBuilder as Builder import ProxyChecker as Checker -workDir = '/tmp/ProxyC' -redisPrefix = 'proxyc-' -redisHost = 'localhost' -redisPort = 6379 - -def loadDir(folderPath): # 创建文件夹 +def __loadDir(folderPath): # 创建文件夹 try: if os.path.exists(folderPath): # 文件 / 文件夹 存在 if not os.path.isdir(folderPath): # 文件 @@ -25,9 +18,13 @@ def loadDir(folderPath): # 创建文件夹 except: return False -def proxyHttpCheck(socksPort): # Http检测 +def __proxyHttpCheck(socksPort, httpCheckUrl, httpCheckTimeout): # Http检测 try: - health, httpDelay = Checker.httpCheck(socksPort, timeout = 12) + health, httpDelay = Checker.httpCheck( + socksPort, + url = httpCheckUrl, + timeout = httpCheckTimeout + ) if health == None: # 连接失败 return None return { @@ -37,11 +34,17 @@ def proxyHttpCheck(socksPort): # Http检测 except: # 未知错误 return None -def proxyTest(rawInfo, startDelay = 1): +def proxyTest( + rawInfo, + startDelay = 1, + workDir = '/tmp/ProxyC', + httpCheckUrl = 'http://gstatic.com/generate_204', + httpCheckTimeout = 12, + ): ''' 代理检测入口 - 异常错误: + 程序异常: return None 启动失败: @@ -56,7 +59,7 @@ def proxyTest(rawInfo, startDelay = 1): } ''' - if loadDir(workDir) == False: # 工作文件夹无效 + if __loadDir(workDir) == False: # 工作文件夹无效 return None if not 'info' in rawInfo: # 缺少代理服务器信息 return None @@ -91,7 +94,7 @@ def proxyTest(rawInfo, startDelay = 1): checkResult = {} for item in checkItem: if item == 'http': # http检测 - result = proxyHttpCheck(client['port']) + result = __proxyHttpCheck(client['port'], httpCheckUrl, httpCheckTimeout) else: # 未知检测项目 result = None if result == None: # 检测出错 @@ -104,45 +107,3 @@ def proxyTest(rawInfo, startDelay = 1): 'success': True, 'result': checkResult } - -def getTask(): # 获取检测任务 - redisObject = redis.StrictRedis(host = redisHost, port = redisPort, db = 0) - checkList = redisObject.keys(redisPrefix + 'check-a-*') # 优先级排序 - if len(checkList) == 0: - checkList = redisObject.keys(redisPrefix + 'check-b-*') - if len(checkList) == 0: - checkList = redisObject.keys(redisPrefix + 'check-c-*') - if len(checkList) == 0: - checkList = redisObject.keys(redisPrefix + 'check-d-*') - if len(checkList) == 0: - checkList = redisObject.keys(redisPrefix + 'check-e-*') - - if len(checkList) == 0: # 无任务 - return None - key = checkList[0] # 选取首个任务 - taskContent = redisObject.get(key) - redisObject.delete(key) - tag = str(key[len(redisPrefix) + 8:], encoding = "utf-8") - try: - return tag, json.loads(taskContent) # JSON解码 - except: # JSON解码失败 - return tag, None - -def setResult(tag, result): # 写入检测结果 - redisObject = redis.StrictRedis(host = redisHost, port = redisPort, db = 0) - key = redisPrefix + 'result-' + tag - redisObject.set(key, json.dumps(result)) - -def main(): - try: - taskTag, task = getTask() # 获取检测任务 - except: - return - if task == None: - return - try: - setResult(taskTag, proxyTest(task)) # 检测并写入数据库 - except: - pass - -main() diff --git a/ProxyChecker/Http.py b/ProxyChecker/Http.py index c43c512..5eaca0c 100644 --- a/ProxyChecker/Http.py +++ b/ProxyChecker/Http.py @@ -38,7 +38,7 @@ def httpPing(port, url = 'http://gstatic.com/generate_204', timeout = 30): statusCode = httpRequest.status_code # 获取http状态码 except: return None, 'Http request error' - if 'statusCode' in vars() and statusCode == 204: # http测试成功 + if 'statusCode' in vars() and statusCode >= 200 and statusCode < 300: # http测试成功 delay = (time.time_ns() - startTime) / (10 ** 6) return True, round(delay, 2) # 保留小数点后两位 else: diff --git a/Run.py b/Run.py new file mode 100644 index 0000000..85b057b --- /dev/null +++ b/Run.py @@ -0,0 +1,80 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +import json +import redis +import Checker + +def __loadRedis(redisHost = 'localhost', redisPort = 6379): # 连接Redis数据库 + return redis.StrictRedis(host = redisHost, port = redisPort, db = 0) + +def __getCheckInfo(redisObject, redisPrefix): + ''' + 获取检测任务 + + 无任务或发生异常: + return None, None + + 任务格式有误: + return tag, None + + 任务获取成功: + return tag, {...} + ''' + try: + checkList = redisObject.keys(redisPrefix + 'check-a-*') # 优先级排序 + if len(checkList) == 0: + checkList = redisObject.keys(redisPrefix + 'check-b-*') + if len(checkList) == 0: + checkList = redisObject.keys(redisPrefix + 'check-c-*') + if len(checkList) == 0: + checkList = redisObject.keys(redisPrefix + 'check-d-*') + if len(checkList) == 0: + checkList = redisObject.keys(redisPrefix + 'check-e-*') + if len(checkList) == 0: # 无任务 + return None, None + key = checkList[0] # 选取首个任务 + taskContent = redisObject.get(key) # 获取任务信息 + redisObject.delete(key) # 删除任务记录 + tag = str(key[len(redisPrefix) + 8:], encoding = "utf-8") # {prefix}check-x-{tag} + except: + return None, None + try: + return tag, json.loads(taskContent) # JSON解码 + except: # JSON解码失败 + return tag, None + +def __setCheckResult(checkTag, checkResult, redisObject, redisPrefix): # 写入检测结果 + try: + key = redisPrefix + 'result-' + checkTag + redisObject.set(key, json.dumps(checkResult)) + return True + except: + return False + +def main(startDelay, httpCheckUrl, httpCheckTimeout): + redisPrefix = 'proxyc-' + redisObject = __loadRedis() + checkTag, checkInfo = __getCheckInfo(redisObject, redisPrefix) # 获取检测任务 + if checkTag == None: + print("no task found") + return + print(checkInfo) + checkResult = Checker.proxyTest(checkInfo) + if checkResult == None: + print("some bad things happen") + return + elif checkResult['success'] == False: + print("error proxy info") + return + print(checkResult) + if __setCheckResult(checkTag, checkResult, redisObject, redisPrefix) == False: + print("redis write error") + return + print("ok") + +defaultStartDelay = 1 +defaultHttpCheckTimeout = 30 +defaultHttpCheckUrl = 'https://api.v2fly.org/checkConnection.svgz' + +main(defaultStartDelay, defaultHttpCheckUrl, defaultHttpCheckTimeout) diff --git a/decode.py b/decode.py deleted file mode 100644 index 0367b42..0000000 --- a/decode.py +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/python -# -*- coding:utf-8 -*- - -import ProxyDecoder as Decoder - -ssPlainUrl = 'ss://bf-cfb:test@192.168.100.1:8888' -ssCommonUrl = 'ss://YmYtY2ZiOnRlc3RAMTkyLjE2OC4xMDAuMTo4ODg4#example-server' -ssSip002Url = 'ss://cmM0LW1kNTpwYXNzd2Q@192.168.100.1:8888/?plugin=obfs-local%3Bobfs%3Dhttp#Example' - -ssrCommonUrl = 'ssr://ZmU4MDo6MTo2MDA0OmF1dGhfYWVzMTI4X21kNTphZXMtMjU2LWNmYjp0bHMxLjJfdGlja2V0X2F1dGg6Y0dGemMzZGsvP29iZnNwYXJhbT1ZMlUzTUdVeE5EY3dOekF1ZFhCa1lYUmxMbTFwWTNKdmMyOW1kQzVqYjIwJnByb3RvcGFyYW09TVRRM01EY3dPa0pGTTIxck9RJnJlbWFya3M9UlZoQlRWQk1SUSZncm91cD1kR1Z6ZEE' - -print(Decoder.decode(ssrCommonUrl)) diff --git a/demo.py b/demo.py deleted file mode 100644 index 9d942d0..0000000 --- a/demo.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/python -# -*- coding:utf-8 -*- - -import ProxyFilter as Filter - -ssInfo = { - "type": "ss", - "server": "127.0.0.1 ", - "port": 12345, - "password": "dnomd343", - "method": "aes_256-ctr", - "plugin": "obfs", - "pluginParam": "obfs=http;host=www.bing.com" -} - -ssrInfo = { - "type": "ssr", - "server": " 127.0.0.1", - "port": 23456, - "password": "dnomd343", - "method": "table", - "protocol": "auth-aes128_md5", - "protocolParam": "", - "obfs": "", - "obfsParam": "fafadfaf" -} - -print(Filter.filter(ssrInfo))