From ea3778acbc07b772a8d498330d49cfcb6624a750 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sun, 31 Jul 2022 13:34:37 +0800 Subject: [PATCH] feat: builder of Brook --- Basis/Functions.py | 13 ++++++++----- Builder/Brook.py | 39 +++++++++++++++++++++++++++++++++++++++ Builder/__init__.py | 2 ++ Tester/Plugin.py | 6 +++--- demo.py | 18 +++++++++++++++++- test.py | 4 ++-- 6 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 Builder/Brook.py diff --git a/Basis/Functions.py b/Basis/Functions.py index 6f748e9..19ab99e 100644 --- a/Basis/Functions.py +++ b/Basis/Functions.py @@ -14,11 +14,14 @@ def md5Sum(data: str, encode: str = 'UTF-8') -> str: return hashlib.md5(data.encode(encoding = encode)).hexdigest() -def ipFormat(ipAddr: str, v6Bracket: bool = False) -> str: - ip = IP(ipAddr) - if v6Bracket and ip.version() == 6: - return '[%s]' % str(ip) # [IPv6] - return str(ip) # IPv4 / IPV6 +def hostFormat(host: str, v6Bracket: bool = False) -> str: + try: + ip = IP(host) + if v6Bracket and ip.version() == 6: + return '[%s]' % str(ip) # [IPv6] + return str(ip) # IPv4 / IPV6 + except: # not ip address + return host def genFlag(length: int = 12) -> str: # generate random task flag diff --git a/Builder/Brook.py b/Builder/Brook.py new file mode 100644 index 0000000..37b1eb5 --- /dev/null +++ b/Builder/Brook.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from Basis.Functions import hostFormat + + +def loadOrigin(proxyInfo: dict) -> list: # origin stream + return ['client'] + [ + '--server', '%s:%i' % (hostFormat(proxyInfo['server'], v6Bracket = True), proxyInfo['port']), + '--password', proxyInfo['passwd'], + ] + (['--udpovertcp'] if proxyInfo['stream']['uot'] else []) + + +def loadWebsocket(proxyInfo: dict) -> list: + isTls = proxyInfo['stream']['secure'] is not None + wsAddress = (('wss' if isTls else 'ws') + '://%s:%i%s') % ( + hostFormat(proxyInfo['stream']['host'], v6Bracket = True), proxyInfo['port'], proxyInfo['stream']['path'] + ) + brookCommand = [ + 'wssclient' if isTls else 'wsclient', + '--address', '%s:%i' % (hostFormat(proxyInfo['server'], v6Bracket = True), proxyInfo['port']), + '--password', proxyInfo['passwd'], + ] + (['--withoutBrookProtocol'] if proxyInfo['stream']['raw'] else []) + if not isTls: + return brookCommand + ['--wsserver', wsAddress] + return brookCommand + ['--wssserver', wsAddress] + ( + [] if proxyInfo['stream']['secure']['verify'] else ['--insecure'] + ) + + +def load(proxyInfo: dict, socksInfo: dict, configFile: str) -> tuple[list, str, dict]: + brookCommand = ['brook', '--debug', '--listen', ':'] + { # debug module listen on random port + 'origin': loadOrigin, + 'ws': loadWebsocket, + }[proxyInfo['stream']['type']](proxyInfo) + [ + '--socks5', '%s:%i' % (hostFormat(socksInfo['addr'], v6Bracket = True), socksInfo['port']) + ] + print(brookCommand) + return brookCommand, 'Config file %s no need' % configFile, {} diff --git a/Builder/__init__.py b/Builder/__init__.py index 5c34342..322c26d 100644 --- a/Builder/__init__.py +++ b/Builder/__init__.py @@ -4,6 +4,7 @@ import os import copy +from Builder import Brook from Builder import VMess from Builder import VLESS from Builder import Trojan @@ -23,6 +24,7 @@ clientEntry = { 'vless': [VLESS.load, '.json'], 'trojan': [Trojan.load, '.json'], 'trojan-go': [TrojanGo.load, '.json'], + 'brook': [Brook.load, ''], } diff --git a/Tester/Plugin.py b/Tester/Plugin.py index 3c3b559..a48fc6d 100644 --- a/Tester/Plugin.py +++ b/Tester/Plugin.py @@ -8,7 +8,7 @@ from Basis.Logger import logging from Basis.Process import Process from Basis.Methods import plugins from Basis.Functions import genFlag -from Basis.Functions import ipFormat +from Basis.Functions import hostFormat from Basis.Functions import getAvailablePort settings = { @@ -260,7 +260,7 @@ def rabbitShadowsocks(server: Process, pluginInfo: dict) -> Process: ssConfig = json.loads(server.file[0]['content']) # modify origin config ssConfig.pop('plugin') # remove plugin option ssConfig.pop('plugin_opts') - rabbitBind = ipFormat(ssConfig['server'], v6Bracket=True) # ipv4 / [ipv6] + rabbitBind = hostFormat(ssConfig['server'], v6Bracket=True) # ipv4 / [ipv6] rabbitPort = ssConfig['server_port'] ssConfig['server'] = '127.0.0.1' # SIP003 use ipv4 localhost for communication ssConfig['server_port'] = int(pluginInfo['server']['param']) # aka ${RABBIT_PORT} @@ -273,7 +273,7 @@ def rabbitShadowsocks(server: Process, pluginInfo: dict) -> Process: def rabbitTrojanGo(server: Process, pluginInfo: dict) -> Process: trojanConfig = json.loads(server.file[0]['content']) # modify origin config - rabbitBind = ipFormat(trojanConfig['local_addr'], v6Bracket=True) # ipv4 / [ipv6] + rabbitBind = hostFormat(trojanConfig['local_addr'], v6Bracket=True) # ipv4 / [ipv6] rabbitPort = trojanConfig['local_port'] trojanConfig['local_addr'] = '127.0.0.1' # SIP003 use ipv4 localhost for communication trojanConfig['local_port'] = int(pluginInfo['server']['param']) # aka ${RABBIT_PORT} diff --git a/demo.py b/demo.py index 3110292..243d843 100755 --- a/demo.py +++ b/demo.py @@ -97,12 +97,28 @@ proxyTrojanGo = { }, } +proxyBrook = { + 'server': '::1', + 'port': 12345, + 'passwd': 'dnomd343', + 'stream': { + 'type': 'ws', + 'host': '343.re', + 'path': '/test', + 'raw': True, + 'secure': { + 'verify': False + }, + } +} + # client = Builder('ss', proxySS) # client = Builder('ssr', proxySSR) # client = Builder('vmess', proxyVMess) # client = Builder('vless', proxyVLESS) # client = Builder('trojan', proxyTrojan) -client = Builder('trojan-go', proxyTrojanGo) +# client = Builder('trojan-go', proxyTrojanGo) +client = Builder('brook', proxyBrook) logging.critical(client.id) logging.critical(client.proxyType) diff --git a/test.py b/test.py index 4033e53..1bc2e00 100755 --- a/test.py +++ b/test.py @@ -13,7 +13,7 @@ from Tester import Shadowsocks from Tester import ShadowsocksR from Basis.Logger import logging -from Basis.Functions import ipFormat +from Basis.Functions import hostFormat from Basis.Functions import checkPortStatus @@ -39,7 +39,7 @@ def test(testObj: dict) -> None: errFlag = False socks5 = '%s:%i' % ( - ipFormat(testObj['socks']['addr'], v6Bracket = True), + hostFormat(testObj['socks']['addr'], v6Bracket = True), testObj['socks']['port'] ) try: