diff --git a/ProxyBuilder/Brook.py b/ProxyBuilder/Brook.py new file mode 100644 index 0000000..044525a --- /dev/null +++ b/ProxyBuilder/Brook.py @@ -0,0 +1,52 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +def __originConfig(proxyInfo: dict) -> list: + return [ + 'client', + '--server', proxyInfo['server'] + ':' + str(proxyInfo['port']), + '--password', proxyInfo['passwd'] + ] + +def __wsConfig(proxyInfo: dict) -> list: + return [ + 'wsclient', + '--wsserver', 'ws://' + proxyInfo['ws']['host'] + ':' + str(proxyInfo['port']) + proxyInfo['ws']['path'], + '--address', proxyInfo['server'] + ':' + str(proxyInfo['port']), + '--password', proxyInfo['passwd'] + ] + +def __wssConfig(proxyInfo: dict) -> list: + wssConfig = [ + 'wssclient', + '--wssserver', 'wss://' + proxyInfo['ws']['host'] + ':' + str(proxyInfo['port']) + proxyInfo['ws']['path'], + '--address', proxyInfo['server'] + ':' + str(proxyInfo['port']), + '--password', proxyInfo['passwd'] + ] + if not proxyInfo['ws']['secure']['verify']: + wssConfig += ['--insecure'] + return wssConfig + +def load(proxyInfo: dict, socksPort: int, configFile: str) -> tuple[list, str or None, dict]: + """ + Brook配置载入 + proxyInfo: 节点信息 + socksPort: 本地通讯端口 + configFile: 配置文件路径 + + return startCommand, fileContent, envVar + """ + command = [ + 'brook', + '--debug', '--listen', 'skip success', # debug on + ] + if proxyInfo['ws'] is None: + command += __originConfig(proxyInfo) # original mode + elif proxyInfo['ws']['secure'] is None: + command += __wsConfig(proxyInfo) # ws mode + else: + command += __wssConfig(proxyInfo) # wss mode + command += [ + '--socks5', '127.0.0.1:' + str(socksPort) + ] + return command, None, {} diff --git a/ProxyBuilder/builder.py b/ProxyBuilder/builder.py index 938b586..5206eb4 100644 --- a/ProxyBuilder/builder.py +++ b/ProxyBuilder/builder.py @@ -15,6 +15,7 @@ from ProxyBuilder import VMess from ProxyBuilder import VLESS from ProxyBuilder import Trojan from ProxyBuilder import TrojanGo +from ProxyBuilder import Brook libcPaths = [ '/usr/lib/libc.so.6', # CentOS @@ -122,6 +123,8 @@ def build(proxyInfo: dict, configDir: str, clientObj = Trojan elif proxyInfo['type'] == 'trojan-go': # Trojan-Go节点 clientObj = TrojanGo + elif proxyInfo['type'] == 'brook': # Brook节点 + clientObj = Brook else: # 未知类型 return False, 'Unknown proxy type' @@ -132,8 +135,9 @@ def build(proxyInfo: dict, configDir: str, return False, 'Format error with ' + str(proxyInfo['type']) try: - with open(configFile, 'w') as fileObject: # 保存配置文件 - fileObject.write(fileContent) + if fileContent is not None: + with open(configFile, 'w') as fileObject: # 保存配置文件 + fileObject.write(fileContent) except: # 配置文件写入失败 raise Exception('Unable write to file ' + str(configFile)) @@ -161,7 +165,7 @@ def build(proxyInfo: dict, configDir: str, return True, { # 返回连接参数 'flag': taskFlag, 'port': socksPort, - 'file': configFile, + 'file': configFile if fileContent is not None else None, 'process': process } @@ -202,6 +206,8 @@ def destroy(client: dict) -> bool: try: file = client['file'] + if file is None: # 无配置文件 + return True if os.path.exists(file) and os.path.isfile(file): os.remove(file) # 删除配置文件 return True # 销毁成功 diff --git a/ProxyFilter/Brook.py b/ProxyFilter/Brook.py index da766cf..47b8786 100644 --- a/ProxyFilter/Brook.py +++ b/ProxyFilter/Brook.py @@ -83,8 +83,13 @@ def brookFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: try: if not isExtra: # 去除非必要参数 brookFilterRules['rootObject'].pop('remark') - return baseFunc.ruleFilter(rawInfo, brookFilterRules, { + status, result = baseFunc.ruleFilter(rawInfo, brookFilterRules, { 'type': 'brook' }) + if not status: # 节点格式错误 + return False, result + if result['ws'] is not None and result['ws']['host'] == '': + result['ws']['host'] = result['server'] + return True, result except: return False, 'Unknown error' diff --git a/demo.py b/demo.py index 494645a..ebf6a6b 100644 --- a/demo.py +++ b/demo.py @@ -10,15 +10,26 @@ info = { 'server': '127.0.0.1', 'port': '12345', 'passwd': 'dnomd343', - 'ws': { - 'host': 'local.343.re', - 'path': '/test', - 'secure': { - 'verify': False - } - } + # 'ws': { + # 'host': 'local.343.re', + # 'path': '/test', + # 'secure': { + # 'verify': False + # } + # } } status, ret = Filter.filte(info) print(status) print(ret) + +# print() +# status, ret = Builder.build(ret, '/tmp/ProxyC') +# print(status) +# print(ret) + +data = Checker.proxyTest({ + 'check': ['http'], + 'info': ret +}) +print(data)