Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

Commit

Permalink
dnsdialer: allow to divert lookuphost
Browse files Browse the repository at this point in the history
A MeasurementRoot can contain an alternative LookupHost function
which may be used to bypass the usual chain.

This is also functional to the OONI reactive system.

See ooni/probe-engine#87
  • Loading branch information
bassosimone committed Oct 28, 2019
1 parent 518daf9 commit 80a70fe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/dialer/dnsdialer/dnsdialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,9 @@ func (d *Dialer) lookupHost(ctx context.Context, hostname string) ([]string, err
if net.ParseIP(hostname) != nil {
return []string{hostname}, nil
}
root := model.ContextMeasurementRootOrDefault(ctx)
if root.LookupHost != nil {
return root.LookupHost(ctx, hostname)
}
return d.resolver.LookupHost(ctx, hostname)
}
20 changes: 20 additions & 0 deletions internal/dialer/dnsdialer/dnsdialer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/ooni/netx/handlers"
"github.com/ooni/netx/model"
)

Expand Down Expand Up @@ -93,3 +94,22 @@ func TestReduceErrors(t *testing.T) {
}
})
}

func TestIntegrationDivertLookupHost(t *testing.T) {
dialer := newdialer()
root := &model.MeasurementRoot{
Beginning: time.Now(),
Handler: handlers.NoHandler,
LookupHost: func(ctx context.Context, hostname string) ([]string, error) {
return nil, errors.New("mocked error")
},
}
ctx := model.WithMeasurementRoot(context.Background(), root)
conn, err := dialer.DialContext(ctx, "tcp", "google.com:443")
if err == nil {
t.Fatal("expected an error here")
}
if conn != nil {
t.Fatal("expected a nil conn here")
}
}
4 changes: 4 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@ type MeasurementRoot struct {

// Handler is the handler that will handle events.
Handler Handler

// LookupHost allows to override the host lookup for all the request
// and dials that use this measurement root.
LookupHost func(ctx context.Context, hostname string) ([]string, error)
}

type measurementRootContextKey struct{}
Expand Down

0 comments on commit 80a70fe

Please sign in to comment.