Browse Source

update: more compatibility in ProxyFilter

master
Dnomd343 3 years ago
parent
commit
0001bd1a89
  1. 21
      ProxyFilter/Shadowsocks.py
  2. 52
      ProxyFilter/ShadowsocksR.py
  3. 15
      ProxyFilter/VMess.py
  4. 1
      ProxyFilter/baseFunc.py
  5. 27
      demo.py

21
ProxyFilter/Shadowsocks.py

@ -61,32 +61,34 @@ ssFilterRules = {
'remark': { 'remark': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str 'type': str,
'format': baseFunc.toStr
}, },
'server': { 'server': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'format': lambda s: s.lower().strip(), 'format': baseFunc.toStrTidy,
'filter': baseFunc.isHost, 'filter': baseFunc.isHost,
'errMsg': 'Illegal server address' 'errMsg': 'Illegal server address'
}, },
'port': { 'port': {
'optional': True, 'optional': True,
'type': int, 'type': int,
'format': lambda i: int(i), 'format': baseFunc.toInt,
'filter': baseFunc.isPort, 'filter': baseFunc.isPort,
'errMsg': 'Illegal port number' 'errMsg': 'Illegal port number'
}, },
'method': { 'method': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'format': lambda s: s.replace('_', '-').lower().strip(), 'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'),
'filter': lambda method: method in ssMethodList, 'filter': lambda method: method in ssMethodList,
'errMsg': 'Unknown Shadowsocks method' 'errMsg': 'Unknown Shadowsocks method'
}, },
'passwd': { 'passwd': {
'optional': True, 'optional': True,
'type': str 'type': str,
'format': baseFunc.toStr
}, },
'plugin': { 'plugin': {
'optional': False, 'optional': False,
@ -99,14 +101,15 @@ ssFilterRules = {
'type': { 'type': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'format': Plugin.pluginFormat, 'format': lambda pluginType: Plugin.pluginFormat(baseFunc.toStrTidy(pluginType)),
'filter': Plugin.isPlugin, 'filter': Plugin.isPlugin,
'errMsg': 'Unknown SIP003 plugin' 'errMsg': 'Unknown SIP003 plugin'
}, },
'param': { 'param': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str 'type': str,
'format': baseFunc.toStr
} }
} }
} }
@ -125,9 +128,9 @@ def ssFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]:
} }
""" """
try: try:
if not isExtra: if not isExtra: # 去除非必要参数
ssFilterRules['rootObject'].pop('remark') ssFilterRules['rootObject'].pop('remark')
return baseFunc.rulesFilter(rawInfo, ssFilterRules, { return baseFunc.ruleFilter(rawInfo, ssFilterRules, {
'type': 'ss' 'type': 'ss'
}) })
except: except:

52
ProxyFilter/ShadowsocksR.py

