This repository has been archived by the owner on Mar 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge nervousresolver chaining with stable code (#106)
Now, rather than using nervousresolver as an opt-in that is not so easy to use, you can chain as many resolvers as you wish. Xref: ooni/probe-engine#88
- Loading branch information
1 parent
d7d1d58
commit c29f7ee
Showing
15 changed files
with
344 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Package brokenresolver is a broken resolver | ||
package brokenresolver | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
// Resolver is a broken resolver. | ||
type Resolver struct{} | ||
|
||
// New creates a new broken Resolver instance. | ||
func New() *Resolver { | ||
return &Resolver{} | ||
} | ||
|
||
var errNotFound = &net.DNSError{ | ||
Err: "no such host", | ||
} | ||
|
||
// LookupAddr returns the name of the provided IP address | ||
func (c *Resolver) LookupAddr(ctx context.Context, addr string) ([]string, error) { | ||
return nil, errNotFound | ||
} | ||
|
||
// LookupCNAME returns the canonical name of a host | ||
func (c *Resolver) LookupCNAME(ctx context.Context, host string) (string, error) { | ||
return "", errNotFound | ||
} | ||
|
||
// LookupHost returns the IP addresses of a host | ||
func (c *Resolver) LookupHost(ctx context.Context, hostname string) ([]string, error) { | ||
return nil, errNotFound | ||
} | ||
|
||
// LookupMX returns the MX records of a specific name | ||
func (c *Resolver) LookupMX(ctx context.Context, name string) ([]*net.MX, error) { | ||
return nil, errNotFound | ||
} | ||
|
||
// LookupNS returns the NS records of a specific name | ||
func (c *Resolver) LookupNS(ctx context.Context, name string) ([]*net.NS, error) { | ||
return nil, errNotFound | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package brokenresolver | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestLookupAddr(t *testing.T) { | ||
client := New() | ||
names, err := client.LookupAddr(context.Background(), "8.8.8.8") | ||
if err == nil { | ||
t.Fatal("expected an error here") | ||
} | ||
if names != nil { | ||
t.Fatal("expected nil here") | ||
} | ||
} | ||
|
||
func TestLookupCNAME(t *testing.T) { | ||
client := New() | ||
cname, err := client.LookupCNAME(context.Background(), "www.ooni.io") | ||
if err == nil { | ||
t.Fatal("expected an error here") | ||
} | ||
if cname != "" { | ||
t.Fatal("expected empty string here") | ||
} | ||
} | ||
|
||
func TestLookupHost(t *testing.T) { | ||
client := New() | ||
addrs, err := client.LookupHost(context.Background(), "www.google.com") | ||
if err == nil { | ||
t.Fatal("expected an error here") | ||
} | ||
if addrs != nil { | ||
t.Fatal("expected nil here") | ||
} | ||
} | ||
|
||
func TestLookupMX(t *testing.T) { | ||
client := New() | ||
records, err := client.LookupMX(context.Background(), "ooni.io") | ||
if err == nil { | ||
t.Fatal("expected an error here") | ||
} | ||
if records != nil { | ||
t.Fatal("expected nil here") | ||
} | ||
} | ||
|
||
func TestLookupNS(t *testing.T) { | ||
client := New() | ||
records, err := client.LookupNS(context.Background(), "ooni.io") | ||
if err == nil { | ||
t.Fatal("expected an error here") | ||
} | ||
if records != nil { | ||
t.Fatal("expected nil here") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Package chainresolver allows to chain two resolvers | ||
package chainresolver | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"github.com/ooni/netx/model" | ||
) | ||
|
||
// Resolver is a chain resolver. | ||
type Resolver struct { | ||
primary model.DNSResolver | ||
secondary model.DNSResolver | ||
} | ||
|
||
// New creates a new chain Resolver instance. | ||
func New(primary, secondary model.DNSResolver) *Resolver { | ||
return &Resolver{ | ||
primary: primary, | ||
secondary: secondary, | ||
} | ||
} | ||
|
||
// LookupAddr returns the name of the provided IP address | ||
func (c *Resolver) LookupAddr(ctx context.Context, addr string) ([]string, error) { | ||
names, err := c.primary.LookupAddr(ctx, addr) | ||
if err != nil { | ||
names, err = c.secondary.LookupAddr(ctx, addr) | ||
} | ||
return names, err | ||
} | ||
|
||
// LookupCNAME returns the canonical name of a host | ||
func (c *Resolver) LookupCNAME(ctx context.Context, host string) (string, error) { | ||
cname, err := c.primary.LookupCNAME(ctx, host) | ||
if err != nil { | ||
cname, err = c.secondary.LookupCNAME(ctx, host) | ||
} | ||
return cname, err | ||
} | ||
|
||
// LookupHost returns the IP addresses of a host | ||
func (c *Resolver) LookupHost(ctx context.Context, hostname string) ([]string, error) { | ||
addrs, err := c.primary.LookupHost(ctx, hostname) | ||
if err != nil { | ||
addrs, err = c.secondary.LookupHost(ctx, hostname) | ||
} | ||
return addrs, err | ||
} | ||
|
||
// LookupMX returns the MX records of a specific name | ||
func (c *Resolver) LookupMX(ctx context.Context, name string) ([]*net.MX, error) { | ||
records, err := c.primary.LookupMX(ctx, name) | ||
if err != nil { | ||
records, err = c.secondary.LookupMX(ctx, name) | ||
} | ||
return records, err | ||
} | ||
|
||
// LookupNS returns the NS records of a specific name | ||
func (c *Resolver) LookupNS(ctx context.Context, name string) ([]*net.NS, error) { | ||
records, err := c.primary.LookupNS(ctx, name) | ||
if err != nil { | ||
records, err = c.secondary.LookupNS(ctx, name) | ||
} | ||
return records, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package chainresolver | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"testing" | ||
|
||
"github.com/ooni/netx/internal/resolver/brokenresolver" | ||
) | ||
|
||
func TestLookupAddr(t *testing.T) { | ||
client := New(brokenresolver.New(), new(net.Resolver)) | ||
names, err := client.LookupAddr(context.Background(), "8.8.8.8") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if names == nil { | ||
t.Fatal("expect non nil return value here") | ||
} | ||
} | ||
|
||
func TestLookupCNAME(t *testing.T) { | ||
client := New(brokenresolver.New(), new(net.Resolver)) | ||
cname, err := client.LookupCNAME(context.Background(), "www.ooni.io") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if cname == "" { | ||
t.Fatal("expect non empty return value here") | ||
} | ||
} | ||
|
||
func TestLookupHost(t *testing.T) { | ||
client := New(brokenresolver.New(), new(net.Resolver)) | ||
addrs, err := client.LookupHost(context.Background(), "www.google.com") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if addrs == nil { | ||
t.Fatal("expect non nil return value here") | ||
} | ||
} | ||
|
||
func TestLookupMX(t *testing.T) { | ||
client := New(brokenresolver.New(), new(net.Resolver)) | ||
records, err := client.LookupMX(context.Background(), "ooni.io") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if records == nil { | ||
t.Fatal("expect non nil return value here") | ||
} | ||
} | ||
|
||
func TestLookupNS(t *testing.T) { | ||
client := New(brokenresolver.New(), new(net.Resolver)) | ||
records, err := client.LookupNS(context.Background(), "ooni.io") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if records == nil { | ||
t.Fatal("expect non nil return value here") | ||
} | ||
} |
Oops, something went wrong.