github.com/xraypb/xray-core@v1.6.6/transport/internet/xtls/xtls.go (about)

     1  package xtls
     2  
     3  import (
     4  	xtls "github.com/xtls/go"
     5  
     6  	"github.com/xraypb/xray-core/common/net"
     7  )
     8  
     9  //go:generate go run github.com/xraypb/xray-core/common/errors/errorgen
    10  
    11  type Conn struct {
    12  	*xtls.Conn
    13  }
    14  
    15  func (c *Conn) HandshakeAddress() net.Address {
    16  	if err := c.Handshake(); err != nil {
    17  		return nil
    18  	}
    19  	state := c.ConnectionState()
    20  	if state.ServerName == "" {
    21  		return nil
    22  	}
    23  	return net.ParseAddress(state.ServerName)
    24  }
    25  
    26  // Client initiates a XTLS client handshake on the given connection.
    27  func Client(c net.Conn, config *xtls.Config) net.Conn {
    28  	xtlsConn := xtls.Client(c, config)
    29  	return &Conn{Conn: xtlsConn}
    30  }
    31  
    32  // Server initiates a XTLS server handshake on the given connection.
    33  func Server(c net.Conn, config *xtls.Config) net.Conn {
    34  	xtlsConn := xtls.Server(c, config)
    35  	return &Conn{Conn: xtlsConn}
    36  }