github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/transport/internet/tls/tls.go (about)

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