From 5e5e208c270098522eaeb54433fab9890f9f4212 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 30 Jul 2022 22:28:01 +0800 Subject: [PATCH] feat: builder of Trojan-Go --- Builder/TrojanGo.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ Builder/__init__.py | 2 ++ demo.py | 24 ++++++++++++++++- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 Builder/TrojanGo.py diff --git a/Builder/TrojanGo.py b/Builder/TrojanGo.py new file mode 100644 index 0000000..2e78543 --- /dev/null +++ b/Builder/TrojanGo.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import json + + +def sslConfig(proxyInfo: dict) -> dict: + return {**{ + 'verify': proxyInfo['verify'] + }, **({} if proxyInfo['sni'] == '' else { + 'sni': proxyInfo['sni'] + }), **({} if proxyInfo['alpn'] is None else { + 'alpn': proxyInfo['alpn'].split(',') + })} + + +def wsConfig(proxyInfo: dict) -> dict: + if proxyInfo['ws'] is None: + return {'enabled': False} + wsObject = { + 'enabled': True, + 'path': proxyInfo['ws']['path'] + } + if proxyInfo['ws']['host'] != '': + wsObject['host'] = proxyInfo['ws']['host'] + return wsObject + + +def ssConfig(proxyInfo: dict) -> dict: + return {**{ + 'enabled': False if proxyInfo['ss'] is None else True + }, **({} if proxyInfo['ss'] is None else { + 'method': proxyInfo['ss']['method'], + 'password': proxyInfo['ss']['passwd'], + })} + + +def pluginConfig(proxyInfo: dict) -> dict: + return {**{ + 'enabled': False if proxyInfo['plugin'] is None else True + }, **({} if proxyInfo['plugin'] is None else { + 'type': 'shadowsocks', + 'command': proxyInfo['plugin']['type'], + 'option': proxyInfo['plugin']['param'], + })} + + +def load(proxyInfo: dict, socksInfo: dict, configFile: str) -> tuple[list, str, dict]: + trojanGoConfig = { + 'run_type': 'client', + 'local_addr': socksInfo['addr'], + 'local_port': socksInfo['port'], + 'remote_addr': proxyInfo['server'], + 'remote_port': proxyInfo['port'], + 'password': [ + proxyInfo['passwd'] + ], + 'log_level': 0, # 0 -> debug level + 'ssl': sslConfig(proxyInfo), + 'websocket': wsConfig(proxyInfo), + 'shadowsocks': ssConfig(proxyInfo), + 'transport_plugin': pluginConfig(proxyInfo) + } + return ['trojan-go', '-config', configFile], json.dumps(trojanGoConfig), {} diff --git a/Builder/__init__.py b/Builder/__init__.py index f63116c..5c34342 100644 --- a/Builder/__init__.py +++ b/Builder/__init__.py @@ -7,6 +7,7 @@ import copy from Builder import VMess from Builder import VLESS from Builder import Trojan +from Builder import TrojanGo from Builder import Shadowsocks from Builder import ShadowsocksR @@ -21,6 +22,7 @@ clientEntry = { 'vmess': [VMess.load, '.json'], 'vless': [VLESS.load, '.json'], 'trojan': [Trojan.load, '.json'], + 'trojan-go': [TrojanGo.load, '.json'], } diff --git a/demo.py b/demo.py index c627ceb..3110292 100755 --- a/demo.py +++ b/demo.py @@ -76,11 +76,33 @@ proxyTrojan = { } } +proxyTrojanGo = { + 'server': '127.0.0.1', + 'port': 12345, + 'passwd': 'dnomd343', + 'sni': '343.re', + 'alpn': None, + 'verify': True, + 'ws': { + 'host': '343.re', + 'path': '/test', + }, + 'ss': { + 'method': 'AES-128-GCM', + 'passwd': 'dnomd343', + }, + 'plugin': { + 'type': 'obfs-local', + 'param': 'obfs=http;obfs-host=www.bing.com' + }, +} + # client = Builder('ss', proxySS) # client = Builder('ssr', proxySSR) # client = Builder('vmess', proxyVMess) # client = Builder('vless', proxyVLESS) -client = Builder('trojan', proxyTrojan) +# client = Builder('trojan', proxyTrojan) +client = Builder('trojan-go', proxyTrojanGo) logging.critical(client.id) logging.critical(client.proxyType)