Browse Source

update: add SNI automatically

master
Dnomd343 3 years ago
parent
commit
211a46cd68
  1. 9
      ProxyFilter/Trojan.py
  2. 18
      ProxyFilter/V2ray.py
  3. 9
      ProxyFilter/VLESS.py
  4. 9
      ProxyFilter/VMess.py
  5. 2
      ProxyFilter/Xray.py
  6. 47
      ProxyFilter/baseFunc.py
  7. 31
      demo.py

9
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'

18
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

9
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'

9
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'

2
ProxyFilter/Xray.py

@ -106,3 +106,5 @@ xrayStreamRules['xtlsObject'] = {
'format': baseFunc.toBool
}
}
addSni = V2ray.addSni

47
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

31
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)

Loading…
Cancel
Save