|
@ -1,10 +1,41 @@ |
|
|
#!/usr/bin/python |
|
|
#!/usr/bin/python |
|
|
# -*- coding:utf-8 -*- |
|
|
# -*- coding:utf-8 -*- |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
'server': '...', |
|
|
|
|
|
'port': ..., |
|
|
|
|
|
'method': '...', |
|
|
|
|
|
'passwd': '...', |
|
|
|
|
|
'plugin': pluginObject |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
server (str) -> required |
|
|
|
|
|
|
|
|
|
|
|
port (int or str) -> required |
|
|
|
|
|
|
|
|
|
|
|
method (str) -> required |
|
|
|
|
|
|
|
|
|
|
|
passwd (str) -> required |
|
|
|
|
|
|
|
|
|
|
|
plugin (None or dict) -> optional |
|
|
|
|
|
|
|
|
|
|
|
pluginObject: { |
|
|
|
|
|
'type': '...', |
|
|
|
|
|
'param': '...' |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type (str) -> optional |
|
|
|
|
|
|
|
|
|
|
|
param (str) -> optional |
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
from ProxyFilter import baseFunc |
|
|
from ProxyFilter import baseFunc |
|
|
from ProxyFilter import Plugin as sip003 |
|
|
from ProxyFilter import Plugin as sip003 |
|
|
|
|
|
|
|
|
ssMethodList = [ |
|
|
ssMethodList = [ # Shadowsocks加密方式 |
|
|
'aes-128-gcm', |
|
|
'aes-128-gcm', |
|
|
'aes-192-gcm', |
|
|
'aes-192-gcm', |
|
|
'aes-256-gcm', |
|
|
'aes-256-gcm', |
|
@ -56,7 +87,7 @@ ssMethodList = [ |
|
|
'xchacha20-ietf-poly1305' |
|
|
'xchacha20-ietf-poly1305' |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
pluginList = [ |
|
|
pluginList = [ # SIP003插件列表 |
|
|
'obfs-local', |
|
|
'obfs-local', |
|
|
'simple-tls', |
|
|
'simple-tls', |
|
|
'v2ray-plugin', |
|
|
'v2ray-plugin', |
|
@ -71,18 +102,32 @@ pluginList = [ |
|
|
'gun-plugin' |
|
|
'gun-plugin' |
|
|
] |
|
|
] |
|
|
|
|
|
|
|
|
def __ssFormat(raw): # 容错性格式化 |
|
|
def __ssFill(raw: dict) -> dict: # 补全可选值 |
|
|
|
|
|
try: |
|
|
|
|
|
if 'plugin' not in raw: |
|
|
|
|
|
raw['plugin'] = None |
|
|
|
|
|
if raw['plugin'] is not None: |
|
|
|
|
|
if 'type' not in raw['plugin']: |
|
|
|
|
|
raw['plugin']['type'] = '' |
|
|
|
|
|
if 'param' not in raw['plugin']: |
|
|
|
|
|
raw['plugin']['param'] = '' |
|
|
|
|
|
except: |
|
|
|
|
|
pass |
|
|
|
|
|
return raw |
|
|
|
|
|
|
|
|
|
|
|
def __ssFormat(raw: dict) -> dict: # 容错性格式化 |
|
|
try: |
|
|
try: |
|
|
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() |
|
|
raw['plugin'] = sip003.pluginFormat(raw['plugin']) |
|
|
if raw['plugin'] is not None: |
|
|
|
|
|
raw['plugin']['type'] = sip003.pluginFormat(raw['plugin']['type']) |
|
|
except: |
|
|
except: |
|
|
pass |
|
|
pass |
|
|
return raw |
|
|
return raw |
|
|
|
|
|
|
|
|
def ssFilter(raw): |
|
|
def ssFilter(raw: dict) -> tuple[bool, str or dict]: |
|
|
''' |
|
|
""" |
|
|
Shadowsocks节点合法性检查 |
|
|
Shadowsocks节点合法性检查 |
|
|
|
|
|
|
|
|
不合法: |
|
|
不合法: |
|
@ -91,51 +136,46 @@ def ssFilter(raw): |
|
|
合法: |
|
|
合法: |
|
|
return True, { |
|
|
return True, { |
|
|
'type': 'ss', |
|
|
'type': 'ss', |
|
|
'server': '...', |
|
|
... |
|
|
'port': ..., |
|
|
|
|
|
'password': '...', |
|
|
|
|
|
'method": '...', |
|
|
|
|
|
'plugin": '...', |
|
|
|
|
|
'pluginParam": '...' |
|
|
|
|
|
} |
|
|
} |
|
|
''' |
|
|
""" |
|
|
try: |
|
|
try: |
|
|
result = {} |
|
|
if 'server' not in raw: # 必选值检查 |
|
|
result['type'] = 'ss' |
|
|
|
|
|
raw = __ssFormat(raw) |
|
|
|
|
|
|
|
|
|
|
|
if not 'server' in raw: |
|
|
|
|
|
return False, 'Missing `server` option' |
|
|
return False, 'Missing `server` option' |
|
|
if not 'port' in raw: |
|
|
if 'port' not in raw: |
|
|
return False, 'Missing `port` option' |
|
|
return False, 'Missing `port` option' |
|
|
if not 'password' in raw: |
|
|
if 'method' not in raw: |
|
|
return False, 'Missing `password` option' |
|
|
|
|
|
if not 'method' in raw: |
|
|
|
|
|
return False, 'Missing `method` option' |
|
|
return False, 'Missing `method` option' |
|
|
|
|
|
if 'passwd' not in raw: |
|
|
|
|
|
return False, 'Missing `passwd` option' |
|
|
|
|
|
raw = __ssFormat(__ssFill(raw)) # 预处理 |
|
|
|
|
|
|
|
|
|
|
|
result = {'type': 'ss'} |
|
|
if baseFunc.isHost(raw['server']): |
|
|
if baseFunc.isHost(raw['server']): |
|
|
result['server'] = raw['server'] |
|
|
result['server'] = raw['server'] # server |
|
|
else: |
|
|
else: |
|
|
return False, 'Illegal `server` option' |
|
|
return False, 'Illegal `server` option' |
|
|
if baseFunc.isPort(raw['port']): |
|
|
if baseFunc.isPort(raw['port']): |
|
|
result['port'] = raw['port'] |
|
|
result['port'] = raw['port'] # port |
|
|
else: |
|
|
else: |
|
|
return False, 'Illegal `port` option' |
|
|
return False, 'Illegal `port` option' |
|
|
result['password'] = raw['password'] |
|
|
|
|
|
if raw['method'] in ssMethodList: |
|
|
if raw['method'] in ssMethodList: |
|
|
result['method'] = raw['method'] |
|
|
result['method'] = raw['method'] # method |
|
|
else: |
|
|
else: |
|
|
return False, 'Unknown Shadowsocks method' |
|
|
return False, 'Unknown Shadowsocks method' |
|
|
|
|
|
result['passwd'] = raw['passwd'] # passwd |
|
|
|
|
|
|
|
|
if (not 'plugin' in raw) or raw['plugin'] == '': |
|
|
if raw['plugin'] is None or raw['plugin']['type'] in [None, '']: |
|
|
result['plugin'] = '' |
|
|
plugin = None |
|
|
result['pluginParam'] = '' |
|
|
|
|
|
else: |
|
|
else: |
|
|
if raw['plugin'] in pluginList: |
|
|
if raw['plugin']['type'] in pluginList: |
|
|
result['plugin'] = raw['plugin'] |
|
|
plugin = { |
|
|
result['pluginParam'] = raw['pluginParam'] |
|
|
'type': raw['plugin']['type'], |
|
|
|
|
|
'param': raw['plugin']['param'] |
|
|
|
|
|
} |
|
|
else: |
|
|
else: |
|
|
return False, 'Unknown sip003 plugin' |
|
|
return False, 'Unknown sip003 plugin' |
|
|
|
|
|
result['plugin'] = plugin |
|
|
except: |
|
|
except: |
|
|
return False, 'Unknown error' |
|
|
return False, 'Unknown error' |
|
|
return True, result |
|
|
return True, result |
|
|