mirror of https://github.com/dnomd343/ProxyC
Dnomd343
3 years ago
10 changed files with 647 additions and 5 deletions
@ -0,0 +1,45 @@ |
|||
#!/usr/bin/python |
|||
# -*- coding:utf-8 -*- |
|||
|
|||
import json |
|||
from ProxyBuilder import Xray |
|||
|
|||
def load(proxyInfo: dict, socksPort: int, configFile: str) -> tuple[list, str, dict]: |
|||
""" |
|||
Trojan配置载入 |
|||
proxyInfo: 节点信息 |
|||
socksPort: 本地通讯端口 |
|||
configFile: 配置文件路径 |
|||
|
|||
return startCommand, fileContent, envVar |
|||
""" |
|||
flowType = None |
|||
if proxyInfo['stream']['secure'] is not None and proxyInfo['stream']['secure']['type'] == 'xtls': |
|||
flowType = proxyInfo['stream']['secure']['flow'] |
|||
if flowType == 'xtls-origin': |
|||
flowType = 'xtls-rprx-origin' |
|||
elif flowType == 'xtls-direct': |
|||
flowType = 'xtls-rprx-direct' |
|||
elif flowType == 'xtls-splice': |
|||
flowType = 'xtls-rprx-splice' |
|||
else: |
|||
raise Exception('Unknown XTLS flow') |
|||
if proxyInfo['stream']['secure']['udp443']: |
|||
flowType += '-udp443' |
|||
outboundConfig = { |
|||
'protocol': 'trojan', |
|||
'settings': { |
|||
'servers': [ |
|||
{ |
|||
'address': proxyInfo['server'], |
|||
'port': proxyInfo['port'], |
|||
'password': proxyInfo['passwd'], |
|||
} |
|||
] |
|||
}, |
|||
'streamSettings': Xray.xrayStreamConfig(proxyInfo['stream']) |
|||
} |
|||
if flowType is not None: # 添加XTLS流控类型 |
|||
outboundConfig['settings']['servers'][0]['flow'] = flowType |
|||
config = Xray.baseConfig(socksPort, outboundConfig) # Trojan节点配置 |
|||
return ['xray', '-c', configFile], json.dumps(config), {} |
@ -0,0 +1,84 @@ |
|||
#!/usr/bin/python |
|||
# -*- coding:utf-8 -*- |
|||
|
|||
from ProxyFilter import baseFunc |
|||
from ProxyFilter import Xray |
|||
|
|||
trojanFilterRules = { |
|||
'rootObject': { |
|||
'remark': { |
|||
'optional': False, |
|||
'default': '', |
|||
'type': str, |
|||
'format': baseFunc.toStr |
|||
}, |
|||
'server': { |
|||
'optional': True, |
|||
'type': str, |
|||
'format': baseFunc.toStrTidy, |
|||
'filter': baseFunc.isHost, |
|||
'errMsg': 'Illegal server address' |
|||
}, |
|||
'port': { |
|||
'optional': True, |
|||
'type': int, |
|||
'format': baseFunc.toInt, |
|||
'filter': baseFunc.isPort, |
|||
'errMsg': 'Illegal port number' |
|||
}, |
|||
'passwd': { |
|||
'optional': True, |
|||
'type': str, |
|||
'format': baseFunc.toStr |
|||
}, |
|||
'stream': { |
|||
'optional': False, |
|||
'default': { |
|||
'type': 'tcp' |
|||
}, |
|||
'type': [ |
|||
'tcpObject', |
|||
'kcpObject', |
|||
'wsObject', |
|||
'h2Object', |
|||
'quicObject', |
|||
'grpcObject', |
|||
] |
|||
} |
|||
} |
|||
} |
|||
|
|||
def trojanFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: |
|||
""" |
|||
Trojan节点合法性检查 |
|||
|
|||
不合法: |
|||
return False, {reason} |
|||
|
|||
合法: |
|||
return True, { |
|||
'type': 'trojan', |
|||
... |
|||
} |
|||
""" |
|||
try: |
|||
if not isExtra: # 去除非必要参数 |
|||
trojanFilterRules['rootObject'].pop('remark') |
|||
for key, obj in Xray.xrayStreamRules.items(): # xray.stream -> trojan |
|||
trojanFilterRules[key] = obj |
|||
status, result = baseFunc.ruleFilter(rawInfo, trojanFilterRules, { |
|||
'type': 'trojan' |
|||
}) |
|||
if not status: # 节点格式错误 |
|||
return False, result |
|||
stream = result['stream'] |
|||
if stream['secure'] is not None and stream['secure']['sni'] == '': # 未指定SNI |
|||
if stream['type'] == 'tcp' and stream['obfs'] is not None: |
|||
stream['secure']['sni'] = stream['obfs']['host'].split(',')[0] |
|||
elif stream['type'] == 'ws': |
|||
stream['secure']['sni'] = stream['host'] |
|||
elif stream['type'] == 'h2': |
|||
stream['secure']['sni'] = stream['host'].split(',')[0] |
|||
return True, result |
|||
except: |
|||
return False, 'Unknown error' |
@ -0,0 +1,143 @@ |
|||
#!/usr/bin/python |
|||
# -*- coding:utf-8 -*- |
|||
import json |
|||
|
|||
from ProxyTester import Xray |
|||
|
|||
config = {} |
|||
|
|||
def trojanBasicTest() -> dict: |
|||
serverConfig = { |
|||
'run_type': 'server', |
|||
'local_addr': '127.0.0.1', |
|||
'local_port': config['port'], |
|||
'password': [ |
|||
config['passwd'] |
|||
], |
|||
'ssl': { |
|||
'cert': config['cert'], |
|||
'key': config['key'] |
|||
} |
|||
} |
|||
return { |
|||
'caption': 'Trojan basic', |
|||
'proxy': { |
|||
'type': 'trojan', |
|||
'server': '127.0.0.1', |
|||
'port': config['port'], |
|||
'passwd': config['passwd'], |
|||
'stream': { |
|||
'type': 'tcp', |
|||
'secure': { |
|||
'type': 'tls', |
|||
'sni': config['host'] |
|||
} |
|||
} |
|||
}, |
|||
'server': { |
|||
'startCommand': ['trojan', '-c', config['file']], |
|||
'fileContent': json.dumps(serverConfig), |
|||
'filePath': config['file'], |
|||
'envVar': {} |
|||
}, |
|||
'aider': None |
|||
} |
|||
|
|||
def loadTrojanStream(streamInfo: dict, xtlsFlow: str or None) -> dict: |
|||
proxyInfo = { |
|||
'type': 'trojan', |
|||
'server': '127.0.0.1', |
|||
'port': config['port'], |
|||
'passwd': config['passwd'], |
|||
'stream': streamInfo['client'] |
|||
} |
|||
inboundConfig = { |
|||
'protocol': 'trojan', |
|||
'listen': '127.0.0.1', |
|||
'port': config['port'], |
|||
'settings': { |
|||
'clients': [ |
|||
{ |
|||
'password': config['passwd'] |
|||
} |
|||
] |
|||
}, |
|||
'streamSettings': streamInfo['server'] |
|||
} |
|||
if xtlsFlow is not None: # add XTLS flow option |
|||
inboundConfig['settings']['clients'][0]['flow'] = xtlsFlow |
|||
return { |
|||
'caption': 'Trojan network ' + streamInfo['caption'], |
|||
'proxy': proxyInfo, |
|||
'server': { |
|||
'startCommand': ['xray', '-c', config['file']], |
|||
'fileContent': Xray.xrayConfig(inboundConfig), |
|||
'filePath': config['file'], |
|||
'envVar': {} |
|||
}, |
|||
'aider': None |
|||
} |
|||
|
|||
def trojanTest(trojanConfig: dict) -> list: |
|||
result = [] |
|||
for key, value in trojanConfig.items(): # trojanConfig -> config |
|||
config[key] = value |
|||
|
|||
result.append(trojanBasicTest()) # basic test |
|||
|
|||
# TCP stream |
|||
streamInfo = Xray.loadTcpStream(False, '', '') |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
streamInfo = Xray.addTlsConfig(streamInfo, config['cert'], config['key'], config['host']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
for flow in Xray.xtlsFlowList: |
|||
streamInfo = Xray.loadTcpStream(False, '', '') |
|||
xtlsFlow, streamInfo = Xray.addXtlsConfig(streamInfo, config['cert'], config['key'], config['host'], flow) |
|||
result.append(loadTrojanStream(streamInfo, xtlsFlow)) |
|||
|
|||
streamInfo = Xray.loadTcpStream(True, config['host'], '/') |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
streamInfo = Xray.addTlsConfig(streamInfo, config['cert'], config['key'], config['host']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
|
|||
# mKCP stream |
|||
for obfs in Xray.udpObfsList: |
|||
streamInfo = Xray.loadKcpStream(config['passwd'], obfs) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
streamInfo = Xray.addTlsConfig(streamInfo, config['cert'], config['key'], config['host']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
for flow in Xray.xtlsFlowList: |
|||
streamInfo = Xray.loadKcpStream(config['passwd'], obfs) |
|||
xtlsFlow, streamInfo = Xray.addXtlsConfig(streamInfo, config['cert'], config['key'], config['host'], flow) |
|||
result.append(loadTrojanStream(streamInfo, xtlsFlow)) |
|||
|
|||
# WebSocket stream |
|||
streamInfo = Xray.loadWsStream(config['host'], config['path'], False) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
streamInfo = Xray.addTlsConfig(streamInfo, config['cert'], config['key'], config['host']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
|
|||
streamInfo = Xray.loadWsStream(config['host'], config['path'], True) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
streamInfo = Xray.addTlsConfig(streamInfo, config['cert'], config['key'], config['host']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
|
|||
# HTTP/2 stream |
|||
streamInfo = Xray.loadH2Stream(config['host'], config['path']) |
|||
streamInfo = Xray.addTlsConfig(streamInfo, config['cert'], config['key'], config['host']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
|
|||
# QUIC stream |
|||
for method in Xray.quicMethodList: |
|||
for obfs in Xray.udpObfsList: |
|||
streamInfo = Xray.loadQuicStream(method, config['passwd'], obfs) |
|||
streamInfo = Xray.addTlsConfig(streamInfo, config['cert'], config['key'], config['host']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
|
|||
# GRPC stream |
|||
streamInfo = Xray.loadGrpcStream(config['service']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
streamInfo = Xray.addTlsConfig(streamInfo, config['cert'], config['key'], config['host']) |
|||
result.append(loadTrojanStream(streamInfo, None)) |
|||
|
|||
return result |
Loading…
Reference in new issue