Browse Source

feat: builder of Brook

master
dnomd343 2 years ago
parent
commit
ea3778acbc
  1. 7
      Basis/Functions.py
  2. 39
      Builder/Brook.py
  3. 2
      Builder/__init__.py
  4. 6
      Tester/Plugin.py
  5. 18
      demo.py
  6. 4
      test.py

7
Basis/Functions.py

@ -14,11 +14,14 @@ def md5Sum(data: str, encode: str = 'UTF-8') -> str:
return hashlib.md5(data.encode(encoding = encode)).hexdigest() return hashlib.md5(data.encode(encoding = encode)).hexdigest()
def ipFormat(ipAddr: str, v6Bracket: bool = False) -> str: def hostFormat(host: str, v6Bracket: bool = False) -> str:
ip = IP(ipAddr) try:
ip = IP(host)
if v6Bracket and ip.version() == 6: if v6Bracket and ip.version() == 6:
return '[%s]' % str(ip) # [IPv6] return '[%s]' % str(ip) # [IPv6]
return str(ip) # IPv4 / IPV6 return str(ip) # IPv4 / IPV6
except: # not ip address
return host
def genFlag(length: int = 12) -> str: # generate random task flag def genFlag(length: int = 12) -> str: # generate random task flag

39
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, {}

2
Builder/__init__.py

@ -4,6 +4,7 @@
import os import os
import copy import copy
from Builder import Brook
from Builder import VMess from Builder import VMess
from Builder import VLESS from Builder import VLESS
from Builder import Trojan from Builder import Trojan
@ -23,6 +24,7 @@ clientEntry = {
'vless': [VLESS.load, '.json'], 'vless': [VLESS.load, '.json'],
'trojan': [Trojan.load, '.json'], 'trojan': [Trojan.load, '.json'],
'trojan-go': [TrojanGo.load, '.json'], 'trojan-go': [TrojanGo.load, '.json'],
'brook': [Brook.load, ''],
} }

6
Tester/Plugin.py

@ -8,7 +8,7 @@ from Basis.Logger import logging
from Basis.Process import Process from Basis.Process import Process
from Basis.Methods import plugins from Basis.Methods import plugins
from Basis.Functions import genFlag from Basis.Functions import genFlag
from Basis.Functions import ipFormat from Basis.Functions import hostFormat
from Basis.Functions import getAvailablePort from Basis.Functions import getAvailablePort
settings = { settings = {
@ -260,7 +260,7 @@ def rabbitShadowsocks(server: Process, pluginInfo: dict) -> Process:
ssConfig = json.loads(server.file[0]['content']) # modify origin config ssConfig = json.loads(server.file[0]['content']) # modify origin config
ssConfig.pop('plugin') # remove plugin option ssConfig.pop('plugin') # remove plugin option
ssConfig.pop('plugin_opts') ssConfig.pop('plugin_opts')
rabbitBind = ipFormat(ssConfig['server'], v6Bracket=True) # ipv4 / [ipv6] rabbitBind = hostFormat(ssConfig['server'], v6Bracket=True) # ipv4 / [ipv6]
rabbitPort = ssConfig['server_port'] rabbitPort = ssConfig['server_port']
ssConfig['server'] = '127.0.0.1' # SIP003 use ipv4 localhost for communication ssConfig['server'] = '127.0.0.1' # SIP003 use ipv4 localhost for communication
ssConfig['server_port'] = int(pluginInfo['server']['param']) # aka ${RABBIT_PORT} 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: def rabbitTrojanGo(server: Process, pluginInfo: dict) -> Process:
trojanConfig = json.loads(server.file[0]['content']) # modify origin config 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'] rabbitPort = trojanConfig['local_port']
trojanConfig['local_addr'] = '127.0.0.1' # SIP003 use ipv4 localhost for communication trojanConfig['local_addr'] = '127.0.0.1' # SIP003 use ipv4 localhost for communication
trojanConfig['local_port'] = int(pluginInfo['server']['param']) # aka ${RABBIT_PORT} trojanConfig['local_port'] = int(pluginInfo['server']['param']) # aka ${RABBIT_PORT}

18
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('ss', proxySS)
# client = Builder('ssr', proxySSR) # client = Builder('ssr', proxySSR)
# client = Builder('vmess', proxyVMess) # client = Builder('vmess', proxyVMess)
# client = Builder('vless', proxyVLESS) # client = Builder('vless', proxyVLESS)
# client = Builder('trojan', proxyTrojan) # 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.id)
logging.critical(client.proxyType) logging.critical(client.proxyType)

4
test.py

@ -13,7 +13,7 @@ from Tester import Shadowsocks
from Tester import ShadowsocksR from Tester import ShadowsocksR
from Basis.Logger import logging from Basis.Logger import logging
from Basis.Functions import ipFormat from Basis.Functions import hostFormat
from Basis.Functions import checkPortStatus from Basis.Functions import checkPortStatus
@ -39,7 +39,7 @@ def test(testObj: dict) -> None:
errFlag = False errFlag = False
socks5 = '%s:%i' % ( socks5 = '%s:%i' % (
ipFormat(testObj['socks']['addr'], v6Bracket = True), hostFormat(testObj['socks']['addr'], v6Bracket = True),
testObj['socks']['port'] testObj['socks']['port']
) )
try: try:

Loading…
Cancel
Save