xtls.go 809 B

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