2022-06-01 06:12:43 +03:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing/common/uot"
|
2024-06-29 21:32:57 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2022-06-01 06:12:43 +03:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
|
|
|
"github.com/xtls/xray-core/transport/internet/stat"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *Handler) getUoTConnection(ctx context.Context, dest net.Destination) (stat.Connection, error) {
|
2023-07-18 23:11:09 +03:00
|
|
|
if dest.Address == nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("nil destination address")
|
2023-07-18 23:11:09 +03:00
|
|
|
}
|
2023-03-15 09:42:32 +03:00
|
|
|
if !dest.Address.Family().IsDomain() {
|
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
var uotVersion int
|
|
|
|
if dest.Address.Domain() == uot.MagicAddress {
|
|
|
|
uotVersion = uot.Version
|
|
|
|
} else if dest.Address.Domain() == uot.LegacyMagicAddress {
|
|
|
|
uotVersion = uot.LegacyVersion
|
|
|
|
} else {
|
2022-06-01 06:12:43 +03:00
|
|
|
return nil, os.ErrInvalid
|
|
|
|
}
|
|
|
|
packetConn, err := internet.ListenSystemPacket(ctx, &net.UDPAddr{IP: net.AnyIP.IP(), Port: 0}, h.streamSettings.SocketSettings)
|
|
|
|
if err != nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
return nil, errors.New("unable to listen socket").Base(err)
|
2022-06-01 06:12:43 +03:00
|
|
|
}
|
2023-03-15 09:42:32 +03:00
|
|
|
conn := uot.NewServerConn(packetConn, uotVersion)
|
2022-06-01 06:12:43 +03:00
|
|
|
return h.getStatCouterConnection(conn), nil
|
|
|
|
}
|