From e64e41dac61ccc98430199e9dccd229eadca0a0d Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 1 Mar 2022 19:00:27 +0800 Subject: [PATCH] feat: Builder of Trojan-Go --- ProxyBuilder/TrojanGo.py | 76 ++++++++++++++++++++++++++++++++++++++++ ProxyBuilder/builder.py | 3 ++ demo.py | 34 +++++++++++------- 3 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 ProxyBuilder/TrojanGo.py diff --git a/ProxyBuilder/TrojanGo.py b/ProxyBuilder/TrojanGo.py new file mode 100644 index 0000000..bfe62f4 --- /dev/null +++ b/ProxyBuilder/TrojanGo.py @@ -0,0 +1,76 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +import json + +def __tlsConfig(proxyInfo: dict) -> dict: + tlsConfig = { + 'verify': proxyInfo['verify'] + } + if proxyInfo['sni'] != '': + tlsConfig['sni'] = proxyInfo['sni'] + if proxyInfo['alpn'] is not None: + tlsConfig['alpn'] = proxyInfo['alpn'].split(',') + return tlsConfig + +def __wsConfig(proxyInfo: dict) -> dict: + if proxyInfo['ws'] is None: + return { + 'enabled': False + } + wsConfig = { + 'enabled': True, + 'path': proxyInfo['ws']['path'] + } + if proxyInfo['ws']['host'] != '': + wsConfig['host'] = proxyInfo['ws']['host'] + return wsConfig + +def __ssConfig(proxyInfo: dict) -> dict: + if proxyInfo['ss'] is None: + return { + 'enabled': False + } + return { + 'enabled': True, + 'method': proxyInfo['ss']['method'], + 'password': proxyInfo['ss']['passwd'] + } + +def __pluginConfig(proxyInfo: dict) -> dict: + if proxyInfo['plugin'] is None: + return { + 'enabled': False + } + return { + 'enabled': True, + 'type': 'shadowsocks', + 'command': proxyInfo['plugin']['type'], + 'option': proxyInfo['plugin']['param'] + } + +def load(proxyInfo: dict, socksPort: int, configFile: str) -> tuple[list, str, dict]: + """ + Trojan-Go配置载入 + proxyInfo: 节点信息 + socksPort: 本地通讯端口 + configFile: 配置文件路径 + + return startCommand, fileContent, envVar + """ + config = { + 'run_type': 'client', + 'local_addr': '127.0.0.1', + 'local_port': socksPort, + 'remote_addr': proxyInfo['server'], + 'remote_port': proxyInfo['port'], + 'password': [ + proxyInfo['passwd'] + ], + 'log_level': 0, + 'ssl': __tlsConfig(proxyInfo), + 'websocket': __wsConfig(proxyInfo), + 'shadowsocks': __ssConfig(proxyInfo), + 'transport_plugin': __pluginConfig(proxyInfo) + } + return ['trojan-go', '-config', configFile], json.dumps(config), {} diff --git a/ProxyBuilder/builder.py b/ProxyBuilder/builder.py index 775834d..4baeff4 100644 --- a/ProxyBuilder/builder.py +++ b/ProxyBuilder/builder.py @@ -13,6 +13,7 @@ from ProxyBuilder import ShadowsocksR from ProxyBuilder import VMess from ProxyBuilder import VLESS from ProxyBuilder import Trojan +from ProxyBuilder import TrojanGo libcPaths = [ '/usr/lib64/libc.so.6', # CentOS @@ -107,6 +108,8 @@ def build(proxyInfo: dict, configDir: str, clientObj = VLESS elif proxyInfo['type'] == 'trojan': # Trojan节点 clientObj = Trojan + elif proxyInfo['type'] == 'trojan-go': # Trojan-Go节点 + clientObj = TrojanGo else: # 未知类型 return False, 'Unknown proxy type' diff --git a/demo.py b/demo.py index bb5de02..3bc98e4 100644 --- a/demo.py +++ b/demo.py @@ -1,3 +1,4 @@ +import ProxyBuilder as Builder import ProxyDecoder as Decoder import ProxyFilter as Filter import Check as Checker @@ -10,20 +11,29 @@ info = { 'sni': 'local.343.re', 'alpn': 'h2', 'verify': False, - 'ws': { - 'host': 'local.343.re', - 'path': '/test' - }, - 'ss': { - 'method': 'chacha20-ietf-poly1305', - 'passwd': 'dnomd343' - }, - 'plugin': { - 'type': 'obfs-local', - 'param': 'obfs=http' - } + # 'ws': { + # 'host': 'local.343.re', + # 'path': '/test' + # }, + # 'ss': { + # 'method': 'chacha20-ietf-poly1305', + # 'passwd': 'dnomd343' + # }, + # 'plugin': { + # 'type': 'obfs-local', + # 'param': 'obfs=http' + # } } status, ret = Filter.filte(info, isExtra = True) print(status) print(ret) + +# Builder.build(ret, '/tmp/ProxyC') + +data = Checker.proxyTest({ + 'check': ['http'], + 'info': ret +}) + +print(data)