Browse Source

refactor: change CRLF to LF

master
Dnomd343 2 years ago
parent
commit
270df1d187
  1. 664
      ProxyFilter/VMess.py

664
ProxyFilter/VMess.py

@ -1,332 +1,332 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
from ProxyFilter import baseFunc from ProxyFilter import baseFunc
vmessMethodList = [ vmessMethodList = [
'aes-128-gcm', 'aes-128-gcm',
'chacha20-poly1305', 'chacha20-poly1305',
'auto', 'auto',
'none', 'none',
'zero', 'zero',
] ]
udpObfsList = [ udpObfsList = [
'none', 'none',
'srtp', 'srtp',
'utp', 'utp',
'wechat-video', 'wechat-video',
'dtls', 'dtls',
'wireguard' 'wireguard'
] ]
quicMethodList = [ quicMethodList = [
'none', 'none',
'aes-128-gcm', 'aes-128-gcm',
'chacha20-poly1305', 'chacha20-poly1305',
] ]
vmessFilterRules = { vmessFilterRules = {
'rootObject': { 'rootObject': {
'remark': { 'remark': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'server': { 'server': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'format': baseFunc.toStrTidy, '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': baseFunc.toInt, 'format': baseFunc.toInt,
'filter': baseFunc.isPort, 'filter': baseFunc.isPort,
'errMsg': 'Illegal port number' 'errMsg': 'Illegal port number'
}, },
'method': { 'method': {
'optional': False, 'optional': False,
'default': 'auto', 'default': 'auto',
'type': str, 'type': str,
'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'), 'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'),
'filter': lambda method: method in vmessMethodList, 'filter': lambda method: method in vmessMethodList,
'errMsg': 'Unknown VMess method' 'errMsg': 'Unknown VMess method'
}, },
'id': { 'id': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'aid': { 'aid': {
'optional': False, 'optional': False,
'default': 0, 'default': 0,
'type': int, 'type': int,
'format': baseFunc.toInt, 'format': baseFunc.toInt,
'filter': lambda aid: aid in range(0, 65536), # 0 ~ 65535 'filter': lambda aid: aid in range(0, 65536), # 0 ~ 65535
'errMsg': 'Illegal alter Id' 'errMsg': 'Illegal alter Id'
}, },
'stream': { 'stream': {
'optional': False, 'optional': False,
'default': { 'default': {
'type': 'tcp' 'type': 'tcp'
}, },
'type': [ 'type': [
'tcpObject', 'tcpObject',
'kcpObject', 'kcpObject',
'wsObject', 'wsObject',
'h2Object', 'h2Object',
'quicObject', 'quicObject',
'grpcObject', 'grpcObject',
] ]
} }
}, },
'tcpObject': { 'tcpObject': {
'type': { 'type': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'indexKey': True, 'indexKey': True,
'format': baseFunc.toStrTidy, 'format': baseFunc.toStrTidy,
'filter': lambda streamType: streamType == 'tcp', 'filter': lambda streamType: streamType == 'tcp',
'errMsg': 'Unexpected stream type' 'errMsg': 'Unexpected stream type'
}, },
'obfs': { 'obfs': {
'optional': False, 'optional': False,
'default': None, 'default': None,
'allowNone': True, 'allowNone': True,
'type': 'obfsObject' 'type': 'obfsObject'
}, },
'secure': { 'secure': {
'optional': False, 'optional': False,
'default': None, 'default': None,
'allowNone': True, 'allowNone': True,
'type': 'secureObject' 'type': 'secureObject'
} }
}, },
'kcpObject': { 'kcpObject': {
'type': { 'type': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'indexKey': True, 'indexKey': True,
'format': baseFunc.toStrTidy, 'format': baseFunc.toStrTidy,
'filter': lambda streamType: streamType == 'kcp', 'filter': lambda streamType: streamType == 'kcp',
'errMsg': 'Unexpected stream type' 'errMsg': 'Unexpected stream type'
}, },
'seed': { 'seed': {
'optional': False, 'optional': False,
'default': None, 'default': None,
'allowNone': True, 'allowNone': True,
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'obfs': { 'obfs': {
'optional': False, 'optional': False,
'default': 'none', 'default': 'none',
'type': str, 'type': str,
'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'), 'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'),
'filter': lambda obfs: obfs in udpObfsList, 'filter': lambda obfs: obfs in udpObfsList,
'errMsg': 'Unknown mKCP obfs method' 'errMsg': 'Unknown mKCP obfs method'
}, },
'secure': { 'secure': {
'optional': False, 'optional': False,
'default': None, 'default': None,
'allowNone': True, 'allowNone': True,
'type': 'secureObject' 'type': 'secureObject'
} }
}, },
'wsObject': { 'wsObject': {
'type': { 'type': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'indexKey': True, 'indexKey': True,
'format': baseFunc.toStrTidy, 'format': baseFunc.toStrTidy,
'filter': lambda streamType: streamType == 'ws', 'filter': lambda streamType: streamType == 'ws',
'errMsg': 'Unexpected stream type' 'errMsg': 'Unexpected stream type'
}, },
'host': { 'host': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'path': { 'path': {
'optional': False, 'optional': False,
'default': '/', 'default': '/',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'ed': { 'ed': {
'optional': False, 'optional': False,
'default': None, 'default': None,
'allowNone': True, 'allowNone': True,
'type': int, 'type': int,
'format': baseFunc.toInt, 'format': baseFunc.toInt,
'filter': lambda ed: ed > 0, 'filter': lambda ed: ed > 0,
'errMsg': 'Illegal Max-Early-Data length' 'errMsg': 'Illegal Max-Early-Data length'
}, },
'secure': { 'secure': {
'optional': False, 'optional': False,
'default': None, 'default': None,
'allowNone': True, 'allowNone': True,
'type': 'secureObject' 'type': 'secureObject'
} }
}, },
'h2Object': { 'h2Object': {
'type': { 'type': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'indexKey': True, 'indexKey': True,
'format': baseFunc.toStrTidy, 'format': baseFunc.toStrTidy,
'filter': lambda streamType: streamType == 'h2', 'filter': lambda streamType: streamType == 'h2',
'errMsg': 'Unexpected stream type' 'errMsg': 'Unexpected stream type'
}, },
'host': { 'host': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'path': { 'path': {
'optional': False, 'optional': False,
'default': '/', 'default': '/',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'secure': { 'secure': {
'optional': False, 'optional': False,
'default': None, 'default': None,
'allowNone': True, 'allowNone': True,
'type': 'secureObject' 'type': 'secureObject'
} }
}, },
'quicObject': { 'quicObject': {
'type': { 'type': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'indexKey': True, 'indexKey': True,
'format': baseFunc.toStrTidy, 'format': baseFunc.toStrTidy,
'filter': lambda streamType: streamType == 'quic', 'filter': lambda streamType: streamType == 'quic',
'errMsg': 'Unexpected stream type' 'errMsg': 'Unexpected stream type'
}, },
'method': { 'method': {
'optional': False, 'optional': False,
'default': 'none', 'default': 'none',
'type': str, 'type': str,
'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'), 'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'),
'filter': lambda method: method in quicMethodList, 'filter': lambda method: method in quicMethodList,
'errMsg': 'Unknown QUIC method' 'errMsg': 'Unknown QUIC method'
}, },
'passwd': { 'passwd': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'obfs': { 'obfs': {
'optional': False, 'optional': False,
'default': 'none', 'default': 'none',
'type': str, 'type': str,
'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'), 'format': lambda s: baseFunc.toStrTidy(s).replace('_', '-'),
'filter': lambda obfs: obfs in udpObfsList, 'filter': lambda obfs: obfs in udpObfsList,
'errMsg': 'Unknown QUIC obfs method' 'errMsg': 'Unknown QUIC obfs method'
}, },
'secure': { 'secure': {
'optional': False, 'optional': False,
'default': {}, 'default': {},
'type': 'secureObject' 'type': 'secureObject'
} }
}, },
'grpcObject': { 'grpcObject': {
'type': { 'type': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'indexKey': True, 'indexKey': True,
'format': baseFunc.toStrTidy, 'format': baseFunc.toStrTidy,
'filter': lambda streamType: streamType == 'grpc', 'filter': lambda streamType: streamType == 'grpc',
'errMsg': 'Unexpected stream type' 'errMsg': 'Unexpected stream type'
}, },
'service': { 'service': {
'optional': True, 'optional': True,
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'secure': { 'secure': {
'optional': False, 'optional': False,
'default': None, 'default': None,
'allowNone': True, 'allowNone': True,
'type': 'secureObject' 'type': 'secureObject'
} }
}, },
'obfsObject': { 'obfsObject': {
'host': { 'host': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'path': { 'path': {
'optional': False, 'optional': False,
'default': '/', 'default': '/',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
} }
}, },
'secureObject': { 'secureObject': {
'sni': { 'sni': {
'optional': False, 'optional': False,
'default': '', 'default': '',
'type': str, 'type': str,
'format': baseFunc.toStr 'format': baseFunc.toStr
}, },
'alpn': { 'alpn': {
'optional': False, 'optional': False,
'default': 'h2,http/1.1', 'default': 'h2,http/1.1',
'type': str, 'type': str,
'format': baseFunc.toStrTidy, 'format': baseFunc.toStrTidy,
'filter': lambda alpn: alpn in ['h2', 'http/1.1', 'h2,http/1.1'], 'filter': lambda alpn: alpn in ['h2', 'http/1.1', 'h2,http/1.1'],
'errMsg': 'Illegal alpn option' 'errMsg': 'Illegal alpn option'
}, },
'verify': { 'verify': {
'optional': False, 'optional': False,
'default': True, 'default': True,
'type': bool, 'type': bool,
'format': baseFunc.toBool 'format': baseFunc.toBool
} }
} }
} }
def vmessFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: def vmessFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]:
""" """
VMess节点合法性检查 VMess节点合法性检查
不合法: 不合法:
return False, {reason} return False, {reason}
合法: 合法:
return True, { return True, {
'type': 'vmess', 'type': 'vmess',
... ...
} }
""" """
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'] stream = result['stream']
if stream['secure'] is not None and stream['secure']['sni'] == '': # 未指定SNI if stream['secure'] is not None and stream['secure']['sni'] == '': # 未指定SNI
if stream['type'] == 'tcp' and stream['obfs'] is not None: if stream['type'] == 'tcp' and stream['obfs'] is not None:
stream['secure']['sni'] = stream['obfs']['host'].split(',')[0] stream['secure']['sni'] = stream['obfs']['host'].split(',')[0]
elif stream['type'] == 'ws': elif stream['type'] == 'ws':
stream['secure']['sni'] = stream['host'] stream['secure']['sni'] = stream['host']
elif stream['type'] == 'h2': elif stream['type'] == 'h2':
stream['secure']['sni'] = stream['host'].split(',')[0] stream['secure']['sni'] = stream['host'].split(',')[0]
return True, result return True, result
except: except:
return False, 'Unknown error' return False, 'Unknown error'

Loading…
Cancel
Save