fix(netxlite): resolver _always_ short circuits IP addrs (#458)
We will use this in a moment when we will add support for the dnstransports that currently are in engine/netx. See https://github.com/ooni/probe/issues/1591
This commit is contained in:
parent
7a9499fee3
commit
b52d784f00
|
@ -33,7 +33,9 @@ type ResolverConfig struct {
|
||||||
func NewResolver(config *ResolverConfig) Resolver {
|
func NewResolver(config *ResolverConfig) Resolver {
|
||||||
return &resolverIDNA{
|
return &resolverIDNA{
|
||||||
Resolver: &resolverLogger{
|
Resolver: &resolverLogger{
|
||||||
|
Resolver: &resolverShortCircuitIPAddr{
|
||||||
Resolver: &resolverSystem{},
|
Resolver: &resolverSystem{},
|
||||||
|
},
|
||||||
Logger: config.Logger,
|
Logger: config.Logger,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -143,3 +145,17 @@ func (r *resolverIDNA) LookupHost(ctx context.Context, hostname string) ([]strin
|
||||||
}
|
}
|
||||||
return r.Resolver.LookupHost(ctx, host)
|
return r.Resolver.LookupHost(ctx, host)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolverShortCircuitIPAddr recognizes when the input hostname is an
|
||||||
|
// IP address and returns it immediately to the caller.
|
||||||
|
type resolverShortCircuitIPAddr struct {
|
||||||
|
Resolver
|
||||||
|
}
|
||||||
|
|
||||||
|
// LookupHost implements Resolver.LookupHost.
|
||||||
|
func (r *resolverShortCircuitIPAddr) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
||||||
|
if net.ParseIP(hostname) != nil {
|
||||||
|
return []string{hostname}, nil
|
||||||
|
}
|
||||||
|
return r.Resolver.LookupHost(ctx, hostname)
|
||||||
|
}
|
||||||
|
|
|
@ -194,7 +194,47 @@ func TestNewResolverTypeChain(t *testing.T) {
|
||||||
if rl.Logger != log.Log {
|
if rl.Logger != log.Log {
|
||||||
t.Fatal("invalid logger")
|
t.Fatal("invalid logger")
|
||||||
}
|
}
|
||||||
if _, ok := rl.Resolver.(*resolverSystem); !ok {
|
scia, ok := rl.Resolver.(*resolverShortCircuitIPAddr)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("invalid resolver")
|
||||||
|
}
|
||||||
|
if _, ok := scia.Resolver.(*resolverSystem); !ok {
|
||||||
t.Fatal("invalid resolver")
|
t.Fatal("invalid resolver")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResolverShortCircuitIPAddrWithIPAddr(t *testing.T) {
|
||||||
|
r := &resolverShortCircuitIPAddr{
|
||||||
|
Resolver: &mocks.Resolver{
|
||||||
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||||
|
return nil, errors.New("mocked error")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
addrs, err := r.LookupHost(ctx, "8.8.8.8")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(addrs) != 1 || addrs[0] != "8.8.8.8" {
|
||||||
|
t.Fatal("invalid result")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolverShortCircuitIPAddrWithDomain(t *testing.T) {
|
||||||
|
r := &resolverShortCircuitIPAddr{
|
||||||
|
Resolver: &mocks.Resolver{
|
||||||
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||||
|
return nil, errors.New("mocked error")
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
addrs, err := r.LookupHost(ctx, "dns.google")
|
||||||
|
if err == nil || err.Error() != "mocked error" {
|
||||||
|
t.Fatal("not the error we expected", err)
|
||||||
|
}
|
||||||
|
if addrs != nil {
|
||||||
|
t.Fatal("invalid result")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user