Browse Source

fix: python reference parameter in dict

master
Dnomd343 3 years ago
parent
commit
f868654f03
  1. 14
      Checker.py
  2. 28
      ProxyBuilder/Shadowsocks.py
  3. 122
      ProxyBuilder/ShadowsocksR.py
  4. 9
      ProxyBuilder/builder.py

14
Checker.py

@ -48,13 +48,15 @@ def proxyTest(
启动失败: 启动失败:
return { return {
'success': False 'success': False,
'info': proxyInfo
} }
测试完成: 测试完成:
return { return {
'success': True, 'success': True,
'result': checkResult 'check': checkResult,
'info': proxyInfo
} }
""" """
@ -74,7 +76,8 @@ def proxyTest(
return None return None
elif not status: # 节点信息有误 elif not status: # 节点信息有误
return { return {
'success': False 'success': False,
'info': rawInfo['info']
} }
time.sleep(startDelay) # 延迟等待客户端启动 time.sleep(startDelay) # 延迟等待客户端启动
@ -85,7 +88,8 @@ def proxyTest(
elif not status: # 客户端异常退出 elif not status: # 客户端异常退出
Builder.destroy(client) Builder.destroy(client)
return { return {
'success': False 'success': False,
'info': rawInfo['info']
} }
if 'check' not in rawInfo: # 缺少检测项目 if 'check' not in rawInfo: # 缺少检测项目
@ -106,5 +110,5 @@ def proxyTest(
return { return {
'success': True, 'success': True,
'check': checkResult, 'check': checkResult,
'proxy': rawInfo['info'] 'info': rawInfo['info']
} }

28
ProxyBuilder/Shadowsocks.py

@ -218,7 +218,8 @@ def __pluginWithUdp(plugin: str, pluginParam: str) -> bool: # 插件是否使用
return False return False
return True # 默认假定占用UDP return True # 默认假定占用UDP
def __ssPython(proxyInfo: dict, socksPort: int, isLegacy: bool = False) -> tuple[dict, str]: # ss-python配置生成 def __ssPython(proxyInfo: dict, socksPort: int,
isUdp: bool, isLegacy: bool = False) -> tuple[dict, str]: # ss-python配置生成
config = __baseConfig(proxyInfo, socksPort) config = __baseConfig(proxyInfo, socksPort)
mbedtlsMethods = [ mbedtlsMethods = [
'aes-128-cfb128', 'aes-128-cfb128',
@ -233,20 +234,21 @@ def __ssPython(proxyInfo: dict, socksPort: int, isLegacy: bool = False) -> tuple
config['method'] = 'mbedtls:' + config['method'] config['method'] = 'mbedtls:' + config['method']
if config['method'] in ['idea-cfb', 'seed-cfb']: # 仅openssl旧版本支持 if config['method'] in ['idea-cfb', 'seed-cfb']: # 仅openssl旧版本支持
config['extra_opts'] = '--libopenssl=libcrypto.so.1.0.0' config['extra_opts'] = '--libopenssl=libcrypto.so.1.0.0'
if not proxyInfo['udp']: if not isUdp:
config['no_udp'] = True # 关闭UDP代理 config['no_udp'] = True # 关闭UDP代理
config['shadowsocks'] = 'ss-python-legacy-local' if isLegacy else 'ss-python-local' config['shadowsocks'] = 'ss-python-legacy-local' if isLegacy else 'ss-python-local'
return config, 'ss-bootstrap-local' return config, 'ss-bootstrap-local'
def __ssLibev(proxyInfo: dict, socksPort: int, isLegacy: bool = False) -> tuple[dict, str]: # ss-libev配置生成 def __ssLibev(proxyInfo: dict, socksPort: int,
isUdp: bool, isLegacy: bool = False) -> tuple[dict, str]: # ss-libev配置生成
config = __baseConfig(proxyInfo, socksPort) config = __baseConfig(proxyInfo, socksPort)
if proxyInfo['udp']: if isUdp:
config['mode'] = 'tcp_and_udp' config['mode'] = 'tcp_and_udp'
return config, 'ss-libev-legacy-local' if isLegacy else 'ss-libev-local' return config, 'ss-libev-legacy-local' if isLegacy else 'ss-libev-local'
def __ssRust(proxyInfo: dict, socksPort: int) -> tuple[dict, str]: # ss-rust配置生成 def __ssRust(proxyInfo: dict, socksPort: int, isUdp: bool) -> tuple[dict, str]: # ss-rust配置生成
config = __baseConfig(proxyInfo, socksPort) config = __baseConfig(proxyInfo, socksPort)
if proxyInfo['udp']: if isUdp:
config['mode'] = 'tcp_and_udp' config['mode'] = 'tcp_and_udp'
return config, 'ss-rust-local' return config, 'ss-rust-local'
@ -287,21 +289,21 @@ def load(proxyInfo: dict, socksPort: int, configFile: str) -> tuple[list or None
if not __ssFormatCheck(proxyInfo): # 参数有误 if not __ssFormatCheck(proxyInfo): # 参数有误
return None, None, None return None, None, None
if proxyInfo['plugin'] is None: # 无插件时启用UDP if proxyInfo['plugin'] is None: # 无插件时启用UDP
proxyInfo['udp'] = True isUdp = True
else: else:
proxyInfo['udp'] = not __pluginWithUdp( # 获取插件UDP冲突状态 isUdp = not __pluginWithUdp( # 获取插件UDP冲突状态
proxyInfo['plugin']['type'], proxyInfo['plugin']['param'] proxyInfo['plugin']['type'], proxyInfo['plugin']['param']
) )
if proxyInfo['method'] in ssMethodList['ss-libev']: # 按序匹配客户端 if proxyInfo['method'] in ssMethodList['ss-libev']: # 按序匹配客户端
config, ssFile = __ssLibev(proxyInfo, socksPort) config, ssFile = __ssLibev(proxyInfo, socksPort, isUdp)
elif proxyInfo['method'] in ssMethodList['ss-libev-legacy']: elif proxyInfo['method'] in ssMethodList['ss-libev-legacy']:
config, ssFile = __ssLibev(proxyInfo, socksPort, isLegacy = True) config, ssFile = __ssLibev(proxyInfo, socksPort, isUdp, isLegacy = True)
elif proxyInfo['method'] in ssMethodList['ss-python']: elif proxyInfo['method'] in ssMethodList['ss-python']:
config, ssFile = __ssPython(proxyInfo, socksPort) config, ssFile = __ssPython(proxyInfo, socksPort, isUdp)
elif proxyInfo['method'] in ssMethodList['ss-python-legacy']: elif proxyInfo['method'] in ssMethodList['ss-python-legacy']:
config, ssFile = __ssPython(proxyInfo, socksPort, isLegacy = True) config, ssFile = __ssPython(proxyInfo, socksPort, isUdp, isLegacy = True)
elif proxyInfo['method'] in ssMethodList['ss-rust']: elif proxyInfo['method'] in ssMethodList['ss-rust']:
config, ssFile = __ssRust(proxyInfo, socksPort) config, ssFile = __ssRust(proxyInfo, socksPort, isUdp)
else: else:
return None, None, None # 无匹配加密方式 return None, None, None # 无匹配加密方式
return [ssFile, '-c', configFile], json.dumps(config), {} return [ssFile, '-c', configFile], json.dumps(config), {}

122
ProxyBuilder/ShadowsocksR.py

@ -54,73 +54,73 @@
import json import json
ssrMethodList = [ # ShadowsocksR加密方式 ssrMethodList = [ # ShadowsocksR加密方式
"aes-128-cfb", 'aes-128-cfb',
"aes-192-cfb", 'aes-192-cfb',
"aes-256-cfb", 'aes-256-cfb',
"aes-128-cfb1", 'aes-128-cfb1',
"aes-192-cfb1", 'aes-192-cfb1',
"aes-256-cfb1", 'aes-256-cfb1',
"aes-128-cfb8", 'aes-128-cfb8',
"aes-192-cfb8", 'aes-192-cfb8',
"aes-256-cfb8", 'aes-256-cfb8',
"aes-128-ctr", 'aes-128-ctr',
"aes-192-ctr", 'aes-192-ctr',
"aes-256-ctr", 'aes-256-ctr',
"aes-128-gcm", 'aes-128-gcm',
"aes-192-gcm", 'aes-192-gcm',
"aes-256-gcm", 'aes-256-gcm',
"aes-128-ofb", 'aes-128-ofb',
"aes-192-ofb", 'aes-192-ofb',
"aes-256-ofb", 'aes-256-ofb',
"camellia-128-cfb", 'camellia-128-cfb',
"camellia-192-cfb", 'camellia-192-cfb',
"camellia-256-cfb", 'camellia-256-cfb',
"none", 'none',
"table", 'table',
"rc4", 'rc4',
"rc4-md5", 'rc4-md5',
"rc4-md5-6", 'rc4-md5-6',
"bf-cfb", 'bf-cfb',
"cast5-cfb", 'cast5-cfb',
"des-cfb", 'des-cfb',
"idea-cfb", 'idea-cfb',
"seed-cfb", 'seed-cfb',
"rc2-cfb", 'rc2-cfb',
"salsa20", 'salsa20',
"xsalsa20", 'xsalsa20',
"chacha20", 'chacha20',
"xchacha20", 'xchacha20',
"chacha20-ietf", 'chacha20-ietf',
] ]
ssrProtocolList = [ # ShadowsocksR协议列表 ssrProtocolList = [ # ShadowsocksR协议列表
"origin", 'origin',
"verify_sha1", 'verify_sha1',
"verify_simple", 'verify_simple',
"verify_deflate", 'verify_deflate',
"auth_simple", 'auth_simple',
"auth_sha1", 'auth_sha1',
"auth_sha1_v2", 'auth_sha1_v2',
"auth_sha1_v4", 'auth_sha1_v4',
"auth_aes128", 'auth_aes128',
"auth_aes128_md5", 'auth_aes128_md5',
"auth_aes128_sha1", 'auth_aes128_sha1',
"auth_chain_a", 'auth_chain_a',
"auth_chain_b", 'auth_chain_b',
"auth_chain_c", 'auth_chain_c',
"auth_chain_d", 'auth_chain_d',
"auth_chain_e", 'auth_chain_e',
"auth_chain_f", 'auth_chain_f',
] ]
ssrObfsList = [ # ShadowsocksR混淆方式 ssrObfsList = [ # ShadowsocksR混淆方式
"plain", 'plain',
"http_post", 'http_post',
"http_simple", 'http_simple',
"tls_simple", 'tls_simple',
"tls1.2_ticket_auth", 'tls1.2_ticket_auth',
"tls1.2_ticket_fastauth", 'tls1.2_ticket_fastauth',
"random_head", 'random_head',
] ]
def __ssrFormatCheck(proxyInfo: dict) -> bool: # ShadowsocksR参数检查 def __ssrFormatCheck(proxyInfo: dict) -> bool: # ShadowsocksR参数检查

9
ProxyBuilder/builder.py

@ -96,12 +96,9 @@ def build(proxyInfo: dict, configDir: str,
socksPort = __getAvailablePort(portRangeStart, portRangeEnd) # 获取Socks5测试端口 socksPort = __getAvailablePort(portRangeStart, portRangeEnd) # 获取Socks5测试端口
if 'type' not in proxyInfo: # 未指定节点类型 if 'type' not in proxyInfo: # 未指定节点类型
return False, 'Proxy type not specified' return False, 'Proxy type not specified'
proxyType = proxyInfo['type'] # 节点类型 if proxyInfo['type'] == 'ss': # Shadowsocks节点
proxyInfo.pop('type')
if proxyType == 'ss': # Shadowsocks节点
clientObj = Shadowsocks clientObj = Shadowsocks
elif proxyType == 'ssr': # ShadowsocksR节点 elif proxyInfo['type'] == 'ssr': # ShadowsocksR节点
clientObj = ShadowsocksR clientObj = ShadowsocksR
else: # 未知类型 else: # 未知类型
return False, 'Unknown proxy type' return False, 'Unknown proxy type'
@ -109,7 +106,7 @@ def build(proxyInfo: dict, configDir: str,
configFile = configDir + '/' + taskFlag + '.json' # 配置文件路径 configFile = configDir + '/' + taskFlag + '.json' # 配置文件路径
startCommand, fileContent, envVar = clientObj.load(proxyInfo, socksPort, configFile) # 载入配置 startCommand, fileContent, envVar = clientObj.load(proxyInfo, socksPort, configFile) # 载入配置
if startCommand is None: # 格式出错 if startCommand is None: # 格式出错
return False, 'Format error with ' + str(proxyType) return False, 'Format error with ' + str(proxyInfo['type'])
try: try:
with open(configFile, 'w') as fileObject: # 保存配置文件 with open(configFile, 'w') as fileObject: # 保存配置文件
fileObject.write(fileContent) fileObject.write(fileContent)

Loading…
Cancel
Save