refactor: continue to simplify engine/netx (#769)
The objective of this diff is to simplify the code inside engine/netx while moving more bits of code inside netxlite. See https://github.com/ooni/probe/issues/2121
This commit is contained in:
@@ -1,32 +0,0 @@
|
||||
package resolver
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
// BogonResolver is a bogon aware resolver. When a bogon is encountered in
|
||||
// a reply, this resolver will return an error.
|
||||
//
|
||||
// Deprecation warning
|
||||
//
|
||||
// This resolver is deprecated. The right thing to do would be to check
|
||||
// for bogons right after a domain name resolution in the nettest.
|
||||
type BogonResolver struct {
|
||||
model.Resolver
|
||||
}
|
||||
|
||||
// LookupHost implements Resolver.LookupHost
|
||||
func (r BogonResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
||||
addrs, err := r.Resolver.LookupHost(ctx, hostname)
|
||||
for _, addr := range addrs {
|
||||
if netxlite.IsBogon(addr) {
|
||||
return nil, netxlite.ErrDNSBogon
|
||||
}
|
||||
}
|
||||
return addrs, err
|
||||
}
|
||||
|
||||
var _ model.Resolver = BogonResolver{}
|
||||
@@ -1,37 +0,0 @@
|
||||
package resolver_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
func TestBogonAwareResolverWithBogon(t *testing.T) {
|
||||
r := resolver.BogonResolver{
|
||||
Resolver: resolver.NewFakeResolverWithResult([]string{"127.0.0.1"}),
|
||||
}
|
||||
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
|
||||
if !errors.Is(err, netxlite.ErrDNSBogon) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if len(addrs) > 0 {
|
||||
t.Fatal("expected to see nil here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBogonAwareResolverWithoutBogon(t *testing.T) {
|
||||
orig := []string{"8.8.8.8"}
|
||||
r := resolver.BogonResolver{
|
||||
Resolver: resolver.NewFakeResolverWithResult(orig),
|
||||
}
|
||||
addrs, err := r.LookupHost(context.Background(), "dns.google.com")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(addrs) != len(orig) || addrs[0] != orig[0] {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user