@ -73,68 +73,85 @@ ssrObfsList = [ # ShadowsocksR混淆方式
'random_head', 'random_head',
] ]
def __ssrProtocol(protocol) -> str:
protocol = baseFunc.toStrTidy(protocol).replace('-', '_')
if protocol == '':
return 'origin'
return protocol
def __ssrObfs(obfs) -> str:
obfs = baseFunc.toStrTidy(obfs).replace('-', '_')
if obfs == '':
return 'plain'
return obfs
ssrFilterRules = { ssrFilterRules = {
'rootObject': { 'rootObject': {
'remark': { 'remark': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str 'type': str,
'format': baseFunc.toStr
}, },
'group': { 'group': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str 'type': str,
'format': baseFunc.toStr
}, },
'server': { 'server': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'format': lambda s: s.lower().strip(), 'format': baseFunc.toStrTidy,
'filter': baseFunc.isHost, 'filter': baseFunc.isHost,
'errMsg': 'Illegal server address' 'errMsg': 'Illegal server address'
}, },
'port': { 'port': {
'optional': True, 'optional': True,
'type': int, 'type': int,
'format': lambda i: int(i), 'format': baseFunc.toInt,
'filter': baseFunc.isPort, 'filter': baseFunc.isPort,
'errMsg': 'Illegal port number' 'errMsg': 'Illegal port number'
}, },
'method': { 'method': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'format': lambda s: s.replace('_', '-').lower().strip(), 'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'),
'filter': lambda method: method in ssrMethodList, 'filter': lambda method: method in ssrMethodList,
'errMsg': 'Unknown ShadowsocksR method' 'errMsg': 'Unknown ShadowsocksR method'
}, },
'passwd': { 'passwd': {
'optional': True, 'optional': True,
'type': str 'type': str,
'format': baseFunc.toStr
}, },
'protocol': { 'protocol': {
'optional': False, 'optional': False,
'default': 'origin', 'default': 'origin',
'type': str, 'type': str,
'format': lambda s: s.replace('-', '_').lower().strip(), 'format': __ssrProtocol,
'filter': lambda method: method in ssrProtocolList, 'filter': lambda protocol: protocol in ssrProtocolList,
'errMsg': 'Unknown ShadowsocksR protocol' 'errMsg': 'Unknown ShadowsocksR protocol'
}, },
'protocolParam': { 'protocolParam': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str 'type': str,
'format': baseFunc.toStr
}, },
'obfs': { 'obfs': {
'optional': False, 'optional': False,
'default': 'plain', 'default': 'plain',
'type': str, 'type': str,
'format': lambda s: s.replace('-', '_').lower().strip(), 'format': __ssrObfs,
'filter': lambda method: method in ssrObfsList, 'filter': lambda obfs: obfs in ssrObfsList,
'errMsg': 'Unknown ShadowsocksR obfs' 'errMsg': 'Unknown ShadowsocksR obfs'
}, },
'obfsParam': { 'obfsParam': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str 'type': str,
'format': baseFunc.toStr
} }
} }
} }
@ -153,11 +170,18 @@ def ssrFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]:
} }
""" """
try: try:
if not isExtra: if not isExtra: # 去除非必要参数
ssrFilterRules['rootObject'].pop('remark') ssrFilterRules['rootObject'].pop('remark')
ssrFilterRules['rootObject'].pop('group') ssrFilterRules['rootObject'].pop('group')
return baseFunc.rulesFilter(rawInfo, ssrFilterRules, { status, result = baseFunc.ruleFilter(rawInfo, ssrFilterRules, {
'type': 'ssr' 'type': 'ssr'
}) })
if not status: # 节点格式错误
return False, result
if result['protocol'] == 'origin': # origin无参数
result['protocolParam'] = ''
if result['obfs'] == 'plain': # plain无参数
result['obfsParam'] = ''
return True, result
except: except:
return False, 'Unknown error' return False, 'Unknown error'

15
ProxyFilter/VMess.py

@ -312,16 +312,21 @@ def vmessFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]:
} }
""" """
try: try:
if not isExtra: if not isExtra: # 去除非必要参数
vmessFilterRules['rootObject'].pop('remark') vmessFilterRules['rootObject'].pop('remark')
status, result = baseFunc.ruleFilter(rawInfo, vmessFilterRules, { status, result = baseFunc.ruleFilter(rawInfo, vmessFilterRules, {
'type': 'vmess' 'type': 'vmess'
}) })
if not status: if not status: # 节点格式错误
return False, result return False, result
stream = result['stream']
# TODO: host -> sni if stream['secure'] is not None and stream['secure']['sni'] == '': # 未指定SNI
if stream['type'] == 'tcp' and stream['obfs'] is not None:
stream['secure']['sni'] = stream['obfs']['host'].split(',')[0]
elif stream['type'] == 'ws':
stream['secure']['sni'] = stream['host']
elif stream['type'] == 'h2':
stream['secure']['sni'] = stream['host'].split(',')[0]
return True, result return True, result
except: except:
return False, 'Unknown error' return False, 'Unknown error'

1
ProxyFilter/baseFunc.py

@ -5,7 +5,6 @@ import re
import IPy import IPy
import copy import copy
def isHost(host: str) -> bool: def isHost(host: str) -> bool:
""" """
判断host是否合法 判断host是否合法

27
demo.py

@ -1,29 +1,20 @@
import ProxyFilter as Filter import ProxyFilter as Filter
info = { info = {
'type': 'vmess', 'type': 'ss',
'remark': 'ok', 'remark': 'ok',
'server': b'127.0.0.1 ', 'server': b'127.0.0.1 ',
'port': b"12345", 'port': b"12345",
'method': 'aes-128_gcm', 'method': 'aes-128_gcm',
'id': 'eb6273f1-a98f-59f6-ba52-945f11dee100', 'passwd': 'dnomd343',
'aid': '64', 'protocol': 'auth_chain-a',
'stream': { 'protocolParam': '123',
'type': 'tcp', # 'obfs': b"http-simple",
'obfs': { 'obfsParam': b'123',
'host': '343.re', 'plugin': {
'path': '/test' 'type': 'OBFS_server',
}, 'param': 'obfs=tls'
'secure': {}
} }
# 'stream': {
# 'type': 'grpc',
# 'service': 'test',
# 'secure': {
# 'sni': 'ip.343.re',
# 'verify': False
# }
# }
} }
status, data = Filter.filte(info, isExtra = True) status, data = Filter.filte(info, isExtra = True)

Loading…
Cancel
Save