From be5e476ddbbef601565d7306a3bfa3e7b3120e1a Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Thu, 7 Jul 2022 00:26:25 +0800 Subject: [PATCH] feat: add shadowsocksr support of Builder --- Basis/Methods.py | 29 ++++++++++++++++++++++++++++- Builder/ShadowsocksR.py | 27 +++++++++++++++++++++++++++ Builder/__init__.py | 4 +++- demo.py | 20 +++++++++++++++++--- 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 Builder/ShadowsocksR.py diff --git a/Basis/Methods.py b/Basis/Methods.py index 893f397..d03c7d8 100644 --- a/Basis/Methods.py +++ b/Basis/Methods.py @@ -63,4 +63,31 @@ ssMethods = { # methods support of different Shadowsocks project ssAllMethods = set() [ssAllMethods.update(ssMethods[x]) for x in ssMethods] -ssAllMethods = list(ssAllMethods) +ssAllMethods = list(ssAllMethods) # methods of Shadowsocks + +ssrMethods = [ # methods of ShadowsocksR + 'aes-128-ctr', 'aes-192-ctr', 'aes-256-ctr', + 'aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm', + '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', + 'camellia-128-cfb', 'camellia-192-cfb', 'camellia-256-cfb', + 'none', 'table', 'rc4', 'rc4-md5', 'rc2-cfb', 'rc4-md5-6', + 'bf-cfb', 'cast5-cfb', 'des-cfb', 'idea-cfb', 'seed-cfb', + 'salsa20', 'xsalsa20', 'chacha20', 'xchacha20', 'chacha20-ietf', +] + +ssrProtocols = [ # protocols of ShadowsocksR + 'origin', 'auth_simple', + 'verify_sha1', 'verify_simple', 'verify_deflate', + 'auth_sha1', 'auth_sha1_v2', 'auth_sha1_v4', + 'auth_aes128', 'auth_aes128_md5', 'auth_aes128_sha1', + 'auth_chain_a', 'auth_chain_b', 'auth_chain_c', + 'auth_chain_d', 'auth_chain_e', 'auth_chain_f', +] + +ssrObfuscations = [ # obfuscations of ShadowsocksR (obfs) + 'plain', 'http_post', 'http_simple', 'random_head', + 'tls_simple', 'tls1.2_ticket_auth', 'tls1.2_ticket_fastauth', +] diff --git a/Builder/ShadowsocksR.py b/Builder/ShadowsocksR.py new file mode 100644 index 0000000..9ec94da --- /dev/null +++ b/Builder/ShadowsocksR.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import json +from Basis.Methods import ssrMethods, ssrProtocols, ssrObfuscations + + +def load(proxyInfo: dict, socksInfo: dict, configFile: str) -> tuple[list, str, dict]: + if proxyInfo['method'] not in ssrMethods: + raise RuntimeError('Unknown shadowsocksr method') + if proxyInfo['protocol'] not in ssrProtocols: + raise RuntimeError('Unknown shadowsocksr protocol') + if proxyInfo['obfs'] not in ssrObfuscations: + raise RuntimeError('Unknown shadowsocksr obfuscation') + ssrConfig = { + 'server': proxyInfo['server'], + 'server_port': proxyInfo['port'], # type -> int + 'local_address': socksInfo['addr'], + 'local_port': socksInfo['port'], # type -> int + 'password': proxyInfo['passwd'], + 'method': proxyInfo['method'], + 'protocol': proxyInfo['protocol'], + 'protocol_param': proxyInfo['protocolParam'], + 'obfs': proxyInfo['obfs'], + 'obfs_param': proxyInfo['obfsParam'] + } + return ['ssr-local', '-vv', '-c', configFile], json.dumps(ssrConfig), {} # tuple[command, fileContent, envVar] diff --git a/Builder/__init__.py b/Builder/__init__.py index c5f80e4..8114f99 100644 --- a/Builder/__init__.py +++ b/Builder/__init__.py @@ -5,6 +5,7 @@ import os import copy from Builder import Shadowsocks +from Builder import ShadowsocksR from Basis.Logger import logging from Basis.Process import Process @@ -13,7 +14,7 @@ from Basis.Functions import genFlag, getAvailablePort default = { 'workDir': '/tmp/ProxyC', 'bindAddr': '127.0.0.1', - 'binDir': '/usr/bin', + 'binDir': '/bin:/usr/bin:/usr/local/bin', } @@ -39,6 +40,7 @@ class Builder(object): def __loadClient(self, isStart: bool): loadFunction = { 'ss': Shadowsocks.load, + 'ssr': ShadowsocksR.load, } if self.proxyType not in loadFunction: raise RuntimeError('Unknown proxy type') diff --git a/demo.py b/demo.py index 28f12db..7fc994e 100755 --- a/demo.py +++ b/demo.py @@ -5,7 +5,7 @@ import time from Builder import Builder from Basis.Logger import logging -proxy = { +proxySS = { 'type': 'ss', 'info': { 'server': '127.0.0.1', @@ -16,7 +16,21 @@ proxy = { } } -client = Builder(proxy) +proxySSR = { + 'type': 'ssr', + 'info': { + 'server': '127.0.0.1', + 'port': 12345, + 'passwd': 'dnomd343', + 'method': 'aes-128-ctr', + 'protocol': 'origin', + 'protocolParam': '', + 'obfs': 'plain', + 'obfsParam': '', + } +} + +client = Builder(proxySSR) logging.critical(client.id) logging.critical(client.proxyType) @@ -24,7 +38,7 @@ logging.critical(client.proxyInfo) logging.critical(client.socksAddr) logging.critical(client.socksPort) -time.sleep(5) +time.sleep(20) logging.critical(client.status()) client.destroy()