Skip to content

Commit

Permalink
refactor(netxlite): use *Netx for the system resolver
Browse files Browse the repository at this point in the history
This code modifies how we construct netxlite's system resolver such that
public functions use the *Netx equivalents.

The general idea for which I am pushing here is to have additional
clarity about dependencies, to better analyze the requirements of non
measuring code for ooni/probe#2531.
  • Loading branch information
bassosimone committed Sep 11, 2023
1 parent 223e7c3 commit caf2514
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
7 changes: 6 additions & 1 deletion internal/netxlite/dnsovergetaddrinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ type dnsOverGetaddrinfoTransport struct {
provider *MaybeCustomUnderlyingNetwork
}

func (netx *Netx) newDNSOverGetaddrinfoTransport() model.DNSTransport {
return &dnsOverGetaddrinfoTransport{provider: netx.tproxyNilSafeProvider()}
}

// NewDNSOverGetaddrinfoTransport creates a new dns-over-getaddrinfo transport.
func NewDNSOverGetaddrinfoTransport() model.DNSTransport {
return &dnsOverGetaddrinfoTransport{}
netx := &Netx{Underlying: nil}
return netx.newDNSOverGetaddrinfoTransport()
}

var _ model.DNSTransport = &dnsOverGetaddrinfoTransport{}
Expand Down
13 changes: 2 additions & 11 deletions internal/netxlite/netx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@ type Netx struct {
}

// tproxyNilSafeProvider wraps the [model.UnderlyingNetwork] using a [tproxyNilSafeProvider].
func (n *Netx) tproxyNilSafeProvider() *MaybeCustomUnderlyingNetwork {
return &MaybeCustomUnderlyingNetwork{n.Underlying}
}

// NewStdlibResolver is like [netxlite.NewStdlibResolver] but the constructed [model.Resolver]
// uses the [model.UnderlyingNetwork] configured inside the [Netx] structure.
func (n *Netx) NewStdlibResolver(logger model.DebugLogger, wrappers ...model.DNSTransportWrapper) model.Resolver {
unwrapped := &resolverSystem{
t: WrapDNSTransport(&dnsOverGetaddrinfoTransport{provider: n.tproxyNilSafeProvider()}, wrappers...),
}
return WrapResolver(logger, unwrapped)
func (netx *Netx) tproxyNilSafeProvider() *MaybeCustomUnderlyingNetwork {
return &MaybeCustomUnderlyingNetwork{netx.Underlying}
}

// NewDialerWithResolver is like [netxlite.NewDialerWithResolver] but the constructed [model.Dialer]
Expand Down
20 changes: 16 additions & 4 deletions internal/netxlite/resolvercore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ var ErrNoDNSTransport = errors.New("operation requires a DNS transport")
// with an internal "stdlib" resolver type. The list of optional wrappers
// allow to wrap the underlying getaddrinfo transport. Any nil wrapper
// will be silently ignored by the code that performs the wrapping.
func (netx *Netx) NewStdlibResolver(logger model.DebugLogger, wrappers ...model.DNSTransportWrapper) model.Resolver {
return WrapResolver(logger, netx.newUnwrappedStdlibResolver(wrappers...))
}

// NewStdlibResolver is equivalent to creating an empty [*Netx]
// and callings its NewStdlibResolver method.
func NewStdlibResolver(logger model.DebugLogger, wrappers ...model.DNSTransportWrapper) model.Resolver {
return WrapResolver(logger, NewUnwrappedStdlibResolver(wrappers...))
netx := &Netx{Underlying: nil}
return netx.NewStdlibResolver(logger, wrappers...)
}

// NewParallelDNSOverHTTPSResolver creates a new DNS over HTTPS resolver
Expand All @@ -40,13 +47,18 @@ func NewParallelDNSOverHTTPSResolver(logger model.DebugLogger, URL string) model
return WrapResolver(logger, NewUnwrappedParallelResolver(txp))
}

func (netx *Netx) newUnwrappedStdlibResolver(wrappers ...model.DNSTransportWrapper) model.Resolver {
return &resolverSystem{
t: WrapDNSTransport(netx.newDNSOverGetaddrinfoTransport(), wrappers...),
}
}

// NewUnwrappedStdlibResolver returns a new, unwrapped resolver using the standard
// library (i.e., getaddrinfo if possible and &net.Resolver{} otherwise). As the name
// implies, this function returns an unwrapped resolver.
func NewUnwrappedStdlibResolver(wrappers ...model.DNSTransportWrapper) model.Resolver {
return &resolverSystem{
t: WrapDNSTransport(NewDNSOverGetaddrinfoTransport(), wrappers...),
}
netx := &Netx{Underlying: nil}
return netx.newUnwrappedStdlibResolver(wrappers...)
}

// NewSerialUDPResolver creates a new Resolver using DNS-over-UDP
Expand Down

0 comments on commit caf2514

Please sign in to comment.