From 442690ffff89b97105b3b7dc8951a39a640ba08d Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Tue, 12 Sep 2023 09:03:34 +0200 Subject: [PATCH] refactor(netxlite): use *Netx for creating UDP sockets (#1250) This diff is similar to https://github.com/ooni/probe-cli/commit/07a048ce8e944ba68b3fd55faf6c1de9a9620485 but here we use *Netx to create UDP sockets. While there, recognize that UDP code needs its own files and should not live inside quic{,_test}.go. Part of https://github.com/ooni/probe/issues/2531. --- internal/netxlite/netx.go | 6 ------ internal/netxlite/quic.go | 19 ------------------- internal/netxlite/quic_test.go | 6 ------ internal/netxlite/udp.go | 33 +++++++++++++++++++++++++++++++++ internal/netxlite/udp_test.go | 9 +++++++++ 5 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 internal/netxlite/udp.go create mode 100644 internal/netxlite/udp_test.go diff --git a/internal/netxlite/netx.go b/internal/netxlite/netx.go index 240d7f12b3..eca345633f 100644 --- a/internal/netxlite/netx.go +++ b/internal/netxlite/netx.go @@ -22,12 +22,6 @@ func (netx *Netx) maybeCustomUnderlyingNetwork() *MaybeCustomUnderlyingNetwork { return &MaybeCustomUnderlyingNetwork{netx.Underlying} } -// NewUDPListener is like [netxlite.NewUDPListener] but the constructed [model.UDPListener] -// uses the [model.UnderlyingNetwork] configured inside the [Netx] structure. -func (n *Netx) NewUDPListener() model.UDPListener { - return &udpListenerErrWrapper{&udpListenerStdlib{provider: n.maybeCustomUnderlyingNetwork()}} -} - // NewQUICDialerWithResolver is like [netxlite.NewQUICDialerWithResolver] but the constructed // [model.QUICDialer] uses the [model.UnderlyingNetwork] configured inside the [Netx] structure. func (n *Netx) NewQUICDialerWithResolver(listener model.UDPListener, logger model.DebugLogger, diff --git a/internal/netxlite/quic.go b/internal/netxlite/quic.go index 23512764af..a1fb7d6656 100644 --- a/internal/netxlite/quic.go +++ b/internal/netxlite/quic.go @@ -16,25 +16,6 @@ import ( "github.com/quic-go/quic-go" ) -// NewUDPListener creates a new UDPListener using the standard -// library to create listening UDP sockets. -func NewUDPListener() model.UDPListener { - return &udpListenerErrWrapper{&udpListenerStdlib{}} -} - -// udpListenerStdlib is a UDPListener using the standard library. -type udpListenerStdlib struct { - // provider is the OPTIONAL nil-safe [model.UnderlyingNetwork] provider. - provider *MaybeCustomUnderlyingNetwork -} - -var _ model.UDPListener = &udpListenerStdlib{} - -// Listen implements UDPListener.Listen. -func (qls *udpListenerStdlib) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) { - return qls.provider.Get().ListenUDP("udp", addr) -} - // NewQUICDialerWithResolver is the WrapDialer equivalent for QUIC where // we return a composed QUICDialer modified by optional wrappers. // diff --git a/internal/netxlite/quic_test.go b/internal/netxlite/quic_test.go index 33ba2d9e5d..5cc60afa34 100644 --- a/internal/netxlite/quic_test.go +++ b/internal/netxlite/quic_test.go @@ -19,12 +19,6 @@ import ( "github.com/quic-go/quic-go" ) -func TestNewUDPListener(t *testing.T) { - ql := NewUDPListener() - qew := ql.(*udpListenerErrWrapper) - _ = qew.UDPListener.(*udpListenerStdlib) -} - type extensionQUICDialerFirst struct { model.QUICDialer } diff --git a/internal/netxlite/udp.go b/internal/netxlite/udp.go new file mode 100644 index 0000000000..957b092b97 --- /dev/null +++ b/internal/netxlite/udp.go @@ -0,0 +1,33 @@ +package netxlite + +import ( + "net" + + "github.com/ooni/probe-cli/v3/internal/model" +) + +// NewUDPListener creates a new UDPListener using the underlying +// [*Netx] structure to create listening UDP sockets. +func (netx *Netx) NewUDPListener() model.UDPListener { + return &udpListenerErrWrapper{&udpListenerStdlib{provider: netx.maybeCustomUnderlyingNetwork()}} +} + +// NewUDPListener is equivalent to creating an empty [*Netx] +// and calling its NewUDPListener method. +func NewUDPListener() model.UDPListener { + netx := &Netx{Underlying: nil} + return netx.NewUDPListener() +} + +// udpListenerStdlib is a UDPListener using the standard library. +type udpListenerStdlib struct { + // provider is the OPTIONAL nil-safe [model.UnderlyingNetwork] provider. + provider *MaybeCustomUnderlyingNetwork +} + +var _ model.UDPListener = &udpListenerStdlib{} + +// Listen implements UDPListener.Listen. +func (qls *udpListenerStdlib) Listen(addr *net.UDPAddr) (model.UDPLikeConn, error) { + return qls.provider.Get().ListenUDP("udp", addr) +} diff --git a/internal/netxlite/udp_test.go b/internal/netxlite/udp_test.go new file mode 100644 index 0000000000..77c9864e68 --- /dev/null +++ b/internal/netxlite/udp_test.go @@ -0,0 +1,9 @@ +package netxlite + +import "testing" + +func TestNewUDPListener(t *testing.T) { + ql := NewUDPListener() + qew := ql.(*udpListenerErrWrapper) + _ = qew.UDPListener.(*udpListenerStdlib) +}