diff --git a/internal/engine/netx/netx.go b/internal/engine/netx/netx.go index 07a684b..09454f9 100644 --- a/internal/engine/netx/netx.go +++ b/internal/engine/netx/netx.go @@ -118,7 +118,9 @@ func NewResolver(config Config) Resolver { config.BaseResolver = &netxlite.ResolverSystem{} } var r Resolver = config.BaseResolver - r = resolver.AddressResolver{Resolver: r} + r = &resolver.AddressResolver{ + Resolver: netxlite.NewResolverLegacyAdapter(r), + } if config.CacheResolutions { r = &resolver.CacheResolver{Resolver: r} } diff --git a/internal/engine/netx/netx_test.go b/internal/engine/netx/netx_test.go index 0e94675..228a63d 100644 --- a/internal/engine/netx/netx_test.go +++ b/internal/engine/netx/netx_test.go @@ -35,11 +35,15 @@ func TestNewResolverVanilla(t *testing.T) { if !ok { t.Fatal("not the resolver we expected") } - ar, ok := ewr.Resolver.(resolver.AddressResolver) + ar, ok := ewr.Resolver.(*resolver.AddressResolver) if !ok { t.Fatal("not the resolver we expected") } - _, ok = ar.Resolver.(*netxlite.ResolverSystem) + arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter) + if !ok { + t.Fatal("not the resolver we expected") + } + _, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem) if !ok { t.Fatal("not the resolver we expected") } @@ -63,11 +67,15 @@ func TestNewResolverSpecificResolver(t *testing.T) { if !ok { t.Fatal("not the resolver we expected") } - ar, ok := ewr.Resolver.(resolver.AddressResolver) + ar, ok := ewr.Resolver.(*resolver.AddressResolver) if !ok { t.Fatal("not the resolver we expected") } - _, ok = ar.Resolver.(resolver.BogonResolver) + arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter) + if !ok { + t.Fatal("not the resolver we expected") + } + _, ok = arw.ResolverLegacy.(resolver.BogonResolver) if !ok { t.Fatal("not the resolver we expected") } @@ -93,11 +101,15 @@ func TestNewResolverWithBogonFilter(t *testing.T) { if !ok { t.Fatal("not the resolver we expected") } - ar, ok := br.Resolver.(resolver.AddressResolver) + ar, ok := br.Resolver.(*resolver.AddressResolver) if !ok { t.Fatal("not the resolver we expected") } - _, ok = ar.Resolver.(*netxlite.ResolverSystem) + arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter) + if !ok { + t.Fatal("not the resolver we expected") + } + _, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem) if !ok { t.Fatal("not the resolver we expected") } @@ -138,11 +150,15 @@ func TestNewResolverWithLogging(t *testing.T) { if !ok { t.Fatalf("not the resolver we expected %T", rla.ResolverLegacy) } - ar, ok := ewr.Resolver.(resolver.AddressResolver) + ar, ok := ewr.Resolver.(*resolver.AddressResolver) if !ok { t.Fatal("not the resolver we expected") } - _, ok = ar.Resolver.(*netxlite.ResolverSystem) + arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter) + if !ok { + t.Fatal("not the resolver we expected") + } + _, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem) if !ok { t.Fatal("not the resolver we expected") } @@ -172,11 +188,15 @@ func TestNewResolverWithSaver(t *testing.T) { if !ok { t.Fatal("not the resolver we expected") } - ar, ok := ewr.Resolver.(resolver.AddressResolver) + ar, ok := ewr.Resolver.(*resolver.AddressResolver) if !ok { t.Fatal("not the resolver we expected") } - _, ok = ar.Resolver.(*netxlite.ResolverSystem) + arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter) + if !ok { + t.Fatal("not the resolver we expected") + } + _, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem) if !ok { t.Fatal("not the resolver we expected") } @@ -205,11 +225,15 @@ func TestNewResolverWithReadWriteCache(t *testing.T) { if cr.ReadOnly != false { t.Fatal("expected readwrite cache here") } - ar, ok := cr.Resolver.(resolver.AddressResolver) + ar, ok := cr.Resolver.(*resolver.AddressResolver) if !ok { t.Fatal("not the resolver we expected") } - _, ok = ar.Resolver.(*netxlite.ResolverSystem) + arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter) + if !ok { + t.Fatal("not the resolver we expected") + } + _, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem) if !ok { t.Fatal("not the resolver we expected") } @@ -243,11 +267,15 @@ func TestNewResolverWithPrefilledReadonlyCache(t *testing.T) { if cr.Get("dns.google.com")[0] != "8.8.8.8" { t.Fatal("cache not correctly prefilled") } - ar, ok := cr.Resolver.(resolver.AddressResolver) + ar, ok := cr.Resolver.(*resolver.AddressResolver) if !ok { t.Fatal("not the resolver we expected") } - _, ok = ar.Resolver.(*netxlite.ResolverSystem) + arw, ok := ar.Resolver.(*netxlite.ResolverLegacyAdapter) + if !ok { + t.Fatal("not the resolver we expected") + } + _, ok = arw.ResolverLegacy.(*netxlite.ResolverSystem) if !ok { t.Fatal("not the resolver we expected") } diff --git a/internal/engine/netx/resolver/address.go b/internal/engine/netx/resolver/address.go index c6cb4a5..cae77eb 100644 --- a/internal/engine/netx/resolver/address.go +++ b/internal/engine/netx/resolver/address.go @@ -1,22 +1,9 @@ package resolver import ( - "context" - "net" + "github.com/ooni/probe-cli/v3/internal/netxlite" ) // AddressResolver is a resolver that knows how to correctly // resolve IP addresses to themselves. -type AddressResolver struct { - Resolver -} - -// LookupHost implements Resolver.LookupHost -func (r AddressResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) { - if net.ParseIP(hostname) != nil { - return []string{hostname}, nil - } - return r.Resolver.LookupHost(ctx, hostname) -} - -var _ Resolver = AddressResolver{} +type AddressResolver = netxlite.AddressResolver diff --git a/internal/engine/netx/resolver/address_test.go b/internal/engine/netx/resolver/address_test.go deleted file mode 100644 index 9d3e14d..0000000 --- a/internal/engine/netx/resolver/address_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package resolver_test - -import ( - "context" - "errors" - "testing" - - "github.com/ooni/probe-cli/v3/internal/engine/netx/resolver" -) - -func TestAddressSuccess(t *testing.T) { - r := resolver.AddressResolver{} - addrs, err := r.LookupHost(context.Background(), "8.8.8.8") - if err != nil { - t.Fatal(err) - } - if len(addrs) != 1 || addrs[0] != "8.8.8.8" { - t.Fatal("not the result we expected") - } -} - -func TestAddressFailure(t *testing.T) { - expected := errors.New("mocked error") - r := resolver.AddressResolver{ - Resolver: resolver.FakeResolver{ - Err: expected, - }, - } - addrs, err := r.LookupHost(context.Background(), "dns.google.com") - if !errors.Is(err, expected) { - t.Fatal("not the error we expected") - } - if addrs != nil { - t.Fatal("expected nil addrs") - } -} diff --git a/internal/netxlite/legacy.go b/internal/netxlite/legacy.go index d4e9035..dbf2116 100644 --- a/internal/netxlite/legacy.go +++ b/internal/netxlite/legacy.go @@ -33,6 +33,7 @@ type ( DialerSystem = dialerSystem TLSDialerLegacy = tlsDialer UserAgentTransport = httpUserAgentTransport + AddressResolver = resolverShortCircuitIPAddr ) // ResolverLegacy performs domain name resolutions.