cleanup: move caching resolvers from netx to netxlite (#799)

Now that we have properly refactored the caching resolvers we can
move them into netxlite as optional resolvers created using the
proper abstract factories we just added.

This diff reduces the complexity and the code size of netx.

See https://github.com/ooni/probe/issues/2121.
This commit is contained in:
Simone Basso 2022-06-05 21:58:34 +02:00 committed by GitHub
parent 6b85dfce88
commit 5d54aa9c5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 36 deletions

View File

@ -67,8 +67,8 @@ func NewResolver(config Config) model.Resolver {
model.ValidLoggerOrDefault(config.Logger), model.ValidLoggerOrDefault(config.Logger),
config.BaseResolver, config.BaseResolver,
) )
r = MaybeWrapWithCachingResolver(config.CacheResolutions, r) r = netxlite.MaybeWrapWithCachingResolver(config.CacheResolutions, r)
r = MaybeWrapWithStaticDNSCache(config.DNSCache, r) r = netxlite.MaybeWrapWithStaticDNSCache(config.DNSCache, r)
r = netxlite.MaybeWrapWithBogonResolver(config.BogonIsError, r) r = netxlite.MaybeWrapWithBogonResolver(config.BogonIsError, r)
return config.Saver.WrapResolver(r) // WAI when config.Saver==nil return config.Saver.WrapResolver(r) // WAI when config.Saver==nil
} }

View File

