1
0

uot.go 941 B

12345678910111213141516171819202122232425262728293031
  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.Family().IsDomain() {
  12. return nil, os.ErrInvalid
  13. }
  14. var uotVersion int
  15. if dest.Address.Domain() == uot.MagicAddress {
  16. uotVersion = uot.Version
  17. } else if dest.Address.Domain() == uot.LegacyMagicAddress {
  18. uotVersion = uot.LegacyVersion
  19. } else {
  20. return nil, os.ErrInvalid
  21. }
  22. packetConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{IP: net.AnyIP.IP(), Port: 0}, h.streamSettings.SocketSettings)
  23. if err != nil {
  24. return nil, newError("unable to listen socket").Base(err)
  25. }
  26. conn := uot.NewServerConn(packetConn, uotVersion)
  27. return h.getStatCouterConnection(conn), nil
  28. }