2023-02-25 05:12:19 +00:00
|
|
|
|
package vless
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
2023-02-26 17:06:41 +00:00
|
|
|
|
"github.com/Dreamacro/clash/log"
|
2023-02-25 05:12:19 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
tls13SupportedVersions = []byte{0x00, 0x2b, 0x00, 0x02, 0x03, 0x04}
|
|
|
|
|
tlsClientHandshakeStart = []byte{0x16, 0x03}
|
|
|
|
|
tlsServerHandshakeStart = []byte{0x16, 0x03, 0x03}
|
|
|
|
|
tlsApplicationDataStart = []byte{0x17, 0x03, 0x03}
|
|
|
|
|
|
|
|
|
|
tls13CipherSuiteMap = map[uint16]string{
|
|
|
|
|
0x1301: "TLS_AES_128_GCM_SHA256",
|
|
|
|
|
0x1302: "TLS_AES_256_GCM_SHA384",
|
|
|
|
|
0x1303: "TLS_CHACHA20_POLY1305_SHA256",
|
|
|
|
|
0x1304: "TLS_AES_128_CCM_SHA256",
|
|
|
|
|
0x1305: "TLS_AES_128_CCM_8_SHA256",
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
tlsHandshakeTypeClientHello byte = 0x01
|
|
|
|
|
tlsHandshakeTypeServerHello byte = 0x02
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-26 17:06:41 +00:00
|
|
|
|
func (vc *Conn) FilterTLS(buffer []byte) (index int) {
|
2023-02-25 05:12:19 +00:00
|
|
|
|
if vc.packetsToFilter <= 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2023-02-26 17:06:41 +00:00
|
|
|
|
lenP := len(buffer)
|
|
|
|
|
vc.packetsToFilter--
|
2023-02-27 04:02:44 +00:00
|
|
|
|
if index = bytes.Index(buffer, tlsServerHandshakeStart); index != -1 {
|
2023-04-01 12:08:49 +00:00
|
|
|
|
if lenP > index+5 {
|
2023-02-26 17:06:41 +00:00
|
|
|
|
if buffer[0] == 22 && buffer[1] == 3 && buffer[2] == 3 {
|
|
|
|
|
vc.isTLS = true
|
|
|
|
|
if buffer[5] == tlsHandshakeTypeServerHello {
|
2023-02-27 04:02:44 +00:00
|
|
|
|
//log.Debugln("isTLS12orAbove")
|
2023-02-26 17:06:41 +00:00
|
|
|
|
vc.remainingServerHello = binary.BigEndian.Uint16(buffer[index+3:]) + 5
|
|
|
|
|
vc.isTLS12orAbove = true
|
|
|
|
|
if lenP-index >= 79 && vc.remainingServerHello >= 79 {
|
|
|
|
|
sessionIDLen := int(buffer[index+43])
|
|
|
|
|
vc.cipher = binary.BigEndian.Uint16(buffer[index+43+sessionIDLen+1:])
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-25 05:12:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-27 04:02:44 +00:00
|
|
|
|
} else if index = bytes.Index(buffer, tlsClientHandshakeStart); index != -1 {
|
2023-04-01 12:08:49 +00:00
|
|
|
|
if lenP > index+5 && buffer[index+5] == tlsHandshakeTypeClientHello {
|
2023-02-25 05:12:19 +00:00
|
|
|
|
vc.isTLS = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if vc.remainingServerHello > 0 {
|
2023-02-26 03:11:55 +00:00
|
|
|
|
end := int(vc.remainingServerHello)
|
2023-02-26 05:04:12 +00:00
|
|
|
|
i := index
|
|
|
|
|
if i < 0 {
|
|
|
|
|
i = 0
|
|
|
|
|
}
|
|
|
|
|
if i+end > lenP {
|
2023-02-26 03:11:55 +00:00
|
|
|
|
end = lenP
|
2023-02-26 05:04:12 +00:00
|
|
|
|
vc.remainingServerHello -= uint16(end - i)
|
2023-02-26 03:11:55 +00:00
|
|
|
|
} else {
|
|
|
|
|
vc.remainingServerHello -= uint16(end)
|
2023-02-26 05:04:12 +00:00
|
|
|
|
end += i
|
2023-02-25 05:12:19 +00:00
|
|
|
|
}
|
2023-02-26 17:06:41 +00:00
|
|
|
|
if bytes.Contains(buffer[i:end], tls13SupportedVersions) {
|
2023-02-25 05:12:19 +00:00
|
|
|
|
// TLS 1.3 Client Hello
|
|
|
|
|
cs, ok := tls13CipherSuiteMap[vc.cipher]
|
|
|
|
|
if ok && cs != "TLS_AES_128_CCM_8_SHA256" {
|
|
|
|
|
vc.enableXTLS = true
|
|
|
|
|
}
|
2023-02-27 04:02:44 +00:00
|
|
|
|
log.Debugln("XTLS Vision found TLS 1.3, packetLength=%d, CipherSuite=%s", lenP, cs)
|
2023-02-25 05:12:19 +00:00
|
|
|
|
vc.packetsToFilter = 0
|
|
|
|
|
return
|
2023-02-26 03:11:55 +00:00
|
|
|
|
} else if vc.remainingServerHello <= 0 {
|
2023-02-27 04:02:44 +00:00
|
|
|
|
log.Debugln("XTLS Vision found TLS 1.2, packetLength=%d", lenP)
|
2023-02-25 05:12:19 +00:00
|
|
|
|
vc.packetsToFilter = 0
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-02-27 04:02:44 +00:00
|
|
|
|
log.Debugln("XTLS Vision found inconclusive server hello, packetLength=%d, remainingServerHelloBytes=%d", lenP, vc.remainingServerHello)
|
2023-02-25 05:12:19 +00:00
|
|
|
|
}
|
|
|
|
|
if vc.packetsToFilter <= 0 {
|
|
|
|
|
log.Debugln("XTLS Vision stop filtering")
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|