From d17f9468e4aafe2b594272a02228b6c4848aa722 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 3 Mar 2022 09:01:27 +0800 Subject: [PATCH] feat: filter of Brook proxy --- ProxyFilter/Brook.py | 90 +++++++++++++++++++++++++++++++++++++++++++ ProxyFilter/filter.py | 3 ++ demo.py | 42 ++++++++------------ 3 files changed, 108 insertions(+), 27 deletions(-) create mode 100644 ProxyFilter/Brook.py diff --git a/ProxyFilter/Brook.py b/ProxyFilter/Brook.py new file mode 100644 index 0000000..da766cf --- /dev/null +++ b/ProxyFilter/Brook.py @@ -0,0 +1,90 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +from ProxyFilter import baseFunc + +brookFilterRules = { + 'rootObject': { + 'remark': { + 'optional': False, + 'default': '', + 'type': str, + 'format': baseFunc.toStr + }, + 'server': { + 'optional': True, + 'type': str, + 'format': baseFunc.toStrTidy, + 'filter': baseFunc.isHost, + 'errMsg': 'Illegal server address' + }, + 'port': { + 'optional': True, + 'type': int, + 'format': baseFunc.toInt, + 'filter': baseFunc.isPort, + 'errMsg': 'Illegal port number' + }, + 'passwd': { + 'optional': True, + 'type': str, + 'format': baseFunc.toStr + }, + 'ws': { + 'optional': False, + 'default': None, + 'allowNone': True, + 'type': 'wsObject' + } + }, + 'wsObject': { + 'host': { + 'optional': False, + 'default': '', + 'type': str, + 'format': baseFunc.toStr + }, + 'path': { + 'optional': False, + 'default': '/', + 'type': str, + 'format': baseFunc.toStr + }, + 'secure': { + 'optional': False, + 'default': None, + 'allowNone': True, + 'type': 'secureObject' + } + }, + 'secureObject': { + 'verify': { + 'optional': False, + 'default': True, + 'type': bool, + 'format': baseFunc.toBool + } + } +} + +def brookFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: + """ + Brook节点合法性检查 + + 不合法: + return False, {reason} + + 合法: + return True, { + 'type': 'brook', + ... + } + """ + try: + if not isExtra: # 去除非必要参数 + brookFilterRules['rootObject'].pop('remark') + return baseFunc.ruleFilter(rawInfo, brookFilterRules, { + 'type': 'brook' + }) + except: + return False, 'Unknown error' diff --git a/ProxyFilter/filter.py b/ProxyFilter/filter.py index 366b664..a57c7e5 100644 --- a/ProxyFilter/filter.py +++ b/ProxyFilter/filter.py @@ -7,6 +7,7 @@ from ProxyFilter import VMess from ProxyFilter import VLESS from ProxyFilter import Trojan from ProxyFilter import TrojanGo +from ProxyFilter import Brook def filte(raw: dict, isExtra: bool = False) -> tuple[bool, str or dict]: """ @@ -37,6 +38,8 @@ def filte(raw: dict, isExtra: bool = False) -> tuple[bool, str or dict]: return Trojan.trojanFilter(raw, isExtra) elif raw['type'] == 'trojan-go': return TrojanGo.trojanGoFilter(raw, isExtra) + elif raw['type'] == 'brook': + return Brook.brookFilter(raw, isExtra) else: return False, 'Unknown proxy type' except: diff --git a/demo.py b/demo.py index eb9b160..494645a 100644 --- a/demo.py +++ b/demo.py @@ -5,32 +5,20 @@ import ProxyDecoder as Decoder import ProxyFilter as Filter import Check as Checker -# info = { -# 'type': 'trojan-go', -# 'server': '127.0.0.1', -# 'port': 12345, -# 'passwd': 'dnomd343', -# 'sni': 'local.343.re', -# 'plugin': { -# 'type': 'simple-tls', -# 'param': 'n=local.343.re;no-verify' -# } -# } -# -# status, ret = Filter.filte(info, isExtra = True) -# print(status) -# print(ret) -# -# data = Checker.proxyTest({ -# 'check': ['http'], -# 'info': ret -# }) -# -# print(data) +info = { + 'type': 'brook', + 'server': '127.0.0.1', + 'port': '12345', + 'passwd': 'dnomd343', + 'ws': { + 'host': 'local.343.re', + 'path': '/test', + 'secure': { + 'verify': False + } + } +} -url = 'trojan-go://password1234@google.com/?sni=microsoft.com&type=ws&host=youtube.com&path=%2Fgo&encryption=ss%3Baes-256-gcm%3Afuckgfw&plugin=obfs-local%3Bobfs%3Dhttp%3Bobfs-host%3Dwww.bing.com#server%20name' -ret = Decoder.decode(url) -print(ret) - -ret = Filter.filte(ret) +status, ret = Filter.filte(info) +print(status) print(ret)