From f462c02dba08c3178664f111c08b4bc0bd0b9f37 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 24 Feb 2022 09:18:11 +0800 Subject: [PATCH] feat: support VMess build --- ProxyBuilder/VMess.py | 112 +++++++++++++++++++++++++++++------------- demo.py | 22 --------- 2 files changed, 77 insertions(+), 57 deletions(-) delete mode 100644 demo.py diff --git a/ProxyBuilder/VMess.py b/ProxyBuilder/VMess.py index 2827fb7..e272abb 100644 --- a/ProxyBuilder/VMess.py +++ b/ProxyBuilder/VMess.py @@ -5,6 +5,40 @@ import json logLevel = 'warning' +httpHeader = { + 'type': 'http', + 'request': { + 'version': '1.1', + 'method': 'GET', + 'path': [], + 'headers': { + 'Host': [], + 'User-Agent': [ + 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36', + 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/53.0.2785.109 Mobile/14A456 Safari/601.1.46' + ], + 'Accept-Encoding': [ + 'gzip, deflate' + ], + 'Connection': [ + 'keep-alive' + ], + 'Pragma': 'no-cache' + } + } +} + +kcpSettings = { + 'mtu': 1350, + 'tti': 50, + 'uplinkCapacity': 12, + 'downlinkCapacity': 100, + 'congestion': False, + 'readBufferSize': 2, + 'writeBufferSize': 2, + 'header': {} +} + def __baseConfig(socksPort: int, outboundObject: dict) -> dict: # v2ray配置生成 return { 'log': { @@ -29,24 +63,33 @@ def __baseConfig(socksPort: int, outboundObject: dict) -> dict: # v2ray配置生 def __secureConfig(secureInfo: dict or None) -> dict: # TLS加密传输配置 if secureInfo is None: return {} + tlsObject = { + 'allowInsecure': not secureInfo['verify'], + 'alpn': secureInfo['alpn'].split(',') + } + if secureInfo['sni'] != '': + tlsObject['serverName'] = secureInfo['sni'] return { 'security': 'tls', - 'tlsSettings': { - 'allowInsecure': not secureInfo['verify'] - # sni - # alpn - } + 'tlsSettings': tlsObject } def __tcpConfig(streamInfo: dict) -> dict: # TCP传输方式配置 tcpObject = {} + if streamInfo['obfs'] is not None: + httpHeader['request']['path'].append(streamInfo['obfs']['path']) + httpHeader['request']['headers']['Host'] = streamInfo['obfs']['host'].split(',') + tcpObject['header'] = httpHeader return {**{ 'network': 'tcp', 'tcpSettings': tcpObject }, **__secureConfig(streamInfo['secure'])} def __kcpConfig(streamInfo: dict) -> dict: # mKCP传输方式配置 - kcpObject = {} + kcpObject = kcpSettings + kcpObject['header']['type'] = streamInfo['obfs'] + if streamInfo['seed'] is not None: + kcpObject['seed'] = streamInfo['seed'] return {**{ 'network': 'kcp', 'kcpSettings': kcpObject @@ -54,30 +97,48 @@ def __kcpConfig(streamInfo: dict) -> dict: # mKCP传输方式配置 def __wsConfig(streamInfo: dict) -> dict: # WebSocket传输方式配置 wsObject = {} + if streamInfo['path'] != '': + wsObject['path'] = streamInfo['path'] + if streamInfo['host'] != '': + wsObject['headers'] = {} + wsObject['headers']['Host'] = streamInfo['host'] + if streamInfo['ed'] is not None: + wsObject['maxEarlyData'] = streamInfo['ed'] + wsObject['earlyDataHeaderName'] = 'Sec-WebSocket-Protocol' return {**{ 'network': 'ws', 'wsSettings': wsObject }, **__secureConfig(streamInfo['secure'])} def __h2Config(streamInfo: dict) -> dict: # HTTP/2传输方式配置 - h2Object = {} + h2Object = { + 'path': streamInfo['path'] + } + if streamInfo['host'] != '': + h2Object['host'] = streamInfo['host'].split(',') return {**{ 'network': 'h2', 'httpSettings': h2Object }, **__secureConfig(streamInfo['secure'])} def __quicConfig(streamInfo: dict) -> dict: # QUIC传输方式配置 - quicObject = {} return {**{ 'network': 'quic', - 'quicSettings': quicObject + 'quicSettings': { + 'security': streamInfo['method'], + 'key': streamInfo['passwd'], + 'header': { + 'type': streamInfo['obfs'] + } + } }, **__secureConfig(streamInfo['secure'])} def __grpcConfig(streamInfo: dict) -> dict: # gRPC传输方式配置 - grpcObject = {} return {**{ 'network': 'grpc', - 'grpcSettings': grpcObject + 'grpcSettings': { + 'serviceName': streamInfo['service'] + } }, **__secureConfig(streamInfo['secure'])} def __vmessConfig(proxyInfo: dict) -> dict: # VMess节点配置 @@ -129,27 +190,8 @@ def load(proxyInfo: dict, socksPort: int, configFile: str) -> tuple[list or None 载入成功: return startCommand, fileContent, envVar """ - - config = __baseConfig(socksPort, __vmessConfig(proxyInfo)) - - return ['v2ray', '-c', configFile], json.dumps(config), {} - -info = { - 'server': '1.1.1.1', - 'port': 12345, - 'method': 'aes-128-gcm', - 'id': 'eb6273f1-a98f-59f6-ba52-945f11dee100', - 'aid': 64, - 'stream': { - 'type': 'tcp', - 'obfs': None, - 'secure': { - 'sni': '', - 'alpn': 'h2,http/1.1', - 'verify': True - } - } -} - -ret = load(info, 1080, '/tmp/ProxyC/test.json') -print(ret[1]) + try: + config = __baseConfig(socksPort, __vmessConfig(proxyInfo)) + return ['v2ray', '-c', configFile], json.dumps(config), {} + except: + return None, None, None diff --git a/demo.py b/demo.py deleted file mode 100644 index 06b5d9c..0000000 --- a/demo.py +++ /dev/null @@ -1,22 +0,0 @@ -import ProxyFilter as Filter - -info = { - 'type': 'ss', - 'remark': 'ok', - 'server': b'127.0.0.1 ', - 'port': b"12345", - 'method': 'aes-128_gcm', - 'passwd': 'dnomd343', - 'protocol': 'auth_chain-a', - 'protocolParam': '123', - # 'obfs': b"http-simple", - 'obfsParam': b'123', - 'plugin': { - 'type': 'OBFS_server', - 'param': 'obfs=tls' - } -} - -status, data = Filter.filte(info, isExtra = True) -print(status) -print(data)