xtls.go 810 B

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