github.com/yaling888/clash@v1.53.0/transport/tls/tls.go (about)

     1  package tls
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"net"
     7  
     8  	C "github.com/yaling888/clash/constant"
     9  )
    10  
    11  type Config struct {
    12  	Host           string
    13  	SkipCertVerify bool
    14  	NextProtos     []string
    15  }
    16  
    17  func StreamTLSConn(conn net.Conn, cfg *Config) (net.Conn, error) {
    18  	tlsConfig := &tls.Config{
    19  		ServerName:         cfg.Host,
    20  		InsecureSkipVerify: cfg.SkipCertVerify,
    21  		NextProtos:         cfg.NextProtos,
    22  	}
    23  
    24  	tlsConn := tls.Client(conn, tlsConfig)
    25  
    26  	// fix tls handshake not timeout
    27  	ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
    28  	defer cancel()
    29  	err := tlsConn.HandshakeContext(ctx)
    30  	return tlsConn, err
    31  }