refactor(netxlite): better integration with tracex (#774)

Rather than passing functions to construct complex objects such
as Dialer and QUICDialer, pass interface implementations.

Ensure that a nil implementation does not cause harm.

Make Saver implement the correct interface either directly or
indirectly. We need to implement the correct interface indirectly
for TCP conns (or connected UDP sockets) because we have two
distinct use cases inside netx: observing just the connect event
and observing just the I/O events.

With this change, the construction of composed Dialers and
QUICDialers is greatly simplified and more obvious.

Part of https://github.com/ooni/probe/issues/2121
This commit is contained in:
Simone Basso 2022-06-01 08:31:20 +02:00 committed by GitHub
commit 7e0b47311d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 119 additions and 58 deletions

View file

@ -14,13 +14,9 @@ import (
"github.com/ooni/probe-cli/v3/internal/model"
)
// DialerWrapper is a function that allows you to customize the kind of Dialer returned
// by WrapDialer, NewDialerWithResolver, and NewDialerWithoutResolver.
type DialerWrapper func(dialer model.Dialer) model.Dialer
// NewDialerWithResolver is equivalent to calling WrapDialer with
// the dialer argument being equal to &DialerSystem{}.
func NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...DialerWrapper) model.Dialer {
func NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...model.DialerWrapper) model.Dialer {
return WrapDialer(dl, r, &DialerSystem{}, w...)
}
@ -40,7 +36,8 @@ func NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...DialerWr
// 3. baseDialer is the dialer to wrap (MUST NOT be nil);
//
// 4. wrappers is a list of zero or more functions allowing you to
// modify the behavior of the returned dialer (see below).
// modify the behavior of the returned dialer (see below). Please note
// that this function will just ignore any nil wrapper.
//
// Return value
//
@ -109,12 +106,15 @@ func NewDialerWithResolver(dl model.DebugLogger, r model.Resolver, w ...DialerWr
// of a single connect operation. You may want to use the context to reduce
// the overall time spent trying all addresses and timing out.
func WrapDialer(logger model.DebugLogger, resolver model.Resolver,
baseDialer model.Dialer, wrappers ...DialerWrapper) (outDialer model.Dialer) {
baseDialer model.Dialer, wrappers ...model.DialerWrapper) (outDialer model.Dialer) {
outDialer = &dialerErrWrapper{
Dialer: baseDialer,
}
for _, wrapper := range wrappers {
outDialer = wrapper(outDialer) // extend with user-supplied constructors
if wrapper == nil {
continue // ignore as documented
}
outDialer = wrapper.WrapDialer(outDialer) // extend with user-supplied constructors
}
return &dialerLogger{
Dialer: &dialerResolver{
@ -131,7 +131,7 @@ func WrapDialer(logger model.DebugLogger, resolver model.Resolver,
// NewDialerWithoutResolver is equivalent to calling NewDialerWithResolver
// with the resolver argument being &NullResolver{}.
func NewDialerWithoutResolver(dl model.DebugLogger, w ...DialerWrapper) model.Dialer {
func NewDialerWithoutResolver(dl model.DebugLogger, w ...model.DialerWrapper) model.Dialer {
return NewDialerWithResolver(dl, &NullResolver{}, w...)
}