feat: dnsping using step-by-step (#831)

Reference issue for this pull request: https://github.com/ooni/probe/issues/2159

This diff refactors the `dnsping` experiment to use the [step-by-step measurement style](https://github.com/ooni/probe-cli/blob/master/docs/design/dd-003-step-by-step.md).

Co-authored-by: decfox <decfox@github.com>
Co-authored-by: Simone Basso <bassosimone@gmail.com>
This commit is contained in:
DecFox 2022-07-08 23:12:24 +05:30 committed by GitHub
commit 5501b2201a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 747 additions and 77 deletions

View file

@ -7,6 +7,7 @@ package measurexlite
import (
"time"
"github.com/miekg/dns"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
@ -31,13 +32,17 @@ import (
type Trace struct {
// Index is the MANDATORY unique index of this trace within the
// current measurement. If you don't care about uniquely identifying
// treaces, you can use zero to indicate the "default" trace.
// traces, you can use zero to indicate the "default" trace.
Index int64
// NetworkEvent is MANDATORY and buffers network events. If you create
// this channel manually, ensure it has some buffer.
NetworkEvent chan *model.ArchivalNetworkEvent
// NewParallelResolverFn is OPTIONAL and can be used to overide
// calls to the netxlite.NewParallelResolver factory.
NewParallelResolverFn func() model.Resolver
// NewDialerWithoutResolverFn is OPTIONAL and can be used to override
// calls to the netxlite.NewDialerWithoutResolver factory.
NewDialerWithoutResolverFn func(dl model.DebugLogger) model.Dialer
@ -46,6 +51,14 @@ type Trace struct {
// calls to the netxlite.NewTLSHandshakerStdlib factory.
NewTLSHandshakerStdlibFn func(dl model.DebugLogger) model.TLSHandshaker
// DNSLookup is MANDATORY and buffers DNSLookup results based on the
// query type. When we create this map using NewTrace, we will create
// an entry for each dns.Type in DNSQueryTypes. If you create this channel
// manually, you probably want to to the same (and most likely you also
// want to create buffered channels). Note that the code will print a
// warning and otherwise ignore all the query types not included in this map.
DNSLookup map[uint16]chan *model.ArchivalDNSLookupResult
// TCPConnect is MANDATORY and buffers TCP connect observations. If you create
// this channel manually, ensure it has some buffer.
TCPConnect chan *model.ArchivalTCPConnectResult
@ -67,6 +80,10 @@ const (
// the Trace's NetworkEvent buffered channel.
NetworkEventBufferSize = 64
// DNSLookupBufferSize is the buffer size for constructing
// the Trace's DNSLookup map of buffered channels.
DNSLookupBufferSize = 8
// TCPConnectBufferSize is the buffer size for constructing
// the Trace's TCPConnect buffered channel.
TCPConnectBufferSize = 8
@ -76,6 +93,25 @@ const (
TLSHandshakeBufferSize = 8
)
// DNSQueryTypes contains the list of DNS query types for which
// NewTrace create entries in Trace.DNSLookup.
var DNSQueryTypes = []uint16{
dns.TypeANY,
dns.TypeA,
dns.TypeAAAA,
dns.TypeCNAME,
dns.TypeNS,
}
// newDefaultDNSLookupMap is a convenience factory for creating Trace.DNSLookup
func newDefaultDNSLookupMap() map[uint16]chan *model.ArchivalDNSLookupResult {
out := make(map[uint16]chan *model.ArchivalDNSLookupResult)
for _, qtype := range DNSQueryTypes {
out[qtype] = make(chan *model.ArchivalDNSLookupResult, DNSLookupBufferSize)
}
return out
}
// NewTrace creates a new instance of Trace using default settings.
//
// We create buffered channels using as buffer sizes the constants that
@ -96,6 +132,7 @@ func NewTrace(index int64, zeroTime time.Time) *Trace {
),
NewDialerWithoutResolverFn: nil, // use default
NewTLSHandshakerStdlibFn: nil, // use default
DNSLookup: newDefaultDNSLookupMap(),
TCPConnect: make(
chan *model.ArchivalTCPConnectResult,
TCPConnectBufferSize,
@ -110,7 +147,7 @@ func NewTrace(index int64, zeroTime time.Time) *Trace {
}
// newDialerWithoutResolver indirectly calls netxlite.NewDialerWithoutResolver
// thus allows us to mock this func for testing.
// thus allowing us to mock this func for testing.
func (tx *Trace) newDialerWithoutResolver(dl model.DebugLogger) model.Dialer {
if tx.NewDialerWithoutResolverFn != nil {
return tx.NewDialerWithoutResolverFn(dl)
@ -118,6 +155,15 @@ func (tx *Trace) newDialerWithoutResolver(dl model.DebugLogger) model.Dialer {
return netxlite.NewDialerWithoutResolver(dl)
}
// newParallelResolver indirectly calls the passed netxlite.NewParallerResolver
// thus allowing us to mock this function for testing
func (tx *Trace) newParallelResolver(newResolver func() model.Resolver) model.Resolver {
if tx.NewParallelResolverFn != nil {
return tx.NewParallelResolverFn()
}
return newResolver()
}
// newTLSHandshakerStdlib indirectly calls netxlite.NewTLSHandshakerStdlib
// thus allowing us to mock this func for testing.
func (tx *Trace) newTLSHandshakerStdlib(dl model.DebugLogger) model.TLSHandshaker {