diff --git a/dnsx/dnsx.go b/dnsx/dnsx.go index addba96..f23a88b 100644 --- a/dnsx/dnsx.go +++ b/dnsx/dnsx.go @@ -25,7 +25,7 @@ type Client interface { LookupNS(ctx context.Context, name string) ([]*net.NS, error) } -// RoundTripper represent an abstract DNS transport. +// RoundTripper represents an abstract DNS transport. type RoundTripper interface { // RoundTrip sends a DNS query and receives the reply. RoundTrip(query []byte) (reply []byte, err error) diff --git a/httpx/httpx.go b/httpx/httpx.go index eff8bb0..d0f332b 100644 --- a/httpx/httpx.go +++ b/httpx/httpx.go @@ -1,6 +1,6 @@ // Package httpx contains OONI's net/http extensions. It defines the Client and // the Transport replacements that we should use in OONI. They emit measurements -// collected at network and HTTP level on a specific channel. +// collected at network and HTTP level using a specific handler. package httpx import ( @@ -20,8 +20,7 @@ type Transport struct { } // NewTransport creates a new Transport. The beginning argument is -// the time to use as zero for computing the elapsed time. The ch -// channel is where we'll emit Measurements. +// the time to use as zero for computing the elapsed time. func NewTransport(beginning time.Time, handler model.Handler) *Transport { t := new(Transport) t.dialer = dialerapi.NewDialer(beginning, handler) diff --git a/internal/dialerapi/dialerapi.go b/internal/dialerapi/dialerapi.go index a662587..e25a08f 100644 --- a/internal/dialerapi/dialerapi.go +++ b/internal/dialerapi/dialerapi.go @@ -37,33 +37,33 @@ type DialHostPortFunc func( // Dialer defines the dialer API. We implement the most basic form // of DNS, but more advanced resolutions are possible. type Dialer struct { - dialerbase.Dialer + Beginning time.Time DialHostPort DialHostPortFunc Handler model.Handler LookupHost LookupHostFunc StartTLSHandshakeHook func(net.Conn) TLSConfig *tls.Config TLSHandshakeTimeout time.Duration + dialer *dialerbase.Dialer } // NewDialer creates a new Dialer. func NewDialer(beginning time.Time, handler model.Handler) (d *Dialer) { d = &Dialer{ - Dialer: dialerbase.Dialer{ - Beginning: beginning, - Dialer: net.Dialer{}, - Handler: handler, - }, + Beginning: beginning, Handler: handler, TLSConfig: &tls.Config{}, StartTLSHandshakeHook: func(net.Conn) {}, + dialer: dialerbase.NewDialer( + beginning, handler, + ), } // This is equivalent to ConfigureDNS("system", "...") r := &net.Resolver{ PreferGo: false, } d.LookupHost = r.LookupHost - d.DialHostPort = d.Dialer.DialHostPort + d.DialHostPort = d.dialer.DialHostPort return } diff --git a/internal/dialerbase/dialerbase.go b/internal/dialerbase/dialerbase.go index 496137f..b0ecd25 100644 --- a/internal/dialerbase/dialerbase.go +++ b/internal/dialerbase/dialerbase.go @@ -20,6 +20,15 @@ type Dialer struct { Handler model.Handler } +// NewDialer creates a new base dialer +func NewDialer(beginning time.Time, handler model.Handler) *Dialer { + return &Dialer{ + Dialer: net.Dialer{}, + Beginning: beginning, + Handler: handler, + } +} + // DialHostPort is like net.DialContext but requires a separate host // and port and returns a measurable net.Conn-like struct. func (d *Dialer) DialHostPort( diff --git a/internal/dialerbase/dialerbase_test.go b/internal/dialerbase/dialerbase_test.go index 9e552e3..8c21dc6 100644 --- a/internal/dialerbase/dialerbase_test.go +++ b/internal/dialerbase/dialerbase_test.go @@ -3,15 +3,16 @@ package dialerbase_test import ( "context" "testing" + "time" "github.com/ooni/netx/handlers" "github.com/ooni/netx/internal/dialerbase" ) func TestIntegrationSuccess(t *testing.T) { - dialer := dialerbase.Dialer{ - Handler: handlers.NoHandler, - } + dialer := dialerbase.NewDialer( + time.Now(), handlers.NoHandler, + ) conn, err := dialer.DialHostPort( context.Background(), "tcp", "8.8.8.8", "53", 17, ) @@ -22,9 +23,9 @@ func TestIntegrationSuccess(t *testing.T) { } func TestIntegrationErrorDomain(t *testing.T) { - dialer := dialerbase.Dialer{ - Handler: handlers.NoHandler, - } + dialer := dialerbase.NewDialer( + time.Now(), handlers.NoHandler, + ) conn, err := dialer.DialHostPort( context.Background(), "tcp", "dns.google.com", "53", 17, ) @@ -37,9 +38,9 @@ func TestIntegrationErrorDomain(t *testing.T) { } func TestIntegrationErrorNoConnect(t *testing.T) { - dialer := dialerbase.Dialer{ - Handler: handlers.NoHandler, - } + dialer := dialerbase.NewDialer( + time.Now(), handlers.NoHandler, + ) ctx, cancel := context.WithTimeout(context.Background(), 1) defer cancel() conn, err := dialer.DialHostPort( diff --git a/netx.go b/netx.go index a730533..768bf40 100644 --- a/netx.go +++ b/netx.go @@ -60,12 +60,12 @@ func NewDialer(handler model.Handler) *Dialer { // // For example: // -// d.SetResolver("system", "") -// d.SetResolver("godns", "") -// d.SetResolver("udp", "8.8.8.8:53") -// d.SetResolver("tcp", "8.8.8.8:53") -// d.SetResolver("dot", "dns.quad9.net") -// d.SetResolver("doh", "https://cloudflare-dns.com/dns-query") +// d.ConfigureDNS("system", "") +// d.ConfigureDNS("godns", "") +// d.ConfigureDNS("udp", "8.8.8.8:53") +// d.ConfigureDNS("tcp", "8.8.8.8:53") +// d.ConfigureDNS("dot", "dns.quad9.net") +// d.ConfigureDNS("doh", "https://cloudflare-dns.com/dns-query") // // ConfigureDNS is currently only executed when Go chooses to // use the pure Go implementation of the DNS. This means that it @@ -108,7 +108,7 @@ func (d *Dialer) NewResolver(network, address string) (dnsx.Client, error) { } // SetCABundle configures the dialer to use a specific CA bundle. This -// function is not goroutine safe. Make sure you call it befor starting +// function is not goroutine safe. Make sure you call it before starting // to use this specific dialer. func (d *Dialer) SetCABundle(path string) error { return d.dialer.SetCABundle(path)