From b69616bbb84e906873d427698c328c3447e1a8e9 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 2 Aug 2022 14:06:45 +0800 Subject: [PATCH] style: optimize test code --- Tester/Brook.py | 11 +++++++---- Tester/Plugin.py | 10 ++++------ Tester/Settings.py | 6 ++++-- Tester/Trojan.py | 9 +++++---- Tester/V2ray.py | 24 +++++++++++++----------- Tester/VLESS.py | 9 +++++---- Tester/Xray.py | 11 ++++++----- Tester/__init__.py | 6 +++--- test.py | 6 +++--- 9 files changed, 50 insertions(+), 42 deletions(-) diff --git a/Tester/Brook.py b/Tester/Brook.py index 7059fcd..b8aab33 100644 --- a/Tester/Brook.py +++ b/Tester/Brook.py @@ -25,17 +25,20 @@ def originStream(isUot: bool) -> dict: def loadWsCommand(proxyInfo: dict) -> list: # load start command for brook server - return ([ + brookCommand = [ 'wsserver', '--listen', '%s:%i' % (hostFormat(proxyInfo['server'], v6Bracket = True), proxyInfo['port']) ] if proxyInfo['stream']['secure'] is None else [ 'wssserver', '--domainaddress', '%s:%i' % (proxyInfo['stream']['host'], proxyInfo['port']) - ]) + [ + ] + brookCommand += [ '--path', proxyInfo['stream']['path'], '--password', proxyInfo['passwd'], - ] + ([] if proxyInfo['stream']['secure'] is None else [ + ] + brookCommand += ([] if proxyInfo['stream']['secure'] is None else [ '--cert', Settings['cert'], '--certkey', Settings['key'], - ]) + (['--withoutBrookProtocol'] if proxyInfo['stream']['raw'] else []) + ]) + return brookCommand + (['--withoutBrookProtocol'] if proxyInfo['stream']['raw'] else []) def wsStream(isRaw: bool, isSecure: bool): diff --git a/Tester/Plugin.py b/Tester/Plugin.py index 249c2a5..55f3517 100644 --- a/Tester/Plugin.py +++ b/Tester/Plugin.py @@ -10,10 +10,7 @@ from Basis.Constant import Plugins from Tester.Settings import Settings from Basis.Functions import genFlag, hostFormat, getAvailablePort - -pluginParams = { - 'SITE': Settings['site'] -} +pluginParams = {} pluginConfig = { 'simple-obfs': { @@ -316,15 +313,16 @@ def paramFill(param: str) -> str: def load(proxyType: str): if proxyType not in ['ss', 'trojan-go']: raise RuntimeError('Unknown proxy type for sip003 plugin') - cloakLoad() # init cloak config - kcptunLoad() # init kcptun config pluginParams.update({ + 'SITE': Settings['site'], 'HOST': Settings['host'], 'CERT': Settings['cert'], 'KEY': Settings['key'], 'PASSWD': genFlag(length = 8), # random password for test 'PATH': '/' + genFlag(length = 6), # random uri path for test }) + cloakLoad() # init cloak config + kcptunLoad() # init kcptun config for pluginType in pluginConfig: for pluginTest, pluginTestInfo in pluginConfig[pluginType].items(): # traverse all plugin test item pluginParams['RANDOM'] = genFlag(length = 8) # refresh RANDOM field diff --git a/Tester/Settings.py b/Tester/Settings.py index abf7a91..5acdee0 100644 --- a/Tester/Settings.py +++ b/Tester/Settings.py @@ -1,11 +1,13 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from Basis.Constant import WorkDir + Settings = { - 'workDir': '/tmp/ProxyC', + 'workDir': WorkDir, + 'site': 'www.bing.com', 'serverBind': '', 'clientBind': '', - 'site': 'www.bing.com', 'host': '', 'cert': '', 'key': '', diff --git a/Tester/Trojan.py b/Tester/Trojan.py index 60c853b..79a07e5 100644 --- a/Tester/Trojan.py +++ b/Tester/Trojan.py @@ -18,11 +18,12 @@ def loadServer(configFile: str, proxyInfo: dict, streamConfig: dict, xtlsFlow: s 'listen': proxyInfo['server'], 'port': proxyInfo['port'], 'settings': { - 'clients': [{**{ + 'clients': [{ 'password': proxyInfo['passwd'], - }, **({} if xtlsFlow is None else { - 'flow': xtlsFlow - })}], + **({} if xtlsFlow is None else { + 'flow': xtlsFlow + }) + }], }, 'streamSettings': streamConfig }) diff --git a/Tester/V2ray.py b/Tester/V2ray.py index b02624c..b651ff7 100644 --- a/Tester/V2ray.py +++ b/Tester/V2ray.py @@ -108,15 +108,16 @@ def wsStream(isEd: bool) -> dict: }, 'server': { 'network': 'ws', - 'wsSettings': {**{ + 'wsSettings': { 'path': path, 'headers': { 'Host': Settings['host'] - } - }, **({} if not isEd else { - 'maxEarlyData': 2048, - 'earlyDataHeaderName': 'Sec-WebSocket-Protocol' - })} + }, + **({} if not isEd else { + 'maxEarlyData': 2048, + 'earlyDataHeaderName': 'Sec-WebSocket-Protocol' + }) + } } } @@ -177,11 +178,12 @@ def grpcStream(isMulti: bool) -> dict: }, 'server': { 'network': 'grpc', - 'grpcSettings': {**{ - 'serviceName': service - }, **({} if not isMulti else { - 'multiMode': True - })} + 'grpcSettings': { + 'serviceName': service, + **({} if not isMulti else { + 'multiMode': True + }) + } } } diff --git a/Tester/VLESS.py b/Tester/VLESS.py index 90f7d06..ed22e7c 100644 --- a/Tester/VLESS.py +++ b/Tester/VLESS.py @@ -18,11 +18,12 @@ def loadServer(configFile: str, proxyInfo: dict, streamConfig: dict, xtlsFlow: s 'listen': proxyInfo['server'], 'port': proxyInfo['port'], 'settings': { - 'clients': [{**{ + 'clients': [{ 'id': proxyInfo['id'], - }, **({} if xtlsFlow is None else { - 'flow': xtlsFlow - })}], + **({} if xtlsFlow is None else { + 'flow': xtlsFlow + }) + }], 'decryption': 'none' }, 'streamSettings': streamConfig diff --git a/Tester/Xray.py b/Tester/Xray.py index ccacd2d..2e61c3f 100644 --- a/Tester/Xray.py +++ b/Tester/Xray.py @@ -20,15 +20,16 @@ def addSecure(streamConfig: dict, xtlsFlow: str or None = None, isUdp443: bool = streamConfig['caption'] += ' (with %s)' % ( 'tls' if xtlsFlow is None else (xtlsFlow + ('-udp443' if isUdp443 else '')) ) - streamConfig['info']['secure'] = {**{ # secure options for client + streamConfig['info']['secure'] = { # secure options for client 'type': 'tls' if xtlsFlow is None else 'xtls', 'sni': Settings['host'], 'alpn': None, 'verify': True, - }, **({} if xtlsFlow is None else { - 'flow': xtlsFlow, - 'udp443': isUdp443, - })} + **({} if xtlsFlow is None else { + 'flow': xtlsFlow, + 'udp443': isUdp443, + }) + } streamConfig['server']['security'] = 'tls' if xtlsFlow is None else 'xtls' streamConfig['server']['%sSettings' % streamConfig['server']['security']] = { # cert and key for server 'alpn': ['h2', 'http/1.1'], diff --git a/Tester/__init__.py b/Tester/__init__.py index 80789ce..dfd464b 100644 --- a/Tester/__init__.py +++ b/Tester/__init__.py @@ -127,14 +127,14 @@ def loadCert(host: str = 'proxyc.net', remark: str = 'ProxyC') -> None: logging.critical('Load self-signed certificate') os.system('mkdir -p %s' % Settings['workDir']) # create work directory logging.critical('Creating CA certificate and key...') - os.system(' '.join(['mad', 'ca'] + [ + os.system(' '.join(['mad', 'ca'] + [ # generate CA certificate and privkey '--ca', caCert, '--key', caKey, '--commonName', remark, '--organization', remark, '--organizationUnit', remark, ])) logging.critical('Signing certificate...') - os.system(' '.join(['mad', 'cert'] + [ + os.system(' '.join(['mad', 'cert'] + [ # generate certificate and privkey, then signed by CA '--ca', caCert, '--ca_key', caKey, '--cert', cert, '--key', key, '--domain', host, @@ -142,7 +142,7 @@ def loadCert(host: str = 'proxyc.net', remark: str = 'ProxyC') -> None: '--organizationUnit', remark, ])) logging.critical('Installing CA certificate...') - os.system('cat %s >> /etc/ssl/certs/ca-certificates.crt' % caCert) + os.system('cat %s >> /etc/ssl/certs/ca-certificates.crt' % caCert) # add into system's trust list Settings['host'] = host Settings['cert'] = cert Settings['key'] = key diff --git a/test.py b/test.py index 2ab60da..7f142d1 100755 --- a/test.py +++ b/test.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import os import sys import Tester from Basis.Logger import logging @@ -46,12 +45,13 @@ if getArg('--filter') is not None: testFilter = set(getArg('--filter').split(',')) isV6 = '--ipv6' in sys.argv -Tester.loadBind(serverV6 = isV6, clientV6 = isV6) -Tester.loadCert('proxyc.net', 'ProxyC') +Tester.loadBind(serverV6 = isV6, clientV6 = isV6) # ipv4 / ipv6 (127.0.0.1 / ::1) +Tester.loadCert('proxyc.net', 'ProxyC') # default cert config logging.critical('TEST ITEM: ' + ('all' if testItem is None else testItem)) logging.critical('FILTER: %s' % testFilter) logging.critical('URL: ' + testUrl) logging.critical('THREAD NUMBER: %i' % threadNum) + logging.critical('-------------------------------- TEST START --------------------------------') if testItem is not None: if testItem == 'ss' and '--all' in sys.argv: