2020-04-03 16:04:24 +08:00
|
|
|
package vmess
|
|
|
|
|
|
|
|
import (
|
2022-01-15 19:33:21 +08:00
|
|
|
"context"
|
2020-04-03 16:04:24 +08:00
|
|
|
"crypto/tls"
|
2022-07-11 12:37:27 +08:00
|
|
|
tlsC "github.com/Dreamacro/clash/component/tls"
|
2020-04-03 16:04:24 +08:00
|
|
|
"net"
|
2022-01-15 19:33:21 +08:00
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2020-04-03 16:04:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type TLSConfig struct {
|
|
|
|
Host string
|
|
|
|
SkipCertVerify bool
|
2020-09-26 20:33:57 +08:00
|
|
|
NextProtos []string
|
2020-04-03 16:04:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func StreamTLSConn(conn net.Conn, cfg *TLSConfig) (net.Conn, error) {
|
2022-07-10 20:44:24 +08:00
|
|
|
tlsConfig := tlsC.MixinTLSConfig(&tls.Config{
|
2020-04-03 16:04:24 +08:00
|
|
|
ServerName: cfg.Host,
|
|
|
|
InsecureSkipVerify: cfg.SkipCertVerify,
|
2020-09-26 20:33:57 +08:00
|
|
|
NextProtos: cfg.NextProtos,
|
2022-07-10 20:44:24 +08:00
|
|
|
})
|
2020-04-03 16:04:24 +08:00
|
|
|
|
|
|
|
tlsConn := tls.Client(conn, tlsConfig)
|
2022-01-15 19:33:21 +08:00
|
|
|
|
|
|
|
// fix tls handshake not timeout
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err := tlsConn.HandshakeContext(ctx)
|
2020-04-03 16:04:24 +08:00
|
|
|
return tlsConn, err
|
|
|
|
}
|