Browse Source

update: filter of shadowsocksr

master
Dnomd343 3 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: try:
if not isExtra: if not isExtra:
ssFilterRules['rootObject'].pop('remark') ssFilterRules['rootObject'].pop('remark')
return baseFunc.rulesFilter(rawInfo, ssFilterRules) return baseFunc.rulesFilter(rawInfo, ssFilterRules, {
'type': 'ss'
})
except: except:
return False, 'Unknown error' return False, 'Unknown error'

199
ProxyFilter/ShadowsocksR.py

@ -73,77 +73,71 @@ ssrObfsList = [ # ShadowsocksR混淆方式
'random_head', 'random_head',
] ]
def __ssrFill(raw: dict) -> dict: # 补全可选值 ssrFilterRules = {
try: 'rootObject': {
if 'protocol' not in raw: 'remark': {
raw['protocol'] = '' 'optional': False,
if 'protocolParam' not in raw: 'default': '',
raw['protocolParam'] = '' 'type': str
if 'obfs' not in raw: },
raw['obfs'] = '' 'group': {
if 'obfsParam' not in raw: 'optional': False,
raw['obfsParam'] = '' 'default': '',
except: 'type': str
pass },
return raw 'server': {
'optional': True,
def __ssrFormat(raw: dict) -> dict: # 容错性格式化 'type': str,
try: 'format': lambda s: s.lower().strip(),
raw['server'] = raw['server'].strip() 'filter': baseFunc.isHost,
raw['port'] = int(raw['port']) 'errMsg': 'Illegal server address'
raw['method'] = raw['method'].replace('_', '-').lower().strip() },
if raw['protocol'] is None: 'port': {
raw['protocol'] = '' 'optional': True,
if raw['protocolParam'] is None: 'type': int,
raw['protocolParam'] = '' 'format': lambda i: int(i),
if raw['obfs'] is None: 'filter': baseFunc.isPort,
raw['obfs'] = '' 'errMsg': 'Illegal port number'
if raw['obfsParam'] is None: },
raw['obfsParam'] = '' 'method': {
raw['protocol'] = raw['protocol'].replace('-', '_').lower().strip() 'optional': True,
raw['obfs'] = raw['obfs'].replace('-', '_').lower().strip() 'type': str,
except: 'format': lambda s: s.replace('_', '-').lower().strip(),
pass 'filter': lambda method: method in ssrMethodList,
return raw 'errMsg': 'Unknown ShadowsocksR method'
},
def __ssrParamCheck(raw: dict) -> tuple[bool, str or None]: # 参数检查 'passwd': {
try: 'optional': True,
if 'server' not in raw: 'type': str
return False, 'Missing `server` option' },
if 'port' not in raw: 'protocol': {
return False, 'Missing `port` option' 'optional': False,
if 'method' not in raw: 'default': 'origin',
return False, 'Missing `method` option' 'type': str,
if 'passwd' not in raw: 'format': lambda s: s.replace('-', '_').lower().strip(),
return False, 'Missing `passwd` option' 'filter': lambda method: method in ssrProtocolList,
if 'protocol' not in raw: 'errMsg': 'Unknown ShadowsocksR protocol'
return False, 'Missing `protocol` option' },
if 'protocolParam' not in raw: 'protocolParam': {
return False, 'Missing `protocolParam` option' 'optional': False,
if 'obfs' not in raw: 'default': '',
return False, 'Missing `obfs` option' 'type': str
if 'obfsParam' not in raw: },
return False, 'Missing `obfsParam` option' 'obfs': {
'optional': False,
if not isinstance(raw['server'], str): 'default': 'plain',
return False, 'Illegal `server` option' 'type': str,
if not isinstance(raw['port'], int): 'format': lambda s: s.replace('-', '_').lower().strip(),
return False, 'Illegal `int` option' 'filter': lambda method: method in ssrObfsList,
if not isinstance(raw['method'], str): 'errMsg': 'Unknown ShadowsocksR obfs'
return False, 'Illegal `method` option' },
if not isinstance(raw['passwd'], str): 'obfsParam': {
return False, 'Illegal `passwd` option' 'optional': False,
if not isinstance(raw['protocol'], str): 'default': '',
return False, 'Illegal `protocol` option' 'type': str
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
def ssrFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: 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: try:
raw = rawInfo if not isExtra:
raw = __ssrFormat(__ssrFill(raw)) # 预处理 ssrFilterRules['rootObject'].pop('remark')
status, reason = __ssrParamCheck(raw) # 参数检查 ssrFilterRules['rootObject'].pop('group')
if not status: # 参数有误 return baseFunc.rulesFilter(rawInfo, ssrFilterRules, {
return False, reason 'type': 'ssr'
})
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'
except: except:
return False, 'Unknown error' 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] result[key] = data[key]
return result 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 -> 必选 optional -> 必选
@ -100,6 +100,6 @@ def rulesFilter(rawData: dict, rulesList: dict) -> tuple[bool, dict or str]:
except filterException as reason: # 节点格式错误 except filterException as reason: # 节点格式错误
return False, str(reason) return False, str(reason)
except: except:
return False, 'Filter error' return False, 'Format error'
else: else:
return True, data return True, {**header, **data}

Loading…
Cancel
Save