xtls.go 999 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // +build !confonly
  2. package xtls
  3. import (
  4. xtls "github.com/xtls/go"
  5. "v2ray.com/core/common/buf"
  6. "v2ray.com/core/common/net"
  7. )
  8. //go:generate errorgen
  9. var (
  10. _ buf.Writer = (*Conn)(nil)
  11. )
  12. type Conn struct {
  13. *xtls.Conn
  14. }
  15. func (c *Conn) WriteMultiBuffer(mb buf.MultiBuffer) error {
  16. mb = buf.Compact(mb)
  17. mb, err := buf.WriteMultiBuffer(c, mb)
  18. buf.ReleaseMulti(mb)
  19. return err
  20. }
  21. func (c *Conn) HandshakeAddress() net.Address {
  22. if err := c.Handshake(); err != nil {
  23. return nil
  24. }
  25. state := c.ConnectionState()
  26. if state.ServerName == "" {
  27. return nil
  28. }
  29. return net.ParseAddress(state.ServerName)
  30. }
  31. // Client initiates a XTLS client handshake on the given connection.
  32. func Client(c net.Conn, config *xtls.Config) net.Conn {
  33. xtlsConn := xtls.Client(c, config)
  34. return &Conn{Conn: xtlsConn}
  35. }
  36. // Server initiates a XTLS server handshake on the given connection.
  37. func Server(c net.Conn, config *xtls.Config) net.Conn {
  38. xtlsConn := xtls.Server(c, config)
  39. return &Conn{Conn: xtlsConn}
  40. }