refactor: fully move IDNAResolver to netxlite (#433)
We started doing this in https://github.com/ooni/probe-cli/pull/432. This work is part of https://github.com/ooni/probe/issues/1733.
This commit is contained in:
parent
c31591f298
commit
bef5b87a8a
|
@ -91,6 +91,6 @@ func newResolver() netxlite.Resolver {
|
||||||
childResolver, err := netx.NewDNSClient(netx.Config{Logger: log.Log}, "doh://google")
|
childResolver, err := netx.NewDNSClient(netx.Config{Logger: log.Log}, "doh://google")
|
||||||
runtimex.PanicOnError(err, "NewDNSClient failed")
|
runtimex.PanicOnError(err, "NewDNSClient failed")
|
||||||
var r netxlite.Resolver = childResolver
|
var r netxlite.Resolver = childResolver
|
||||||
r = &netxlite.IDNAResolver{Resolver: r}
|
r = &netxlite.ResolverIDNA{Resolver: r}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ func DNSDo(ctx context.Context, config DNSConfig) ([]string, error) {
|
||||||
childResolver, err := netx.NewDNSClient(netx.Config{Logger: log.Log}, "doh://google")
|
childResolver, err := netx.NewDNSClient(netx.Config{Logger: log.Log}, "doh://google")
|
||||||
runtimex.PanicOnError(err, "NewDNSClient failed")
|
runtimex.PanicOnError(err, "NewDNSClient failed")
|
||||||
var resolver netxlite.Resolver = childResolver
|
var resolver netxlite.Resolver = childResolver
|
||||||
resolver = &netxlite.IDNAResolver{Resolver: resolver}
|
resolver = &netxlite.ResolverIDNA{Resolver: resolver}
|
||||||
}
|
}
|
||||||
return config.Resolver.LookupHost(ctx, config.Domain)
|
return config.Resolver.LookupHost(ctx, config.Domain)
|
||||||
}
|
}
|
||||||
|
|
|
@ -139,7 +139,7 @@ func NewResolver(config Config) Resolver {
|
||||||
if config.ResolveSaver != nil {
|
if config.ResolveSaver != nil {
|
||||||
r = resolver.SaverResolver{Resolver: r, Saver: config.ResolveSaver}
|
r = resolver.SaverResolver{Resolver: r, Saver: config.ResolveSaver}
|
||||||
}
|
}
|
||||||
return resolver.IDNAResolver{Resolver: r}
|
return &resolver.IDNAResolver{Resolver: r}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDialer creates a new Dialer from the specified config
|
// NewDialer creates a new Dialer from the specified config
|
||||||
|
|
|
@ -20,7 +20,7 @@ import (
|
||||||
|
|
||||||
func TestNewResolverVanilla(t *testing.T) {
|
func TestNewResolverVanilla(t *testing.T) {
|
||||||
r := netx.NewResolver(netx.Config{})
|
r := netx.NewResolver(netx.Config{})
|
||||||
ir, ok := r.(resolver.IDNAResolver)
|
ir, ok := r.(*resolver.IDNAResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ func TestNewResolverSpecificResolver(t *testing.T) {
|
||||||
// not initialized because it doesn't matter in this context
|
// not initialized because it doesn't matter in this context
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
ir, ok := r.(resolver.IDNAResolver)
|
ir, ok := r.(*resolver.IDNAResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ func TestNewResolverWithBogonFilter(t *testing.T) {
|
||||||
r := netx.NewResolver(netx.Config{
|
r := netx.NewResolver(netx.Config{
|
||||||
BogonIsError: true,
|
BogonIsError: true,
|
||||||
})
|
})
|
||||||
ir, ok := r.(resolver.IDNAResolver)
|
ir, ok := r.(*resolver.IDNAResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ func TestNewResolverWithLogging(t *testing.T) {
|
||||||
r := netx.NewResolver(netx.Config{
|
r := netx.NewResolver(netx.Config{
|
||||||
Logger: log.Log,
|
Logger: log.Log,
|
||||||
})
|
})
|
||||||
ir, ok := r.(resolver.IDNAResolver)
|
ir, ok := r.(*resolver.IDNAResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,7 @@ func TestNewResolverWithSaver(t *testing.T) {
|
||||||
r := netx.NewResolver(netx.Config{
|
r := netx.NewResolver(netx.Config{
|
||||||
ResolveSaver: saver,
|
ResolveSaver: saver,
|
||||||
})
|
})
|
||||||
ir, ok := r.(resolver.IDNAResolver)
|
ir, ok := r.(*resolver.IDNAResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -151,7 +151,7 @@ func TestNewResolverWithReadWriteCache(t *testing.T) {
|
||||||
r := netx.NewResolver(netx.Config{
|
r := netx.NewResolver(netx.Config{
|
||||||
CacheResolutions: true,
|
CacheResolutions: true,
|
||||||
})
|
})
|
||||||
ir, ok := r.(resolver.IDNAResolver)
|
ir, ok := r.(*resolver.IDNAResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
@ -182,7 +182,7 @@ func TestNewResolverWithPrefilledReadonlyCache(t *testing.T) {
|
||||||
"dns.google.com": {"8.8.8.8"},
|
"dns.google.com": {"8.8.8.8"},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
ir, ok := r.(resolver.IDNAResolver)
|
ir, ok := r.(*resolver.IDNAResolver)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("not the resolver we expected")
|
t.Fatal("not the resolver we expected")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,34 +1,11 @@
|
||||||
package resolver
|
package resolver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||||
|
|
||||||
"golang.org/x/net/idna"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// IDNAResolver is to support resolving Internationalized Domain Names.
|
// IDNAResolver is to support resolving Internationalized Domain Names.
|
||||||
// See RFC3492 for more information.
|
// See RFC3492 for more information.
|
||||||
type IDNAResolver struct {
|
type IDNAResolver = netxlite.ResolverIDNA
|
||||||
Resolver
|
|
||||||
}
|
|
||||||
|
|
||||||
// LookupHost implements Resolver.LookupHost
|
var _ Resolver = &IDNAResolver{}
|
||||||
func (r IDNAResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
|
||||||
host, err := idna.ToASCII(hostname)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return r.Resolver.LookupHost(ctx, host)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Network implements Resolver.Network.
|
|
||||||
func (r IDNAResolver) Network() string {
|
|
||||||
return "idna"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Address implements Resolver.Address.
|
|
||||||
func (r IDNAResolver) Address() string {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var _ Resolver = IDNAResolver{}
|
|
||||||
|
|
|
@ -1,76 +0,0 @@
|
||||||
package resolver_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
|
||||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ErrUnexpectedPunycode = errors.New("unexpected punycode value")
|
|
||||||
|
|
||||||
type CheckIDNAResolver struct {
|
|
||||||
Addresses []string
|
|
||||||
Error error
|
|
||||||
Expect string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (resolv CheckIDNAResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
|
||||||
if resolv.Error != nil {
|
|
||||||
return nil, resolv.Error
|
|
||||||
}
|
|
||||||
if hostname != resolv.Expect {
|
|
||||||
return nil, ErrUnexpectedPunycode
|
|
||||||
}
|
|
||||||
return resolv.Addresses, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r CheckIDNAResolver) Network() string {
|
|
||||||
return "checkidna"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r CheckIDNAResolver) Address() string {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIDNAResolverSuccess(t *testing.T) {
|
|
||||||
expectedIPs := []string{"77.88.55.66"}
|
|
||||||
resolv := resolver.IDNAResolver{Resolver: CheckIDNAResolver{
|
|
||||||
Addresses: expectedIPs,
|
|
||||||
Expect: "xn--d1acpjx3f.xn--p1ai",
|
|
||||||
}}
|
|
||||||
addrs, err := resolv.LookupHost(context.Background(), "яндекс.рф")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if diff := cmp.Diff(expectedIPs, addrs); diff != "" {
|
|
||||||
t.Fatal(diff)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIDNAResolverFailure(t *testing.T) {
|
|
||||||
resolv := resolver.IDNAResolver{Resolver: CheckIDNAResolver{
|
|
||||||
Error: errors.New("we should not arrive here"),
|
|
||||||
}}
|
|
||||||
// See https://www.farsightsecurity.com/blog/txt-record/punycode-20180711/
|
|
||||||
addrs, err := resolv.LookupHost(context.Background(), "xn--0000h")
|
|
||||||
if err == nil || !strings.HasPrefix(err.Error(), "idna: invalid label") {
|
|
||||||
t.Fatal("not the error we expected")
|
|
||||||
}
|
|
||||||
if addrs != nil {
|
|
||||||
t.Fatal("expected no response here")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIDNAResolverTransportOK(t *testing.T) {
|
|
||||||
resolv := resolver.IDNAResolver{Resolver: CheckIDNAResolver{}}
|
|
||||||
if resolv.Network() != "idna" {
|
|
||||||
t.Fatal("invalid network")
|
|
||||||
}
|
|
||||||
if resolv.Address() != "" {
|
|
||||||
t.Fatal("invalid address")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -44,8 +44,8 @@ func testresolverquickidna(t *testing.T, reso resolver.Resolver) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
t.Skip("skip test in short mode")
|
t.Skip("skip test in short mode")
|
||||||
}
|
}
|
||||||
reso = resolver.IDNAResolver{
|
reso = &resolver.IDNAResolver{
|
||||||
&netxlite.ResolverLogger{Logger: log.Log, Resolver: reso},
|
Resolver: &netxlite.ResolverLogger{Logger: log.Log, Resolver: reso},
|
||||||
}
|
}
|
||||||
addrs, err := reso.LookupHost(context.Background(), "яндекс.рф")
|
addrs, err := reso.LookupHost(context.Background(), "яндекс.рф")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -83,14 +83,15 @@ func (r *ResolverLogger) Address() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// IDNAResolver is to support resolving Internationalized Domain Names.
|
// ResolverIDNA supports resolving Internationalized Domain Names.
|
||||||
|
//
|
||||||
// See RFC3492 for more information.
|
// See RFC3492 for more information.
|
||||||
type IDNAResolver struct {
|
type ResolverIDNA struct {
|
||||||
Resolver
|
Resolver
|
||||||
}
|
}
|
||||||
|
|
||||||
// LookupHost implements Resolver.LookupHost
|
// LookupHost implements Resolver.LookupHost.
|
||||||
func (r *IDNAResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
func (r *ResolverIDNA) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
||||||
host, err := idna.ToASCII(hostname)
|
host, err := idna.ToASCII(hostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -99,7 +100,7 @@ func (r *IDNAResolver) LookupHost(ctx context.Context, hostname string) ([]strin
|
||||||
}
|
}
|
||||||
|
|
||||||
// Network implements Resolver.Network.
|
// Network implements Resolver.Network.
|
||||||
func (r *IDNAResolver) Network() string {
|
func (r *ResolverIDNA) Network() string {
|
||||||
if rn, ok := r.Resolver.(resolverNetworker); ok {
|
if rn, ok := r.Resolver.(resolverNetworker); ok {
|
||||||
return rn.Network()
|
return rn.Network()
|
||||||
}
|
}
|
||||||
|
@ -107,7 +108,7 @@ func (r *IDNAResolver) Network() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Address implements Resolver.Address.
|
// Address implements Resolver.Address.
|
||||||
func (r *IDNAResolver) Address() string {
|
func (r *ResolverIDNA) Address() string {
|
||||||
if ra, ok := r.Resolver.(resolverAddresser); ok {
|
if ra, ok := r.Resolver.(resolverAddresser); ok {
|
||||||
return ra.Address()
|
return ra.Address()
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
|
@ -89,3 +90,64 @@ func TestResolverLoggerNoChildNetworkAddress(t *testing.T) {
|
||||||
t.Fatal("invalid Address")
|
t.Fatal("invalid Address")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResolverIDNAWorksAsIntended(t *testing.T) {
|
||||||
|
expectedIPs := []string{"77.88.55.66"}
|
||||||
|
r := &ResolverIDNA{
|
||||||
|
Resolver: &netxmocks.Resolver{
|
||||||
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||||
|
if domain != "xn--d1acpjx3f.xn--p1ai" {
|
||||||
|
return nil, errors.New("passed invalid domain")
|
||||||
|
}
|
||||||
|
return expectedIPs, nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
ctx := context.Background()
|
||||||
|
addrs, err := r.LookupHost(ctx, "яндекс.рф")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if diff := cmp.Diff(expectedIPs, addrs); diff != "" {
|
||||||
|
t.Fatal(diff)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIDNAResolverWithInvalidPunycode(t *testing.T) {
|
||||||
|
r := &ResolverIDNA{Resolver: &netxmocks.Resolver{
|
||||||
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||||
|
return nil, errors.New("should not happen")
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
// See https://www.farsightsecurity.com/blog/txt-record/punycode-20180711/
|
||||||
|
ctx := context.Background()
|
||||||
|
addrs, err := r.LookupHost(ctx, "xn--0000h")
|
||||||
|
if err == nil || !strings.HasPrefix(err.Error(), "idna: invalid label") {
|
||||||
|
t.Fatal("not the error we expected")
|
||||||
|
}
|
||||||
|
if addrs != nil {
|
||||||
|
t.Fatal("expected no response here")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolverIDNAChildNetworkAddress(t *testing.T) {
|
||||||
|
r := &ResolverIDNA{
|
||||||
|
Resolver: DefaultResolver,
|
||||||
|
}
|
||||||
|
if v := r.Network(); v != "system" {
|
||||||
|
t.Fatal("invalid network", v)
|
||||||
|
}
|
||||||
|
if v := r.Address(); v != "" {
|
||||||
|
t.Fatal("invalid address", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResolverIDNANoChildNetworkAddress(t *testing.T) {
|
||||||
|
r := &ResolverIDNA{}
|
||||||
|
if v := r.Network(); v != "idna" {
|
||||||
|
t.Fatal("invalid network", v)
|
||||||
|
}
|
||||||
|
if v := r.Address(); v != "" {
|
||||||
|
t.Fatal("invalid address", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user