Browse Source

update: filter of shadowsocksr

master
Dnomd343 2 years ago
parent
commit
aaa1ed7dbd
  1. 4
      ProxyFilter/Shadowsocks.py
  2. 199
      ProxyFilter/ShadowsocksR.py
  3. 6
      ProxyFilter/baseFunc.py

4
ProxyFilter/Shadowsocks.py

@ -127,6 +127,8 @@ def ssFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]:
try:
if not isExtra:
ssFilterRules['rootObject'].pop('remark')
return baseFunc.rulesFilter(rawInfo, ssFilterRules)
return baseFunc.rulesFilter(rawInfo, ssFilterRules, {
'type': 'ss'
})
except:
return False, 'Unknown error'

199
ProxyFilter/ShadowsocksR.py

@ -73,77 +73,71 @@ ssrObfsList = [ # ShadowsocksR混淆方式
'random_head',
]
def __ssrFill(raw: dict) -> dict: # 补全可选值
try:
if 'protocol' not in raw:
raw['protocol'] = ''
if 'protocolParam' not in raw:
raw['protocolParam'] = ''
if 'obfs' not in raw:
raw['obfs'] = ''
if 'obfsParam' not in raw:
raw['obfsParam'] = ''
except:
pass
return raw
def __ssrFormat(raw: dict) -> dict: # 容错性格式化
try:
raw['server'] = raw['server'].strip()
raw['port'] = int(raw['port'])
raw['method'] = raw['method'].replace('_', '-').lower().strip()
if raw['protocol'] is None:
raw['protocol'] = ''
if raw['protocolParam'] is None:
raw['protocolParam'] = ''
if raw['obfs'] is None:
raw['obfs'] = ''
if raw['obfsParam'] is None:
raw['obfsParam'] = ''
raw['protocol'] = raw['protocol'].replace('-', '_').lower().strip()
raw['obfs'] = raw['obfs'].replace('-', '_').lower().strip()
except:
pass
return raw
def __ssrParamCheck(raw: dict) -> tuple[bool, str or None]: # 参数检查
try:
if 'server' not in raw:
return False, 'Missing `server` option'
if 'port' not in raw:
return False, 'Missing `port` option'
if 'method' not in raw:
return False, 'Missing `method` option'
if 'passwd' not in raw:
return False, 'Missing `passwd` option'
if 'protocol' not in raw:
return False, 'Missing `protocol` option'
if 'protocolParam' not in raw:
return False, 'Missing `protocolParam` option'
if 'obfs' not in raw:
return False, 'Missing `obfs` option'
if 'obfsParam' not in raw:
return False, 'Missing `obfsParam` option'
if not isinstance(raw['server'], str):
return False, 'Illegal `server` option'
if not isinstance(raw['port'], int):
return False, 'Illegal `int` option'
if not isinstance(raw['method'], str):
return False, 'Illegal `method` option'
if not isinstance(raw['passwd'], str):
return False, 'Illegal `passwd` option'
if not isinstance(raw['protocol'], str):
return False, 'Illegal `protocol` option'
if not isinstance(raw['protocolParam'], str):
return False, 'Illegal `protocolParam` option'
if not isinstance(raw['obfs'], str):
return False, 'Illegal `obfs` option'
if not isinstance(raw['obfsParam'], str):
return False, 'Illegal `obfsParam` option'
except:
return False, 'Unknown error'
return True, None
ssrFilterRules = {
'rootObject': {
'remark': {
'optional': False,
'default': '',
'type': str
},
'group': {
'optional': False,
'default': '',
'type': str
},
'server': {
'optional': True,
'type': str,
'format': lambda s: s.lower().strip(),
'filter': baseFunc.isHost,
'errMsg': 'Illegal server address'
},
'port': {
'optional': True,
'type': int,
'format': lambda i: int(i),
'filter': baseFunc.isPort,
'errMsg': 'Illegal port number'
},
'method': {
'optional': True,
'type': str,
'format': lambda s: s.replace('_', '-').lower().strip(),
'filter': lambda method: method in ssrMethodList,
'errMsg': 'Unknown ShadowsocksR method'
},
'passwd': {
'optional': True,
'type': str
},
'protocol': {
'optional': False,
'default': 'origin',
'type': str,
'format': lambda s: s.replace('-', '_').lower().strip(),
'filter': lambda method: method in ssrProtocolList,
'errMsg': 'Unknown ShadowsocksR protocol'
},
'protocolParam': {
'optional': False,
'default': '',
'type': str
},
'obfs': {
'optional': False,
'default': 'plain',
'type': str,
'format': lambda s: s.replace('-', '_').lower().strip(),
'filter': lambda method: method in ssrObfsList,
'errMsg': 'Unknown ShadowsocksR obfs'
},
'obfsParam': {
'optional': False,
'default': '',
'type': str
}
}
}
def ssrFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]:
"""
@ -159,62 +153,11 @@ def ssrFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]:
}
"""
try:
raw = rawInfo
raw = __ssrFormat(__ssrFill(raw)) # 预处理
status, reason = __ssrParamCheck(raw) # 参数检查
if not status: # 参数有误
return False, reason
result = {'type': 'ssr'}
if isExtra: # 携带额外参数
if 'remark' not in raw: # 补全默认值
raw['remark'] = ''
if 'group' not in raw:
raw['group'] = ''
if raw['remark'] is None: # 容错格式化
raw['remark'] = ''
if raw['group'] is None:
raw['group'] = ''
if not isinstance(raw['remark'], str): # 参数检查
return False, 'Illegal `remark` option'
if not isinstance(raw['group'], str):
return False, 'Illegal `group` option'
result['remark'] = raw['remark']
result['group'] = raw['group']
if baseFunc.isHost(raw['server']):
result['server'] = raw['server'] # server
else:
return False, 'Illegal `server` option'
if baseFunc.isPort(raw['port']):
result['port'] = raw['port'] # port
else:
return False, 'Illegal `port` option'
if raw['method'] in ssrMethodList:
result['method'] = raw['method'] # method
else:
return False, 'Unknown ShadowsocksR method'
result['passwd'] = raw['passwd'] # passwd
if raw['protocol'] in ['', 'origin']: # 默认协议
result['protocol'] = 'origin'
result['protocolParam'] = ''
else:
if raw['protocol'] in ssrProtocolList:
result['protocol'] = raw['protocol']
result['protocolParam'] = raw['protocolParam']
else:
return False, 'Unknown ShadowsocksR protocol'
if raw['obfs'] in ['', 'plain']: # 默认混淆
result['obfs'] = 'plain'
result['obfsParam'] = ''
else:
if raw['obfs'] in ssrObfsList:
result['obfs'] = raw['obfs']
result['obfsParam'] = raw['obfsParam']
else:
return False, 'Unknown ShadowsocksR obfs'
if not isExtra:
ssrFilterRules['rootObject'].pop('remark')
ssrFilterRules['rootObject'].pop('group')
return baseFunc.rulesFilter(rawInfo, ssrFilterRules, {
'type': 'ssr'
})
except:
return False, 'Unknown error'
return True, result

6
ProxyFilter/baseFunc.py

@ -83,7 +83,7 @@ def __dictCheck(data: dict, objectList: dict, limitRules: dict, keyPrefix: str)
result[key] = data[key]
return result
def rulesFilter(rawData: dict, rulesList: dict) -> tuple[bool, dict or str]:
def rulesFilter(rawData: dict, rulesList: dict, header: dict) -> tuple[bool, dict or str]:
"""
规则参数
optional -> 必选
@ -100,6 +100,6 @@ def rulesFilter(rawData: dict, rulesList: dict) -> tuple[bool, dict or str]:
except filterException as reason: # 节点格式错误
return False, str(reason)
except:
return False, 'Filter error'
return False, 'Format error'
else:
return True, data
return True, {**header, **data}

Loading…
Cancel
Save