github.com/v2fly/v2ray-core/v4@v4.45.2/transport/internet/tls/tls.go (about)

     1  //go:build !confonly
     2  // +build !confonly
     3  
     4  package tls
     5  
     6  import (
     7  	"crypto/tls"
     8  
     9  	"github.com/v2fly/v2ray-core/v4/common/buf"
    10  	"github.com/v2fly/v2ray-core/v4/common/net"
    11  )
    12  
    13  //go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
    14  
    15  var _ buf.Writer = (*Conn)(nil)
    16  
    17  type Conn struct {
    18  	*tls.Conn
    19  }
    20  
    21  func (c *Conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
    22  	mb = buf.Compact(mb)
    23  	mb, err := buf.WriteMultiBuffer(c, mb)
    24  	buf.ReleaseMulti(mb)
    25  	return err
    26  }
    27  
    28  func (c *Conn) HandshakeAddress() net.Address {
    29  	if err := c.Handshake(); err != nil {
    30  		return nil
    31  	}
    32  	state := c.ConnectionState()
    33  	if state.ServerName == "" {
    34  		return nil
    35  	}
    36  	return net.ParseAddress(state.ServerName)
    37  }
    38  
    39  // Client initiates a TLS client handshake on the given connection.
    40  func Client(c net.Conn, config *tls.Config) net.Conn {
    41  	tlsConn := tls.Client(c, config)
    42  	return &Conn{Conn: tlsConn}
    43  }
    44  
    45  /*
    46  func copyConfig(c *tls.Config) *utls.Config {
    47  	return &utls.Config{
    48  		NextProtos:         c.NextProtos,
    49  		ServerName:         c.ServerName,
    50  		InsecureSkipVerify: c.InsecureSkipVerify,
    51  		MinVersion:         utls.VersionTLS12,
    52  		MaxVersion:         utls.VersionTLS12,
    53  	}
    54  }
    55  
    56  func UClient(c net.Conn, config *tls.Config) net.Conn {
    57  	uConfig := copyConfig(config)
    58  	return utls.Client(c, uConfig)
    59  }
    60  */
    61  
    62  // Server initiates a TLS server handshake on the given connection.
    63  func Server(c net.Conn, config *tls.Config) net.Conn {
    64  	tlsConn := tls.Server(c, config)
    65  	return &Conn{Conn: tlsConn}
    66  }