From 59849533f68fba448118c000c82e0376311f4dda Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 7 Sep 2022 16:49:48 +0800 Subject: [PATCH] update: filter utils --- Decoder/Shadowsocks.py | 6 ++-- Decoder/ShadowsocksR.py | 74 +++++++++++++++++++------------------- Decoder/VMess.py | 4 +-- Filter/Brook.py | 6 ++-- Filter/Hysteria.py | 8 ++--- Filter/Plugin.py | 6 ++-- Filter/Shadowsocks.py | 8 ++--- Filter/ShadowsocksR.py | 8 ++--- Filter/Trojan.py | 6 ++-- Filter/TrojanGo.py | 8 ++--- Filter/V2ray.py | 6 ++-- Filter/VLESS.py | 6 ++-- Filter/VMess.py | 8 ++--- Filter/Xray.py | 6 ++-- Utils/Constant/__init__.py | 3 +- {Basis => Utils}/Filter.py | 10 +++--- test.py | 6 ++-- 17 files changed, 90 insertions(+), 89 deletions(-) rename {Basis => Utils}/Filter.py (96%) diff --git a/Decoder/Shadowsocks.py b/Decoder/Shadowsocks.py index 3e3954d..7315eac 100644 --- a/Decoder/Shadowsocks.py +++ b/Decoder/Shadowsocks.py @@ -6,8 +6,8 @@ import copy from Utils.Logger import logger +from Utils.Common import urlDecode, b64Decode from Utils.Common import checkScheme, splitTag -from Utils.Common import urlDecode, base64Decode ssBasicConfig = { 'type': 'ss', @@ -37,7 +37,7 @@ def ssCommon(url: str) -> dict: config = copy.deepcopy(ssBasicConfig) logger.debug('Shadowsocks common decode -> %s' % url) url, config['name'] = splitTag(checkScheme(url, 'ss', 'Shadowsocks common')) - userinfo, url = base64Decode(url).rsplit('@', 1) + userinfo, url = b64Decode(url).rsplit('@', 1) config['info']['server'], config['info']['port'] = url.rsplit(':', 1) config['info']['method'], config['info']['passwd'] = userinfo.split(':', 1) logger.debug('Shadowsocks common decode release -> %s', config) @@ -54,7 +54,7 @@ def sip002(url: str) -> dict: url, config['name'] = splitTag(checkScheme(url, 'ss', 'SIP002')) userinfo, url = url.rsplit('@', 1) try: - userinfo = base64Decode(userinfo) # userinfo encode base64 is optional + userinfo = b64Decode(userinfo) # userinfo encode base64 is optional except: userinfo = urlDecode(userinfo) # not base64 decode -> url encode format config['info']['method'], config['info']['passwd'] = userinfo.split(':', 1) diff --git a/Decoder/ShadowsocksR.py b/Decoder/ShadowsocksR.py index 8d9885d..e196c2d 100644 --- a/Decoder/ShadowsocksR.py +++ b/Decoder/ShadowsocksR.py @@ -1,37 +1,37 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -# https://github.com/shadowsocksrr/shadowsocksr-csharp/blob/master/shadowsocks-csharp/Model/Server.cs#L357 - -from Utils.Logger import logger -from Utils.Common import checkScheme, base64Decode, splitParam - -def ssr(url: str) -> dict: - """ - FORMAT: ssr://base64( - server:port:protocol:method:obfs:base64(passwd)/? - obfsparam=base64(...)&protoparam=...(...)&remarks=base64(...)&group=base64(...) - ) - """ - config = { - 'type': 'ssr', - 'info': {} - } - info = config['info'] - logger.debug('ShadowsocksR decode -> %s' % url) - - params = '' - url = base64Decode(checkScheme(url, 'ssr', 'ShadowsocksR')) - if '?' in url: - url, params = url.replace('/?', '?').split('?') # `.../?...` or `...?...` - info['server'], info['port'], info['protocol'], info['method'], info['obfs'], info['passwd'] = url.rsplit(':', 5) - info['passwd'] = base64Decode(info['passwd']) - - params = splitParam(params) - logger.debug('ShadowsocksR decode params -> %s' % params) - info['obfsParam'] = base64Decode(params['obfsparam']) if 'obfsparam' in params else '' - info['protocolParam'] = base64Decode(params['protoparam']) if 'protoparam' in params else '' - config['name'] = base64Decode(params['remarks']) if 'remarks' in params else '' - config['group'] = base64Decode(params['group']) if 'group' in params else '' - logger.debug('ShadowsocksR decode release -> %s', config) - return config +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# https://github.com/shadowsocksrr/shadowsocksr-csharp/blob/master/shadowsocks-csharp/Model/Server.cs#L357 + +from Utils.Logger import logger +from Utils.Common import b64Decode, checkScheme, splitParam + +def ssr(url: str) -> dict: + """ + FORMAT: ssr://base64( + server:port:protocol:method:obfs:base64(passwd)/? + obfsparam=base64(...)&protoparam=...(...)&remarks=base64(...)&group=base64(...) + ) + """ + config = { + 'type': 'ssr', + 'info': {} + } + info = config['info'] + logger.debug('ShadowsocksR decode -> %s' % url) + + params = '' + url = b64Decode(checkScheme(url, 'ssr', 'ShadowsocksR')) + if '?' in url: + url, params = url.replace('/?', '?').split('?') # `.../?...` or `...?...` + info['server'], info['port'], info['protocol'], info['method'], info['obfs'], info['passwd'] = url.rsplit(':', 5) + info['passwd'] = b64Decode(info['passwd']) + + params = splitParam(params) + logger.debug('ShadowsocksR decode params -> %s' % params) + info['obfsParam'] = b64Decode(params['obfsparam']) if 'obfsparam' in params else '' + info['protocolParam'] = b64Decode(params['protoparam']) if 'protoparam' in params else '' + config['name'] = b64Decode(params['remarks']) if 'remarks' in params else '' + config['group'] = b64Decode(params['group']) if 'group' in params else '' + logger.debug('ShadowsocksR decode release -> %s', config) + return config diff --git a/Decoder/VMess.py b/Decoder/VMess.py index a712b84..963ee61 100644 --- a/Decoder/VMess.py +++ b/Decoder/VMess.py @@ -4,7 +4,7 @@ import json from Utils.Logger import logger from Utils.Exception import decodeException -from Utils.Common import base64Decode, checkScheme, hostFormat +from Utils.Common import b64Decode, checkScheme, hostFormat def v2rayN(url: str) -> dict: """ @@ -17,7 +17,7 @@ def v2rayN(url: str) -> dict: } info = config['info'] logger.debug('V2rayN url decode -> %s' % url) - url = json.loads(base64Decode(checkScheme(url, 'vmess', 'V2rayN'))) + url = json.loads(b64Decode(checkScheme(url, 'vmess', 'V2rayN'))) logger.debug('V2rayN json format -> %s' % url) if int(url['v']) != 2: logger.warning('V2rayN url with unknown version') diff --git a/Filter/Brook.py b/Filter/Brook.py index 2ef8da3..bca45be 100644 --- a/Filter/Brook.py +++ b/Filter/Brook.py @@ -2,9 +2,9 @@ # -*- coding: utf-8 -*- import copy -from Basis.Functions import isHost, isPort -from Basis.Filter import Filter, rulesFilter -from Basis.Functions import toInt, toStr, toStrTidy, toBool +from Utils.Common import isHost, isPort +from Utils.Filter import Filter, rulesFilter +from Utils.Common import toInt, toStr, toStrTidy, toBool secureObject = rulesFilter({ 'verify': { diff --git a/Filter/Hysteria.py b/Filter/Hysteria.py index 48f65cc..8820433 100644 --- a/Filter/Hysteria.py +++ b/Filter/Hysteria.py @@ -2,10 +2,10 @@ # -*- coding: utf-8 -*- import copy -from Basis.Functions import isHost, isPort -from Basis.Filter import Filter, rulesFilter -from Basis.Constant import hysteriaProtocols -from Basis.Functions import isIpAddr, toInt, toStr, toStrTidy, toBool +from Utils.Constant import hysteriaProtocols +from Utils.Filter import Filter, rulesFilter +from Utils.Common import isHost, isPort, isIpAddr +from Utils.Common import toInt, toStr, toStrTidy, toBool hysteriaObject = rulesFilter({ 'server': { diff --git a/Filter/Plugin.py b/Filter/Plugin.py index 559676e..9ef7973 100644 --- a/Filter/Plugin.py +++ b/Filter/Plugin.py @@ -2,9 +2,9 @@ # -*- coding: utf-8 -*- import copy -from Basis.Filter import rulesFilter -from Basis.Constant import pluginClients -from Basis.Functions import toStr, toStrTidy +from Utils.Filter import rulesFilter +from Utils.Constant import pluginClients +from Utils.Common import toStr, toStrTidy pluginAlias = { 'obfs-local': {'obfs', 'simple-obfs'}, diff --git a/Filter/Shadowsocks.py b/Filter/Shadowsocks.py index 72ca1ed..428977c 100644 --- a/Filter/Shadowsocks.py +++ b/Filter/Shadowsocks.py @@ -3,10 +3,10 @@ import copy from Filter.Plugin import pluginObject -from Basis.Constant import ssAllMethods -from Basis.Functions import isHost, isPort -from Basis.Filter import Filter, rulesFilter -from Basis.Functions import toInt, toStr, toStrTidy +from Utils.Constant import ssAllMethods +from Utils.Common import isHost, isPort +from Utils.Filter import Filter, rulesFilter +from Utils.Common import toInt, toStr, toStrTidy ssObject = rulesFilter({ 'server': { diff --git a/Filter/ShadowsocksR.py b/Filter/ShadowsocksR.py index 3877d59..bdf2009 100644 --- a/Filter/ShadowsocksR.py +++ b/Filter/ShadowsocksR.py @@ -2,10 +2,10 @@ # -*- coding: utf-8 -*- import copy -from Basis.Functions import isHost, isPort -from Basis.Filter import Filter, rulesFilter -from Basis.Functions import toInt, toStr, toStrTidy -from Basis.Constant import ssrMethods, ssrProtocols, ssrObfuscations +from Utils.Common import isHost, isPort +from Utils.Filter import Filter, rulesFilter +from Utils.Common import toInt, toStr, toStrTidy +from Utils.Constant import ssrMethods, ssrProtocols, ssrObfuscations def ssrProtocolFormat(protocol: str) -> str: diff --git a/Filter/Trojan.py b/Filter/Trojan.py index f4ca221..caaac39 100644 --- a/Filter/Trojan.py +++ b/Filter/Trojan.py @@ -3,9 +3,9 @@ import copy from Filter import Xray -from Basis.Functions import isHost, isPort -from Basis.Filter import Filter, rulesFilter -from Basis.Functions import toInt, toStr, toStrTidy +from Utils.Common import isHost, isPort +from Utils.Filter import Filter, rulesFilter +from Utils.Common import toInt, toStr, toStrTidy trojanObject = rulesFilter({ 'server': { diff --git a/Filter/TrojanGo.py b/Filter/TrojanGo.py index 27689b2..fcfc99c 100644 --- a/Filter/TrojanGo.py +++ b/Filter/TrojanGo.py @@ -3,10 +3,10 @@ import copy from Filter.Plugin import pluginObject -from Basis.Constant import trojanGoMethods -from Basis.Functions import isHost, isPort -from Basis.Filter import Filter, rulesFilter -from Basis.Functions import isIpAddr, toInt, toStr, toStrTidy, toBool +from Utils.Common import isHost, isPort +from Utils.Constant import trojanGoMethods +from Utils.Filter import Filter, rulesFilter +from Utils.Common import isIpAddr, toInt, toStr, toStrTidy, toBool ssObject = rulesFilter({ 'method': { diff --git a/Filter/V2ray.py b/Filter/V2ray.py index 5b4cdc2..e5768ac 100644 --- a/Filter/V2ray.py +++ b/Filter/V2ray.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from Basis.Filter import rulesFilter -from Basis.Constant import quicMethods, udpObfuscations -from Basis.Functions import isIpAddr, toInt, toStr, toStrTidy, toBool +from Utils.Filter import rulesFilter +from Utils.Constant import quicMethods, udpObfuscations +from Utils.Common import isIpAddr, toInt, toStr, toStrTidy, toBool tlsObject = rulesFilter({ 'sni': { diff --git a/Filter/VLESS.py b/Filter/VLESS.py index b8dbd2c..91aa491 100644 --- a/Filter/VLESS.py +++ b/Filter/VLESS.py @@ -3,9 +3,9 @@ import copy from Filter import Xray -from Basis.Functions import isHost, isPort -from Basis.Filter import Filter, rulesFilter -from Basis.Functions import toInt, toStrTidy +from Utils.Common import isHost, isPort +from Utils.Common import toInt, toStrTidy +from Utils.Filter import Filter, rulesFilter vlessObject = rulesFilter({ 'server': { diff --git a/Filter/VMess.py b/Filter/VMess.py index ffc0ffa..90f9abd 100644 --- a/Filter/VMess.py +++ b/Filter/VMess.py @@ -3,10 +3,10 @@ import copy from Filter import V2ray -from Basis.Constant import vmessMethods -from Basis.Functions import isHost, isPort -from Basis.Filter import Filter, rulesFilter -from Basis.Functions import toInt, toStrTidy +from Utils.Constant import vmessMethods +from Utils.Common import isHost, isPort +from Utils.Common import toInt, toStrTidy +from Utils.Filter import Filter, rulesFilter vmessObject = rulesFilter({ 'server': { diff --git a/Filter/Xray.py b/Filter/Xray.py index f014bdb..5af11bf 100644 --- a/Filter/Xray.py +++ b/Filter/Xray.py @@ -3,9 +3,9 @@ import copy from Filter import V2ray -from Basis.Filter import rulesFilter -from Basis.Constant import xtlsFlows -from Basis.Functions import toStrTidy, toBool +from Utils.Filter import rulesFilter +from Utils.Constant import xtlsFlows +from Utils.Common import toStrTidy, toBool def xtlsFlowFormat(flow: str) -> str: diff --git a/Utils/Constant/__init__.py b/Utils/Constant/__init__.py index 406e90b..ba798f4 100644 --- a/Utils/Constant/__init__.py +++ b/Utils/Constant/__init__.py @@ -7,8 +7,7 @@ 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 ...` + os.makedirs(WorkDir) # create work folder (`mkdir -p ...`) except: pass # folder exist or target is another thing diff --git a/Basis/Filter.py b/Utils/Filter.py similarity index 96% rename from Basis/Filter.py rename to Utils/Filter.py index 5a680db..ab6af5d 100644 --- a/Basis/Filter.py +++ b/Utils/Filter.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import copy +from Utils.Logger import logger from Utils.Exception import filterException filterObject = { @@ -72,6 +73,7 @@ def Filter(raw: dict, rules: dict) -> dict: data = {} raw = copy.deepcopy(raw) rules = copy.deepcopy(rules) + for key, rule in rules.items(): # pretreatment process (raw --[copy / default value]--> data) if key not in raw: # key not exist @@ -80,19 +82,19 @@ def Filter(raw: dict, rules: dict) -> dict: data[key] = rule['default'] # set default value else: # key exist data[key] = raw[key] + # format process (data --[format]--> data) - # TODO: some 'none' value should be format as None if data[key] is None: # key content is None if not rule['allowNone']: # key is not allow None raise filterException('Field `%s` shouldn\'t be None' % key) continue # skip following process try: - data[key] = rule['format'](data[key]) # run format - # TODO: format result maybe None, check allowNone again (allow -> skip following process) + data[key] = rule['format'](data[key]) # run format function except: raise filterException(rule['errMsg']) # format error + # filter process (data --[type check (& filter check)]--> pass / non-pass) - if type(rule['type']) == type: # str / int / bool / ... + if type(rule['type']) == type: # str / int / bool / ... -> list rule['type'] = [rule['type']] # str -> [str] / int -> [int] / ... if type(rule['type']) == list: # [str, int, bool, ...] if data[key] == any and any in rule['type']: # special case -> skip type filter diff --git a/test.py b/test.py index 379acac..261dc30 100755 --- a/test.py +++ b/test.py @@ -1,14 +1,14 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from Basis import Constant +from Utils import Constant Constant.LogLevel = 'DEBUG' import Decoder from pprint import pprint from Filter import Filter -# ret = Decoder.ssPlain('ss://aes-128-ctr:pa@ss#word@8.210.148.24:34326#ok%2Bfuck') +ret = Decoder.ssPlain('ss://aes-128-ctr:pa@ss#word@8.210.148.24:34326#ok%2Bfuck') # ret = Decoder.ssCommon('ss://YmYtY2ZiOnRlc3QvIUAjOkAxOTIuMTY4LjEwMC4xOjg4ODg#example-server') # ret = Decoder.sip002('ss://YWVzLTEyOC1nY206dGVzdA@192.168.100.1:8888#Example1') # ret = Decoder.sip002('ss://2022-blake3-aes-256-gcm:YctPZ6U7xPPcU%2Bgp3u%2B0tx%2FtRizJN9K8y%2BuKlW2qjlI%3D@192.168.100.1:8888/?plugin=v2ray-plugin%3Bserver#Example3') @@ -17,7 +17,7 @@ from Filter import Filter # ret = Decoder.ssr('ssr://ZmU4MDo6MTo2MDA0OmF1dGhfYWVzMTI4X21kNTphZXMtMjU2LWNmYjp0bHMxLjJfdGlja2V0X2F1dGg6Y0dGemMzZGsvP3Byb3RvcGFyYW09') # ret = Decoder.ssr('ssr://ZmU4MDo6MTo2MDA0OmF1dGhfYWVzMTI4X21kNTphZXMtMjU2LWNmYjp0bHMxLjJfdGlja2V0X2F1dGg6Y0dGemMzZGs') -ret = Decoder.v2rayN('vmess://eyJhZGQiOiJmbnlkdXpheW92dnNxaTMyZ2kucTc1NDMudG9wIiwicHMiOiJ2MXzpppnmuK8wM3zljp_nlJ984piF4piF4piFICgyKSIsInNjeSI6ImF1dG8iLCJ0eXBlIjoiaHR0cCIsInNuaSI6IiIsInBhdGgiOiIvIiwicG9ydCI6MzgzMzcsInYiOjIsImhvc3QiOiJ4aGY0eHE3ZDVjYjRnc3kzeW1teWR3b2kuc2luYS5jbiIsInRscyI6IiIsImlkIjoiOTAxY2UyNTUtOTM2OS1kMWUyLTk1ODQtZGE1YTdqZjA1NDdrIiwibmV0IjoidGNwIiwiYWlkIjowfQ') +# ret = Decoder.v2rayN('vmess://eyJhZGQiOiJmbnlkdXpheW92dnNxaTMyZ2kucTc1NDMudG9wIiwicHMiOiJ2MXzpppnmuK8wM3zljp_nlJ984piF4piF4piFICgyKSIsInNjeSI6ImF1dG8iLCJ0eXBlIjoiaHR0cCIsInNuaSI6IiIsInBhdGgiOiIvIiwicG9ydCI6MzgzMzcsInYiOjIsImhvc3QiOiJ4aGY0eHE3ZDVjYjRnc3kzeW1teWR3b2kuc2luYS5jbiIsInRscyI6IiIsImlkIjoiOTAxY2UyNTUtOTM2OS1kMWUyLTk1ODQtZGE1YTdqZjA1NDdrIiwibmV0IjoidGNwIiwiYWlkIjowfQ') # ret['info']['server'] = '[%s]' % ret['info']['server']