|
@ -92,14 +92,60 @@ def __ssrFormat(raw: dict) -> dict: # 容错性格式化 |
|
|
raw['server'] = raw['server'].strip() |
|
|
raw['server'] = raw['server'].strip() |
|
|
raw['port'] = int(raw['port']) |
|
|
raw['port'] = int(raw['port']) |
|
|
raw['method'] = raw['method'].replace('_', '-').lower().strip() |
|
|
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['protocol'] = raw['protocol'].replace('-', '_').lower().strip() |
|
|
raw['obfs'] = raw['obfs'].replace('-', '_').lower().strip() |
|
|
raw['obfs'] = raw['obfs'].replace('-', '_').lower().strip() |
|
|
except: |
|
|
except: |
|
|
pass |
|
|
pass |
|
|
return raw |
|
|
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' |
|
|
|
|
|
|
|
|
def ssrFilter(raw: dict) -> tuple[bool, str or dict]: |
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
def ssrFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: |
|
|
""" |
|
|
""" |
|
|
ShadowsocksR节点合法性检查 |
|
|
ShadowsocksR节点合法性检查 |
|
|
|
|
|
|
|
@ -113,18 +159,29 @@ def ssrFilter(raw: dict) -> tuple[bool, str or dict]: |
|
|
} |
|
|
} |
|
|
""" |
|
|
""" |
|
|
try: |
|
|
try: |
|
|
|
|
|
raw = rawInfo |
|
|
if 'server' not in raw: # 必选值检查 |
|
|
raw = __ssrFormat(__ssrFill(raw)) # 预处理 |
|
|
return False, 'Missing `server` option' |
|
|
status, reason = __ssrParamCheck(raw) # 参数检查 |
|
|
if 'port' not in raw: |
|
|
if not status: # 参数有误 |
|
|
return False, 'Missing `port` option' |
|
|
return False, reason |
|
|
if 'method' not in raw: |
|
|
|
|
|
return False, 'Missing `method` option' |
|
|
|
|
|
if 'passwd' not in raw: |
|
|
|
|
|
return False, 'Missing `password` option' |
|
|
|
|
|
raw = __ssrFormat(__ssrFill(raw)) # 预处理 |
|
|
|
|
|
|
|
|
|
|
|
result = {'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']): |
|
|
if baseFunc.isHost(raw['server']): |
|
|
result['server'] = raw['server'] # server |
|
|
result['server'] = raw['server'] # server |
|
|
else: |
|
|
else: |
|
@ -139,7 +196,7 @@ def ssrFilter(raw: dict) -> tuple[bool, str or dict]: |
|
|
return False, 'Unknown ShadowsocksR method' |
|
|
return False, 'Unknown ShadowsocksR method' |
|
|
result['passwd'] = raw['passwd'] # passwd |
|
|
result['passwd'] = raw['passwd'] # passwd |
|
|
|
|
|
|
|
|
if raw['protocol'] in [None, '', 'origin']: # 默认协议 |
|
|
if raw['protocol'] in ['', 'origin']: # 默认协议 |
|
|
result['protocol'] = 'origin' |
|
|
result['protocol'] = 'origin' |
|
|
result['protocolParam'] = '' |
|
|
result['protocolParam'] = '' |
|
|
else: |
|
|
else: |
|
@ -149,7 +206,7 @@ def ssrFilter(raw: dict) -> tuple[bool, str or dict]: |
|
|
else: |
|
|
else: |
|
|
return False, 'Unknown ShadowsocksR protocol' |
|
|
return False, 'Unknown ShadowsocksR protocol' |
|
|
|
|
|
|
|
|
if raw['obfs'] in [None, '', 'plain']: # 默认混淆 |
|
|
if raw['obfs'] in ['', 'plain']: # 默认混淆 |
|
|
result['obfs'] = 'plain' |
|
|
result['obfs'] = 'plain' |
|
|
result['obfsParam'] = '' |
|
|
result['obfsParam'] = '' |
|
|
else: |
|
|
else: |
|
|