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

     1  // +build !confonly
     2  
     3  package xtls
     4  
     5  import (
     6  	xtls "github.com/xtls/go"
     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  	*xtls.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 XTLS client handshake on the given connection.
    41  func Client(c net.Conn, config *xtls.Config) net.Conn {
    42  	xtlsConn := xtls.Client(c, config)
    43  	return &Conn{Conn: xtlsConn}
    44  }
    45  
    46  // Server initiates a XTLS server handshake on the given connection.
    47  func Server(c net.Conn, config *xtls.Config) net.Conn {
    48  	xtlsConn := xtls.Server(c, config)
    49  	return &Conn{Conn: xtlsConn}
    50  }