uot.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. package outbound
  2. import (
  3. "context"
  4. "os"
  5. "github.com/sagernet/sing/common/uot"
  6. "github.com/xtls/xray-core/common/errors"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/transport/internet"
  9. "github.com/xtls/xray-core/transport/internet/stat"
  10. )
  11. func (h *Handler) getUoTConnection(ctx context.Context, dest net.Destination) (stat.Connection, error) {
  12. if dest.Address == nil {
  13. return nil, errors.New("nil destination address")
  14. }
  15. if !dest.Address.Family().IsDomain() {
  16. return nil, os.ErrInvalid
  17. }
  18. var uotVersion int
  19. if dest.Address.Domain() == uot.MagicAddress {
  20. uotVersion = uot.Version
  21. } else if dest.Address.Domain() == uot.LegacyMagicAddress {
  22. uotVersion = uot.LegacyVersion
  23. } else {
  24. return nil, os.ErrInvalid
  25. }
  26. packetConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{IP: net.AnyIP.IP(), Port: 0}, h.streamSettings.SocketSettings)
  27. if err != nil {
  28. return nil, errors.New("unable to listen socket").Base(err)
  29. }
  30. conn := uot.NewServerConn(packetConn, uotVersion)
  31. return h.getStatCouterConnection(conn), nil
  32. }