@ -3,7 +3,8 @@ package netxlite
// //
// Bogon // Bogon
// //
// This file helps us to decide if an IPAddr is a bogon. // This file helps us to decide if an IPAddr is a bogon as well as
// a way to create a resolver that fails when resolving bogons.
// //
import ( import (
@ -17,29 +18,28 @@ import (
// MaybeWrapWithBogonResolver wraps the given resolver with a BogonResolver // MaybeWrapWithBogonResolver wraps the given resolver with a BogonResolver
// iff the provided boolean flag is true. Otherwise, this factory just returns // iff the provided boolean flag is true. Otherwise, this factory just returns
// the provided resolver to the caller without any wrapping. // the provided resolver to the caller without any wrapping.
//
// The returned resolver returns a wrapped ErrDNSBogon if there's a bogon error.
//
// BUG: This resolver currently only implements LookupHost. All the other
// lookup methods will always return ErrNoDNSTransport.
func MaybeWrapWithBogonResolver(enabled bool, reso model.Resolver) model.Resolver { func MaybeWrapWithBogonResolver(enabled bool, reso model.Resolver) model.Resolver {
if enabled { if enabled {
reso = &BogonResolver{Resolver: reso} reso = &bogonResolver{Resolver: reso}
} }
return reso return reso
} }
// BogonResolver is a bogon aware resolver. When a bogon is encountered in // bogonResolver is a bogon aware resolver. When a bogon is encountered in
// a reply, this resolver will return ErrDNSBogon. // a reply, this resolver will return ErrDNSBogon.
// type bogonResolver struct {
// This resolver is not part of the default chain created by WrapResolver
// therefore it returns errors that have already been wrapped.
//
// BUG: This resolver currently only implements LookupHost. All the other
// lookup methods will always return ErrNoDNSTransport.
type BogonResolver struct {
Resolver model.Resolver Resolver model.Resolver
} }
var _ model.Resolver = &BogonResolver{} var _ model.Resolver = &bogonResolver{}
// LookupHost implements Resolver.LookupHost // LookupHost implements Resolver.LookupHost
func (r *BogonResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) { func (r *bogonResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
addrs, err := r.Resolver.LookupHost(ctx, hostname) addrs, err := r.Resolver.LookupHost(ctx, hostname)
if err != nil { if err != nil {
return nil, err // not our responsibility to wrap this error return nil, err // not our responsibility to wrap this error
@ -54,29 +54,29 @@ func (r *BogonResolver) LookupHost(ctx context.Context, hostname string) ([]stri
} }
// LookupHTTPS implements Resolver.LookupHTTPS // LookupHTTPS implements Resolver.LookupHTTPS
func (r *BogonResolver) LookupHTTPS(ctx context.Context, hostname string) (*model.HTTPSSvc, error) { func (r *bogonResolver) LookupHTTPS(ctx context.Context, hostname string) (*model.HTTPSSvc, error) {
// TODO(bassosimone): decide whether we want to implement this method or not // TODO(bassosimone): decide whether we want to implement this method or not
return nil, ErrNoDNSTransport return nil, ErrNoDNSTransport
} }
// LookupNS implements Resolver.LookupNS // LookupNS implements Resolver.LookupNS
func (r *BogonResolver) LookupNS(ctx context.Context, hostname string) ([]*net.NS, error) { func (r *bogonResolver) LookupNS(ctx context.Context, hostname string) ([]*net.NS, error) {
// TODO(bassosimone): decide whether we want to implement this method or not // TODO(bassosimone): decide whether we want to implement this method or not
return nil, ErrNoDNSTransport return nil, ErrNoDNSTransport
} }
// Network implements Resolver.Network // Network implements Resolver.Network
func (r *BogonResolver) Network() string { func (r *bogonResolver) Network() string {
return r.Resolver.Network() return r.Resolver.Network()
} }
// Address implements Resolver.Address // Address implements Resolver.Address
func (r *BogonResolver) Address() string { func (r *bogonResolver) Address() string {
return r.Resolver.Address() return r.Resolver.Address()
} }
// CloseIdleConnections implements Resolver.CloseIdleConnections // CloseIdleConnections implements Resolver.CloseIdleConnections
func (r *BogonResolver) CloseIdleConnections() { func (r *bogonResolver) CloseIdleConnections() {
r.Resolver.CloseIdleConnections() r.Resolver.CloseIdleConnections()
} }

View File

@ -13,7 +13,7 @@ func TestMaybeWrapWithBogonResolver(t *testing.T) {
t.Run("with enabled equal to true", func(t *testing.T) { t.Run("with enabled equal to true", func(t *testing.T) {
underlying := &mocks.Resolver{} underlying := &mocks.Resolver{}
reso := MaybeWrapWithBogonResolver(true, underlying) reso := MaybeWrapWithBogonResolver(true, underlying)
bogoreso := reso.(*BogonResolver) bogoreso := reso.(*bogonResolver)
if bogoreso.Resolver != underlying { if bogoreso.Resolver != underlying {
t.Fatal("did not wrap") t.Fatal("did not wrap")
} }
@ -32,7 +32,7 @@ func TestBogonResolver(t *testing.T) {
t.Run("LookupHost", func(t *testing.T) { t.Run("LookupHost", func(t *testing.T) {
t.Run("with failure", func(t *testing.T) { t.Run("with failure", func(t *testing.T) {
expected := errors.New("mocked") expected := errors.New("mocked")
reso := &BogonResolver{ reso := &bogonResolver{
Resolver: &mocks.Resolver{ Resolver: &mocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) { MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return nil, expected return nil, expected
@ -51,7 +51,7 @@ func TestBogonResolver(t *testing.T) {
t.Run("with success and no bogon", func(t *testing.T) { t.Run("with success and no bogon", func(t *testing.T) {
expected := []string{"8.8.8.8", "149.112.112.112"} expected := []string{"8.8.8.8", "149.112.112.112"}
reso := &BogonResolver{ reso := &bogonResolver{
Resolver: &mocks.Resolver{ Resolver: &mocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) { MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return expected, nil return expected, nil
@ -69,7 +69,7 @@ func TestBogonResolver(t *testing.T) {
}) })
t.Run("with success and bogon", func(t *testing.T) { t.Run("with success and bogon", func(t *testing.T) {
reso := &BogonResolver{ reso := &bogonResolver{
Resolver: &mocks.Resolver{ Resolver: &mocks.Resolver{
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) { MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
return []string{"8.8.8.8", "10.34.34.35", "149.112.112.112"}, nil return []string{"8.8.8.8", "10.34.34.35", "149.112.112.112"}, nil
@ -93,7 +93,7 @@ func TestBogonResolver(t *testing.T) {
t.Run("LookupHTTPS", func(t *testing.T) { t.Run("LookupHTTPS", func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
reso := &BogonResolver{} reso := &bogonResolver{}
https, err := reso.LookupHTTPS(ctx, "dns.google") https, err := reso.LookupHTTPS(ctx, "dns.google")
if !errors.Is(err, ErrNoDNSTransport) { if !errors.Is(err, ErrNoDNSTransport) {
t.Fatal("unexpected err", err) t.Fatal("unexpected err", err)
@ -105,7 +105,7 @@ func TestBogonResolver(t *testing.T) {
t.Run("LookupNS", func(t *testing.T) { t.Run("LookupNS", func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
reso := &BogonResolver{} reso := &bogonResolver{}
ns, err := reso.LookupNS(ctx, "dns.google") ns, err := reso.LookupNS(ctx, "dns.google")
if !errors.Is(err, ErrNoDNSTransport) { if !errors.Is(err, ErrNoDNSTransport) {
t.Fatal("unexpected err", err) t.Fatal("unexpected err", err)
@ -117,7 +117,7 @@ func TestBogonResolver(t *testing.T) {
t.Run("Network", func(t *testing.T) { t.Run("Network", func(t *testing.T) {
expected := "antani" expected := "antani"
reso := &BogonResolver{ reso := &bogonResolver{
Resolver: &mocks.Resolver{ Resolver: &mocks.Resolver{
MockNetwork: func() string { MockNetwork: func() string {
return expected return expected
@ -131,7 +131,7 @@ func TestBogonResolver(t *testing.T) {
t.Run("Address", func(t *testing.T) { t.Run("Address", func(t *testing.T) {
expected := "antani" expected := "antani"
reso := &BogonResolver{ reso := &bogonResolver{
Resolver: &mocks.Resolver{ Resolver: &mocks.Resolver{
MockAddress: func() string { MockAddress: func() string {
return expected return expected
@ -145,7 +145,7 @@ func TestBogonResolver(t *testing.T) {
t.Run("CloseIdleConnections", func(t *testing.T) { t.Run("CloseIdleConnections", func(t *testing.T) {
var called bool var called bool
reso := &BogonResolver{ reso := &bogonResolver{
Resolver: &mocks.Resolver{ Resolver: &mocks.Resolver{
MockCloseIdleConnections: func() { MockCloseIdleConnections: func() {
called = true called = true

View File

@ -1,4 +1,4 @@
package netx package netxlite
import ( import (
"context" "context"
@ -6,7 +6,6 @@ import (
"sync" "sync"
"github.com/ooni/probe-cli/v3/internal/model" "github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
) )
// MaybeWrapWithCachingResolver wraps the provided resolver with a resolver // MaybeWrapWithCachingResolver wraps the provided resolver with a resolver
@ -111,10 +110,10 @@ func (r *cacheResolver) CloseIdleConnections() {
// LookupHTTPS implements model.Resolver.LookupHTTPS. // LookupHTTPS implements model.Resolver.LookupHTTPS.
func (r *cacheResolver) LookupHTTPS(ctx context.Context, domain string) (*model.HTTPSSvc, error) { func (r *cacheResolver) LookupHTTPS(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
return nil, netxlite.ErrNoDNSTransport return nil, ErrNoDNSTransport
} }
// LookupNS implements model.Resolver.LookupNS. // LookupNS implements model.Resolver.LookupNS.
func (r *cacheResolver) LookupNS(ctx context.Context, domain string) ([]*net.NS, error) { func (r *cacheResolver) LookupNS(ctx context.Context, domain string) ([]*net.NS, error) {
return nil, netxlite.ErrNoDNSTransport return nil, ErrNoDNSTransport
} }

View File

@ -1,4 +1,4 @@
package netx package netxlite
import ( import (
"context" "context"
@ -7,7 +7,6 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/ooni/probe-cli/v3/internal/model/mocks" "github.com/ooni/probe-cli/v3/internal/model/mocks"
"github.com/ooni/probe-cli/v3/internal/netxlite"
) )
func TestMaybeWrapWithCachingResolver(t *testing.T) { func TestMaybeWrapWithCachingResolver(t *testing.T) {
@ -184,7 +183,7 @@ func TestCacheResolver(t *testing.T) {
t.Run("LookupHTTPS", func(t *testing.T) { t.Run("LookupHTTPS", func(t *testing.T) {
reso := &cacheResolver{} reso := &cacheResolver{}
https, err := reso.LookupHTTPS(context.Background(), "dns.google") https, err := reso.LookupHTTPS(context.Background(), "dns.google")
if !errors.Is(err, netxlite.ErrNoDNSTransport) { if !errors.Is(err, ErrNoDNSTransport) {
t.Fatal("unexpected err", err) t.Fatal("unexpected err", err)
} }
if https != nil { if https != nil {
@ -195,7 +194,7 @@ func TestCacheResolver(t *testing.T) {
t.Run("LookupNS", func(t *testing.T) { t.Run("LookupNS", func(t *testing.T) {
reso := &cacheResolver{} reso := &cacheResolver{}
ns, err := reso.LookupNS(context.Background(), "dns.google") ns, err := reso.LookupNS(context.Background(), "dns.google")
if !errors.Is(err, netxlite.ErrNoDNSTransport) { if !errors.Is(err, ErrNoDNSTransport) {
t.Fatal("unexpected err", err) t.Fatal("unexpected err", err)
} }
if len(ns) != 0 { if len(ns) != 0 {