2020-04-03 08:04:24 +00:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
2022-01-15 11:33:21 +00:00
|
|
|
"context"
|
2020-04-03 08:04:24 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
2022-01-15 11:33:21 +00:00
|
|
|
|
2023-01-13 01:55:01 +00:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
2022-01-15 11:33:21 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2020-04-03 08:04:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TLSConfig struct {
|
2023-02-01 14:16:06 +00:00
|
|
|
Host string
|
|
|
|
SkipCertVerify bool
|
|
|
|
FingerPrint string
|
|
|
|
ClientFingerprint string
|
|
|
|
NextProtos []string
|
2020-04-03 08:04:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func StreamTLSConn(conn net.Conn, cfg *TLSConfig) (net.Conn, error) {
|
2022-07-11 05:42:28 +00:00
|
|
|
tlsConfig := &tls.Config{
|
2020-04-03 08:04:24 +00:00
|
|
|
ServerName: cfg.Host,
|
|
|
|
InsecureSkipVerify: cfg.SkipCertVerify,
|
2020-09-26 12:33:57 +00:00
|
|
|
NextProtos: cfg.NextProtos,
|
2022-07-11 05:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(cfg.FingerPrint) == 0 {
|
2023-01-14 13:08:06 +00:00
|
|
|
tlsConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
|
2022-07-11 05:42:28 +00:00
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
if tlsConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, cfg.FingerPrint); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 08:04:24 +00:00
|
|
|
|
2023-02-01 14:16:06 +00:00
|
|
|
if len(cfg.ClientFingerprint) != 0 {
|
2023-02-05 09:31:58 +00:00
|
|
|
utlsConn, valid := GetUtlsConnWithClientFingerprint(conn, cfg.ClientFingerprint, tlsConfig)
|
|
|
|
if valid {
|
2023-02-01 14:16:06 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-02-07 08:08:59 +00:00
|
|
|
err := utlsConn.(*tlsC.UConn).HandshakeContext(ctx)
|
2023-02-01 14:16:06 +00:00
|
|
|
return utlsConn, err
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 08:04:24 +00:00
|
|
|
tlsConn := tls.Client(conn, tlsConfig)
|
2022-01-15 11:33:21 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
|
|
|
defer cancel()
|
2023-02-01 14:16:06 +00:00
|
|
|
|
2022-01-15 11:33:21 +00:00
|
|
|
err := tlsConn.HandshakeContext(ctx)
|
2020-04-03 08:04:24 +00:00
|
|
|
return tlsConn, err
|
|
|
|
}
|
2023-02-05 09:31:58 +00:00
|
|
|
|
|
|
|
func GetUtlsConnWithClientFingerprint(conn net.Conn, ClientFingerprint string, tlsConfig *tls.Config) (net.Conn, bool) {
|
|
|
|
|
2023-02-07 08:08:59 +00:00
|
|
|
if fingerprint, exists := tlsC.GetFingerprint(ClientFingerprint); exists {
|
2023-02-07 09:51:37 +00:00
|
|
|
utlsConn := tlsC.UClient(conn, tlsConfig, fingerprint)
|
2023-02-05 09:31:58 +00:00
|
|
|
|
|
|
|
return utlsConn, true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|