github.com/imannamdari/v2ray-core/v5@v5.0.5/transport/internet/tls/tls.go (about)

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