diff --git a/ProxyBuilder/Shadowsocks.py b/ProxyBuilder/Shadowsocks.py index c21cd56..9f4780b 100644 --- a/ProxyBuilder/Shadowsocks.py +++ b/ProxyBuilder/Shadowsocks.py @@ -141,9 +141,9 @@ ssMethodList = { # shadowsocks各版本支持的加密方式 def __baseJSON(proxyInfo, socksPort): # 生成JSON基本结构 jsonContent = { 'server': proxyInfo['server'], - 'server_port': proxyInfo['port'], + 'server_port': int(proxyInfo['port']), 'local_address': '127.0.0.1', - 'local_port': socksPort, + 'local_port': int(socksPort), 'password': proxyInfo['password'], 'method': proxyInfo['method'], } diff --git a/ProxyBuilder/ShadowsocksR.py b/ProxyBuilder/ShadowsocksR.py index c80f991..8870c37 100644 --- a/ProxyBuilder/ShadowsocksR.py +++ b/ProxyBuilder/ShadowsocksR.py @@ -82,9 +82,9 @@ def load(proxyInfo, socksPort, configFile): # ShadowsocksR配置载入 return None, None # 匹配不到混淆方式 return [ 'ssr-local', '-c', configFile ], json.dumps({ 'server': proxyInfo['server'], - 'server_port': proxyInfo['port'], + 'server_port': int(proxyInfo['port']), 'local_address': '127.0.0.1', - 'local_port': socksPort, + 'local_port': int(socksPort), 'password': proxyInfo['password'], 'method': proxyInfo['method'], 'protocol': proxyInfo['protocol'], diff --git a/ProxyBuilder/builder.py b/ProxyBuilder/builder.py index b200563..795c04e 100644 --- a/ProxyBuilder/builder.py +++ b/ProxyBuilder/builder.py @@ -92,13 +92,13 @@ def build(proxyInfo, configDir): # 构建代理节点连接 if os.path.exists(libcPath): # 定位libc.so文件 break exitWithMe = lambda: ctypes.CDLL(libcPath).prctl(1, 15) # SIGTERM - process = subprocess.Popen(startCommand, \ - stdout = subprocess.DEVNULL, \ - stderr = subprocess.DEVNULL, \ + process = subprocess.Popen(startCommand, + stdout = subprocess.DEVNULL, + stderr = subprocess.DEVNULL, preexec_fn = exitWithMe) # 子进程跟随退出 except: - process = subprocess.Popen(startCommand, \ - stdout = subprocess.DEVNULL, \ + process = subprocess.Popen(startCommand, + stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL) # prctl失败 回退正常启动 return { # 返回连接参数 @@ -132,4 +132,42 @@ def destroy(taskInfo): # 结束客户端并清理 except: pass def test(): - print("ProxyBuilder test") + import requests + import ProxyBuilder.test as tester + + data = tester.Shadowsocks.test(1080, 'dnomd343') + + for field in data: + serverProcess = subprocess.Popen( + field['serverCommand'], + stdout = subprocess.DEVNULL, + stderr = subprocess.DEVNULL) + time.sleep(0.5) # 等待服务端启动完成 + if serverProcess.poll() != None: # 服务端启动失败 + print("server unexpected exit") + continue + print(field['caption']) + + client = build(field['proxyInfo'], '/tmp/ProxyC') + time.sleep(0.5) # 等待客户端启动完成 + if not check(client): + print("client unexpected exit") # 客户端启动失败 + else: + try: + startTime = time.time_ns() + r = requests.get('http://gstatic.com/generate_204', proxies = { + 'http': 'socks5://127.0.0.1:' + str(client['port']), + 'https': 'socks5://127.0.0.1:' + str(client['port']), + }) + if r.status_code == 204: + delay = (time.time_ns() - startTime) / (10 ** 6) + print(format(delay, '.2f') + 'ms') + else: + print("connect error") + except: + print("request error") + destroy(client) # 关闭客户端 + time.sleep(0.3) + serverProcess.terminate() # 关闭服务端 + time.sleep(0.3) + print() diff --git a/ProxyBuilder/test/Shadowsocks.py b/ProxyBuilder/test/Shadowsocks.py new file mode 100644 index 0000000..95fc4df --- /dev/null +++ b/ProxyBuilder/test/Shadowsocks.py @@ -0,0 +1,109 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +ssMethodList = [ + 'aes-128-gcm', + 'aes-192-gcm', + 'aes-256-gcm', + 'aes-128-ctr', + 'aes-192-ctr', + 'aes-256-ctr', + 'aes-128-ocb', + 'aes-192-ocb', + 'aes-256-ocb', + 'aes-128-ofb', + 'aes-192-ofb', + 'aes-256-ofb', + 'aes-128-cfb', + 'aes-192-cfb', + 'aes-256-cfb', + 'aes-128-cfb1', + 'aes-192-cfb1', + 'aes-256-cfb1', + 'aes-128-cfb8', + 'aes-192-cfb8', + 'aes-256-cfb8', + 'aes-128-cfb128', + 'aes-192-cfb128', + 'aes-256-cfb128', + 'camellia-128-cfb', + 'camellia-192-cfb', + 'camellia-256-cfb', + 'camellia-128-cfb128', + 'camellia-192-cfb128', + 'camellia-256-cfb128', + 'plain', + 'none', + 'table', + 'rc4', + 'rc4-md5', + 'rc2-cfb', + 'bf-cfb', + 'cast5-cfb', + 'des-cfb', + 'idea-cfb', + 'seed-cfb', + 'salsa20', + 'salsa20-ctr', + 'xchacha20', + 'chacha20', + 'chacha20-ietf', + 'chacha20-poly1305', + 'chacha20-ietf-poly1305', + 'xchacha20-ietf-poly1305' +] + +def test(port, password): + testList = [] + for method in ssMethodList: + proxyInfo = { + 'type': 'ss', + 'server': '127.0.0.1', + 'port': str(port), + 'password': password, + 'method': method, + 'plugin': '', + 'pluginArg': '', + } + testInfo = 'test for ' + method + if method == 'plain' or method == 'none': + serverCommand = [ + 'ss-rust-server', '-U', + '-s', '0.0.0.0:' + str(port), + '-k', password, + '-m', method + ] + elif method == 'salsa20-ctr': + serverCommand = [ + 'ss-bootstrap-server', + '--shadowsocks', 'ss-python-legacy-server', + '-p', str(port), + '-k', password, + '-m', method + ] + else: + specialMethods = [ + 'aes-128-cfb128', + 'aes-192-cfb128', + 'aes-256-cfb128', + 'camellia-128-cfb128', + 'camellia-192-cfb128', + 'camellia-256-cfb128', + ] + if method in specialMethods: + method = 'mbedtls:' + method + serverCommand = [ + 'ss-bootstrap-server', + '--shadowsocks', 'ss-python-server', + '-p', str(port), + '-k', password, + '-m', method + ] + if method == 'idea-cfb' or method == 'seed-cfb': + serverCommand.append('--libopenssl=libcrypto.so.1.0.0') + testList.append({ + 'caption': testInfo, + 'proxyInfo': proxyInfo, + 'serverCommand': serverCommand + }) + return testList diff --git a/ProxyBuilder/test/__init__.py b/ProxyBuilder/test/__init__.py new file mode 100644 index 0000000..b3ef4ca --- /dev/null +++ b/ProxyBuilder/test/__init__.py @@ -0,0 +1,4 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +from ProxyBuilder.test import Shadowsocks diff --git a/demo.py b/demo.py index 5e81a39..c6fe2a3 100644 --- a/demo.py +++ b/demo.py @@ -49,6 +49,7 @@ print(task) time.sleep(1) if Builder.check(task) == False: print("error exit") + Builder.destroy(task) else: print("test with gstatic") checkSocksPort(task['port'])