From ad349fe1c1740ad8109f1b751e74964e8a45e7c6 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 7 Sep 2022 13:57:03 +0800 Subject: [PATCH] feat: constant utils --- Utils/Constant/Default.py | 26 +++++ Utils/Constant/LoadEnv.py | 27 +++++ Utils/Constant/Others.py | 17 +++ Utils/Constant/Plugin.py | 24 +++++ .../Constant/Shadowsocks.py | 100 +----------------- Utils/Constant/__init__.py | 14 +++ env.yaml => env.yml | 6 +- 7 files changed, 113 insertions(+), 101 deletions(-) create mode 100644 Utils/Constant/Default.py create mode 100644 Utils/Constant/LoadEnv.py create mode 100644 Utils/Constant/Others.py create mode 100644 Utils/Constant/Plugin.py rename Basis/Constant.py => Utils/Constant/Shadowsocks.py (62%) create mode 100644 Utils/Constant/__init__.py rename env.yaml => env.yml (52%) diff --git a/Utils/Constant/Default.py b/Utils/Constant/Default.py new file mode 100644 index 0000000..1ca7778 --- /dev/null +++ b/Utils/Constant/Default.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +Version = 'dev' + +LogLevel = 'INFO' +LogFile = 'runtime.log' + +ApiPath = '/' +ApiPort = 7839 +ApiToken = '' + +DnsServer = None +CheckThread = 64 +WorkDir = '/tmp/ProxyC' +TestHost = 'proxyc.net' +TestSite = 'www.bing.com' +PathEnv = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin' + +from Utils.Constant.LoadEnv import loadEnvOptions + +for envKey, envValue in loadEnvOptions('../../env.yml').items(): + if type(envValue) == int: + exec('%s = %s' % (envKey, envValue)) + if type(envValue) == str: + exec('%s = \'%s\'' % (envKey, envValue)) diff --git a/Utils/Constant/LoadEnv.py b/Utils/Constant/LoadEnv.py new file mode 100644 index 0000000..cb5ebfc --- /dev/null +++ b/Utils/Constant/LoadEnv.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import yaml + + +def loadEnvOptions(envFile: str) -> dict: + try: + yamlFile = os.path.join(os.path.dirname(os.path.realpath(__file__)), envFile) + yamlContent = open(yamlFile, 'r', encoding = 'utf-8').read() # read raw yaml content + envOptions = yaml.load(yamlContent, Loader = yaml.FullLoader) # decode yaml configure + except: # something error in env configure + return {} + options = { + 'Version': envOptions['version'] if 'version' in envOptions else None, + 'LogLevel': envOptions['loglevel'] if 'loglevel' in envOptions else None, + 'WorkDir': envOptions['dir'] if 'dir' in envOptions else None, + 'DnsServer': envOptions['dns'] if 'dns' in envOptions else None, + } + if 'api' in envOptions: + options.update({ + 'ApiPort': envOptions['api']['port'] if 'port' in envOptions['api'] else None, + 'ApiPath': envOptions['api']['path'] if 'path' in envOptions['api'] else None, + 'ApiToken': envOptions['api']['token'] if 'token' in envOptions['api'] else None, + }) + return {k: v for k, v in options.items() if v is not None} # remove empty value diff --git a/Utils/Constant/Others.py b/Utils/Constant/Others.py new file mode 100644 index 0000000..a7decd1 --- /dev/null +++ b/Utils/Constant/Others.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# V2ray / Xray +vmessMethods = ['aes-128-gcm', 'chacha20-poly1305', 'auto', 'none', 'zero'] + +quicMethods = ['none', 'aes-128-gcm', 'chacha20-poly1305'] +udpObfuscations = ['none', 'srtp', 'utp', 'wechat-video', 'dtls', 'wireguard'] + +xtlsFlows = ['xtls-origin', 'xtls-direct', 'xtls-splice'] +xtlsFlows = {x: x.replace('-', '-rprx-') for x in xtlsFlows} + +# Trojan-Go +trojanGoMethods = ['aes-128-gcm', 'aes-256-gcm', 'chacha20-ietf-poly1305'] + +# Hysteria +hysteriaProtocols = ['udp', 'wechat-video', 'faketcp'] diff --git a/Utils/Constant/Plugin.py b/Utils/Constant/Plugin.py new file mode 100644 index 0000000..48e457e --- /dev/null +++ b/Utils/Constant/Plugin.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +Plugins = { + 'simple-obfs': ['obfs-local', 'obfs-server'], + 'simple-tls': ['simple-tls'], + 'v2ray': ['v2ray-plugin'], + 'xray': ['xray-plugin'], + 'kcptun': ['kcptun-client', 'kcptun-server'], + 'gost': ['gost-plugin'], + 'cloak': ['ck-client', 'ck-server'], + 'go-quiet': ['gq-client', 'gq-server'], + 'mos-tls-tunnel': ['mtt-client', 'mtt-server'], + 'rabbit': ['rabbit-plugin', 'rabbit'], + 'qtun': ['qtun-client', 'qtun-server'], + 'gun': ['gun-plugin'], +} + +pluginClients = [p[0] for _, p in Plugins.items()] # plugin client list -> obfs-local / simple-tls / ... + +Plugins = {caption: { # format plugins info + 'client': plugin[0], + 'server': plugin[1 if len(plugin) > 1 else 0] +} for caption, plugin in Plugins.items()} diff --git a/Basis/Constant.py b/Utils/Constant/Shadowsocks.py similarity index 62% rename from Basis/Constant.py rename to Utils/Constant/Shadowsocks.py index d30ef82..026b0cc 100644 --- a/Basis/Constant.py +++ b/Utils/Constant/Shadowsocks.py @@ -1,62 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -import os -import yaml - -# Global Options -Version = 'dev' - -ApiPath = '/' -ApiPort = 7839 -ApiToken = '' - -CheckThread = 64 - -LogLevel = 'INFO' -LogFile = 'runtime.log' - -DnsServer = None -WorkDir = '/tmp/ProxyC' -TestHost = 'proxyc.net' -TestSite = 'www.bing.com' -PathEnv = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin' - - -# Load Env Options -envOptions = {} -try: - yamlFile = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../env.yaml') - yamlContent = open(yamlFile, 'r', encoding = 'utf-8').read() - envOptions = yaml.load(yamlContent, Loader = yaml.FullLoader) -except: # something error in env.yaml - pass -if 'version' in envOptions: - Version = envOptions['version'] -if 'loglevel' in envOptions: - LogLevel = envOptions['loglevel'] -if 'dir' in envOptions: - WorkDir = envOptions['dir'] -if 'dns' in envOptions: - DnsServer = envOptions['dns'] -if 'api' in envOptions: - if 'port' in envOptions['api']: - ApiPort = envOptions['api']['port'] - if 'path' in envOptions['api']: - ApiPath = envOptions['api']['path'] - if 'token' in envOptions['api']: - ApiToken = envOptions['api']['token'] - - -# WorkDir Create -try: - os.makedirs(WorkDir) # just like `mkdir -p ...` -except: - pass # folder exist or target is another thing - - -# Shadowsocks Info -mbedtlsMethods = [ +mbedtlsMethods = [ # mbedtls methods in shadowsocks-python 'aes-128-cfb128', 'aes-192-cfb128', 'aes-256-cfb128', @@ -126,32 +71,9 @@ ssMethods = { # methods support of different Shadowsocks project } ssAllMethods = set() -[ssAllMethods.update(ssMethods[x]) for x in ssMethods] +[ssAllMethods.update(x) for _, x in ssMethods.items()] ssAllMethods = sorted(list(ssAllMethods)) # methods of Shadowsocks - -# Plugin Info -Plugins = { - 'simple-obfs': ['obfs-local', 'obfs-server'], - 'simple-tls': ['simple-tls'], - 'v2ray': ['v2ray-plugin'], - 'xray': ['xray-plugin'], - 'kcptun': ['kcptun-client', 'kcptun-server'], - 'gost': ['gost-plugin'], - 'cloak': ['ck-client', 'ck-server'], - 'go-quiet': ['gq-client', 'gq-server'], - 'mos-tls-tunnel': ['mtt-client', 'mtt-server'], - 'rabbit': ['rabbit-plugin', 'rabbit'], - 'qtun': ['qtun-client', 'qtun-server'], - 'gun': ['gun-plugin'], -} - -Plugins = {x: [Plugins[x][0], Plugins[x][1 if len(Plugins[x]) == 2 else 0]] for x in Plugins} -Plugins = {x: {'client': Plugins[x][0], 'server': Plugins[x][1]} for x in Plugins} # format plugins info -pluginClients = [Plugins[x]['client'] for x in Plugins] # plugin client list -> obfs-local / simple-tls / ... - - -# ShadowsocksR Info ssrMethods = [ # methods of ShadowsocksR 'aes-128-ctr', 'aes-192-ctr', 'aes-256-ctr', 'aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm', @@ -178,21 +100,3 @@ ssrObfuscations = [ # obfuscations of ShadowsocksR (obfs) 'plain', 'http_post', 'http_simple', 'random_head', 'tls_simple', 'tls1.2_ticket_auth', 'tls1.2_ticket_fastauth', ] - - -# V2ray / Xray Info -vmessMethods = ['aes-128-gcm', 'chacha20-poly1305', 'auto', 'none', 'zero'] - -quicMethods = ['none', 'aes-128-gcm', 'chacha20-poly1305'] -udpObfuscations = ['none', 'srtp', 'utp', 'wechat-video', 'dtls', 'wireguard'] - -xtlsFlows = ['xtls-origin', 'xtls-direct', 'xtls-splice'] -xtlsFlows = {x: x.replace('-', '-rprx-') for x in xtlsFlows} - - -# Trojan-Go Info -trojanGoMethods = ['aes-128-gcm', 'aes-256-gcm', 'chacha20-ietf-poly1305'] - - -# Hysteria Info -hysteriaProtocols = ['udp', 'wechat-video', 'faketcp'] diff --git a/Utils/Constant/__init__.py b/Utils/Constant/__init__.py new file mode 100644 index 0000000..406e90b --- /dev/null +++ b/Utils/Constant/__init__.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +from Utils.Constant.Plugin import * +from Utils.Constant.Others import * +from Utils.Constant.Default import * +from Utils.Constant.Shadowsocks import * + +# Create WorkDir +try: + os.makedirs(WorkDir) # just like `mkdir -p ...` +except: + pass # folder exist or target is another thing diff --git a/env.yaml b/env.yml similarity index 52% rename from env.yaml rename to env.yml index 83407f9..a276699 100644 --- a/env.yaml +++ b/env.yml @@ -1,8 +1,8 @@ -version: 'v0.1' +version: '0.1' loglevel: 'INFO' -dir: '/tmp/ProxyC' +dir: '/tmp/ProxyC-test' dns: null api: port: 7839 - path: '/' +# path: '/' token: ''