github.com/xraypb/Xray-core@v1.8.1/app/proxyman/outbound/uot.go (about)

     1  package outbound
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  
     7  	"github.com/sagernet/sing/common/uot"
     8  	"github.com/xraypb/Xray-core/common/net"
     9  	"github.com/xraypb/Xray-core/transport/internet"
    10  	"github.com/xraypb/Xray-core/transport/internet/stat"
    11  )
    12  
    13  func (h *Handler) getUoTConnection(ctx context.Context, dest net.Destination) (stat.Connection, error) {
    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  }