uot.go 1020 B

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