diff --git a/ProxyFilter/Trojan.py b/ProxyFilter/Trojan.py index beb23b8..39e356b 100644 --- a/ProxyFilter/Trojan.py +++ b/ProxyFilter/Trojan.py @@ -71,14 +71,7 @@ def trojanFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: }) if not status: # 节点格式错误 return False, result - stream = result['stream'] - if stream['secure'] is not None and stream['secure']['sni'] == '': # 未指定SNI - if stream['type'] == 'tcp' and stream['obfs'] is not None: - stream['secure']['sni'] = stream['obfs']['host'].split(',')[0] - elif stream['type'] == 'ws': - stream['secure']['sni'] = stream['host'] - elif stream['type'] == 'h2': - stream['secure']['sni'] = stream['host'].split(',')[0] + Xray.addSni(result) return True, result except: return False, 'Unknown error' diff --git a/ProxyFilter/V2ray.py b/ProxyFilter/V2ray.py index 724a499..2cc1b13 100644 --- a/ProxyFilter/V2ray.py +++ b/ProxyFilter/V2ray.py @@ -240,3 +240,21 @@ v2rayStreamRules = { } } } + +def addSni(info: dict) -> None: + stream = info['stream'] + if stream['secure'] is None or stream['secure']['sni'] != '': # 未指定SNI + return + if baseFunc.isDomain(info['server']): + stream['secure']['sni'] = info['server'] + + sniContent = '' + if stream['type'] == 'tcp' and stream['obfs'] is not None: + sniContent = stream['obfs']['host'].split(',')[0] + elif stream['type'] == 'ws': + sniContent = stream['host'] + elif stream['type'] == 'h2': + sniContent = stream['host'].split(',')[0] + + if sniContent != '': + stream['secure']['sni'] = sniContent diff --git a/ProxyFilter/VLESS.py b/ProxyFilter/VLESS.py index 6b3c694..8f2d19a 100644 --- a/ProxyFilter/VLESS.py +++ b/ProxyFilter/VLESS.py @@ -81,14 +81,7 @@ def vlessFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: }) if not status: # 节点格式错误 return False, result - stream = result['stream'] - if stream['secure'] is not None and stream['secure']['sni'] == '': # 未指定SNI - if stream['type'] == 'tcp' and stream['obfs'] is not None: - stream['secure']['sni'] = stream['obfs']['host'].split(',')[0] - elif stream['type'] == 'ws': - stream['secure']['sni'] = stream['host'] - elif stream['type'] == 'h2': - stream['secure']['sni'] = stream['host'].split(',')[0] + Xray.addSni(result) return True, result except: return False, 'Unknown error' diff --git a/ProxyFilter/VMess.py b/ProxyFilter/VMess.py index 35d58cc..eacef70 100644 --- a/ProxyFilter/VMess.py +++ b/ProxyFilter/VMess.py @@ -95,14 +95,7 @@ def vmessFilter(rawInfo: dict, isExtra: bool) -> tuple[bool, str or dict]: }) if not status: # 节点格式错误 return False, result - stream = result['stream'] - if stream['secure'] is not None and stream['secure']['sni'] == '': # 未指定SNI - if stream['type'] == 'tcp' and stream['obfs'] is not None: - stream['secure']['sni'] = stream['obfs']['host'].split(',')[0] - elif stream['type'] == 'ws': - stream['secure']['sni'] = stream['host'] - elif stream['type'] == 'h2': - stream['secure']['sni'] = stream['host'].split(',')[0] + V2ray.addSni(result) return True, result except: return False, 'Unknown error' diff --git a/ProxyFilter/Xray.py b/ProxyFilter/Xray.py index 55ea4eb..a9f8e6d 100644 --- a/ProxyFilter/Xray.py +++ b/ProxyFilter/Xray.py @@ -106,3 +106,5 @@ xrayStreamRules['xtlsObject'] = { 'format': baseFunc.toBool } } + +addSni = V2ray.addSni diff --git a/ProxyFilter/baseFunc.py b/ProxyFilter/baseFunc.py index 9f4d6dd..33eba89 100644 --- a/ProxyFilter/baseFunc.py +++ b/ProxyFilter/baseFunc.py @@ -5,33 +5,59 @@ import re import IPy import copy -def isHost(host: str) -> bool: +def isIpAddr(content: str) -> bool: """ - 判断host是否合法 + 判断是否为IP地址(IPv4 / IPv6) - IPv4 / IPv6 / Domain + 域名: return True - 合法: return True + 非域名: return False - 不合法: return False """ try: - IPy.IP(host) - if host.find('/') != -1: # filter CIDR + if content.find('/') != -1: # filter CIDR return False - if host.find('.') == -1 and host.find(':') == -1: + if content.find('.') == -1 and content.find(':') == -1: return False + IPy.IP(content) return True # IP地址合法 except: - pass + return False + + +def isDomain(content: str) -> bool: + """ + 判断是否为域名 + + 域名: return True + + 非域名: return False + + """ try: return re.search( # 域名匹配 - r'^(?=^.{3,255}$)[a-zA-Z0-9_][a-zA-Z0-9_-]{0,62}(\.[a-zA-Z0-9_][a-zA-Z0-9_-]{0,62})+$', host + r'^(?=^.{3,255}$)[a-zA-Z0-9_][a-zA-Z0-9_-]{0,62}(\.[a-zA-Z0-9_][a-zA-Z0-9_-]{0,62})+$', content ) is not None except: # 异常错误 return False +def isHost(host: str) -> bool: + """ + 判断host是否合法 + + IPv4 / IPv6 / Domain + + 合法: return True + + 不合法: return False + + """ + if isIpAddr(host) or isDomain(host): + return True + return False + + def isPort(port: int) -> bool: """ 判断端口是否合法 @@ -41,6 +67,7 @@ def isPort(port: int) -> bool: 合法: return True 不合法: return False + """ try: if 1 <= port <= 65535: # 1 ~ 65535 diff --git a/demo.py b/demo.py index bd3e57f..1235fa3 100644 --- a/demo.py +++ b/demo.py @@ -1,11 +1,38 @@ import ProxyDecoder as Decoder import ProxyFilter as Filter +import Check as Checker -url = 'trojan://dnomd343@1.1.1.1:443?security=tls&type=tcp&headerType=http&host=ip.343.re' +# url = 'trojan://dnomd343@127.0.0.1:12345?security=tls&sni=local.343.re' +url = 'trojan://dnomd343@local.343.re:12345' ret = Decoder.decode(url) print(ret) - +# status, ret = Filter.filte(ret, isExtra = True) print(status) print(ret) + +# info = { +# 'type': 'vmess', +# 'server': 'baidu.com', +# 'port': 12345, +# 'id': 'a859f794-1fcb-422e-bcad-3264dcea1f12', +# 'aid': 0, +# 'stream': { +# 'type': 'ws', +# 'host': 'host.343.re', +# 'secure': { +# # 'sni': 'sni.343.re' +# } +# } +# } + +# status, ret = Filter.filte(info, isExtra = True) +# print(status) +# print(ret) + +data = Checker.proxyTest({ + 'check': ['http'], + 'info': ret +}) +print(data)