feat: add a simple dnsping experiment (#674)
See https://github.com/ooni/probe/issues/1987 (issue). See https://github.com/ooni/spec/pull/238 (impl). While there, fix the build for go1.18 by adding go1.18 specific tests. I was increasingly bothered by the build being red.
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
// Package dnsping is the experimental dnsping experiment.
|
||||
//
|
||||
// See https://github.com/ooni/spec/blob/master/nettests/ts-035-dnsping.md.
|
||||
package dnsping
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/measurex"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
const (
|
||||
testName = "dnsping"
|
||||
testVersion = "0.1.0"
|
||||
)
|
||||
|
||||
// Config contains the experiment configuration.
|
||||
type Config struct {
|
||||
// Delay is the delay between each repetition (in milliseconds).
|
||||
Delay int64 `ooni:"number of milliseconds to wait before sending each ping"`
|
||||
|
||||
// Domains is the space-separated list of domains to measure.
|
||||
Domains string `ooni:"space-separated list of domains to measure"`
|
||||
|
||||
// Repetitions is the number of repetitions for each ping.
|
||||
Repetitions int64 `ooni:"number of times to repeat the measurement"`
|
||||
}
|
||||
|
||||
func (c *Config) delay() time.Duration {
|
||||
if c.Delay > 0 {
|
||||
return time.Duration(c.Delay) * time.Millisecond
|
||||
}
|
||||
return time.Second
|
||||
}
|
||||
|
||||
func (c Config) repetitions() int64 {
|
||||
if c.Repetitions > 0 {
|
||||
return c.Repetitions
|
||||
}
|
||||
return 10
|
||||
}
|
||||
|
||||
func (c Config) domains() string {
|
||||
if c.Domains != "" {
|
||||
return c.Domains
|
||||
}
|
||||
return "edge-chat.instagram.com example.com"
|
||||
}
|
||||
|
||||
// TestKeys contains the experiment results.
|
||||
type TestKeys struct {
|
||||
Pings []*SinglePing `json:"pings"`
|
||||
}
|
||||
|
||||
// TODO(bassosimone): save more data once the dnsping improvements at
|
||||
// github.com/bassosimone/websteps-illustrated contains have been merged
|
||||
// into this repository. When this happens, we'll able to save raw
|
||||
// queries and network events of each individual query.
|
||||
|
||||
// SinglePing contains the results of a single ping.
|
||||
type SinglePing struct {
|
||||
Queries []*measurex.ArchivalDNSLookupEvent `json:"queries"`
|
||||
}
|
||||
|
||||
// Measurer performs the measurement.
|
||||
type Measurer struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
// ExperimentName implements ExperimentMeasurer.ExperiExperimentName.
|
||||
func (m *Measurer) ExperimentName() string {
|
||||
return testName
|
||||
}
|
||||
|
||||
// ExperimentVersion implements ExperimentMeasurer.ExperimentVersion.
|
||||
func (m *Measurer) ExperimentVersion() string {
|
||||
return testVersion
|
||||
}
|
||||
|
||||
var (
|
||||
// errNoInputProvided indicates you didn't provide any input
|
||||
errNoInputProvided = errors.New("not input provided")
|
||||
|
||||
// errInputIsNotAnURL indicates that input is not an URL
|
||||
errInputIsNotAnURL = errors.New("input is not an URL")
|
||||
|
||||
// errInvalidScheme indicates that the scheme is invalid
|
||||
errInvalidScheme = errors.New("scheme must be udp")
|
||||
|
||||
// errMissingPort indicates that there is no port.
|
||||
errMissingPort = errors.New("the URL must include a port")
|
||||
)
|
||||
|
||||
// Run implements ExperimentMeasurer.Run.
|
||||
func (m *Measurer) Run(
|
||||
ctx context.Context,
|
||||
sess model.ExperimentSession,
|
||||
measurement *model.Measurement,
|
||||
callbacks model.ExperimentCallbacks,
|
||||
) error {
|
||||
if measurement.Input == "" {
|
||||
return errNoInputProvided
|
||||
}
|
||||
parsed, err := url.Parse(string(measurement.Input))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %s", errInputIsNotAnURL, err.Error())
|
||||
}
|
||||
if parsed.Scheme != "udp" {
|
||||
return errInvalidScheme
|
||||
}
|
||||
if parsed.Port() == "" {
|
||||
return errMissingPort
|
||||
}
|
||||
tk := new(TestKeys)
|
||||
measurement.TestKeys = tk
|
||||
mxmx := measurex.NewMeasurerWithDefaultSettings()
|
||||
out := make(chan *measurex.DNSMeasurement)
|
||||
domains := strings.Split(m.config.domains(), " ")
|
||||
for _, domain := range domains {
|
||||
go m.dnsPingLoop(ctx, mxmx, parsed.Host, domain, out)
|
||||
}
|
||||
// The following multiplication could overflow but we're always using small
|
||||
// numbers so it's fine for us not to bother with checking for that.
|
||||
//
|
||||
// We emit two results (A and AAAA) for each domain and repetition.
|
||||
numResults := int(m.config.repetitions()) * len(domains) * 2
|
||||
for len(tk.Pings) < numResults {
|
||||
meas := <-out
|
||||
// TODO(bassosimone): when we merge the improvements at
|
||||
// https://github.com/bassosimone/websteps-illustrated it
|
||||
// will become unnecessary to split with query type
|
||||
// as we're doing below.
|
||||
queries := measurex.NewArchivalDNSLookupEventList(meas.LookupHost)
|
||||
tk.Pings = append(tk.Pings, m.onlyQueryWithType(queries, "A")...)
|
||||
tk.Pings = append(tk.Pings, m.onlyQueryWithType(queries, "AAAA")...)
|
||||
}
|
||||
return nil // return nil so we always submit the measurement
|
||||
}
|
||||
|
||||
// onlyQueryWithType returns only the queries with the given type.
|
||||
func (m *Measurer) onlyQueryWithType(
|
||||
in []*measurex.ArchivalDNSLookupEvent, kind string) (out []*SinglePing) {
|
||||
for _, query := range in {
|
||||
if query.QueryType == kind {
|
||||
out = append(out, &SinglePing{
|
||||
Queries: []*measurex.ArchivalDNSLookupEvent{query},
|
||||
})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// dnsPingLoop sends all the ping requests and emits the results onto the out channel.
|
||||
func (m *Measurer) dnsPingLoop(ctx context.Context, mxmx *measurex.Measurer,
|
||||
address string, domain string, out chan<- *measurex.DNSMeasurement) {
|
||||
ticker := time.NewTicker(m.config.delay())
|
||||
defer ticker.Stop()
|
||||
for i := int64(0); i < m.config.repetitions(); i++ {
|
||||
go m.dnsPingAsync(ctx, mxmx, address, domain, out)
|
||||
<-ticker.C
|
||||
}
|
||||
}
|
||||
|
||||
// dnsPingAsync performs a DNS ping and emits the result onto the out channel.
|
||||
func (m *Measurer) dnsPingAsync(ctx context.Context, mxmx *measurex.Measurer,
|
||||
address string, domain string, out chan<- *measurex.DNSMeasurement) {
|
||||
out <- m.dnsRoundTrip(ctx, mxmx, address, domain)
|
||||
}
|
||||
|
||||
// dnsRoundTrip performs a round trip and returns the results to the caller.
|
||||
func (m *Measurer) dnsRoundTrip(ctx context.Context, mxmx *measurex.Measurer,
|
||||
address string, domain string) *measurex.DNSMeasurement {
|
||||
// TODO(bassosimone): make the timeout user-configurable
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
return mxmx.LookupHostUDP(ctx, domain, address)
|
||||
}
|
||||
|
||||
// NewExperimentMeasurer creates a new ExperimentMeasurer.
|
||||
func NewExperimentMeasurer(config Config) model.ExperimentMeasurer {
|
||||
return &Measurer{config: config}
|
||||
}
|
||||
|
||||
// SummaryKeys contains summary keys for this experiment.
|
||||
//
|
||||
// Note that this structure is part of the ABI contract with ooniprobe
|
||||
// therefore we should be careful when changing it.
|
||||
type SummaryKeys struct {
|
||||
IsAnomaly bool `json:"-"`
|
||||
}
|
||||
|
||||
// GetSummaryKeys implements model.ExperimentMeasurer.GetSummaryKeys.
|
||||
func (m Measurer) GetSummaryKeys(measurement *model.Measurement) (interface{}, error) {
|
||||
return SummaryKeys{IsAnomaly: false}, nil
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
package dnsping
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/mockable"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
||||
)
|
||||
|
||||
func TestConfig_domains(t *testing.T) {
|
||||
c := Config{}
|
||||
if c.domains() != "edge-chat.instagram.com example.com" {
|
||||
t.Fatal("invalid default domains list")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_repetitions(t *testing.T) {
|
||||
c := Config{}
|
||||
if c.repetitions() != 10 {
|
||||
t.Fatal("invalid default number of repetitions")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfig_delay(t *testing.T) {
|
||||
c := Config{}
|
||||
if c.delay() != time.Second {
|
||||
t.Fatal("invalid default delay")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMeasurer_run(t *testing.T) {
|
||||
// expectedPings is the expected number of pings
|
||||
const expectedPings = 4
|
||||
|
||||
// runHelper is an helper function to run this set of tests.
|
||||
runHelper := func(input string) (*model.Measurement, model.ExperimentMeasurer, error) {
|
||||
m := NewExperimentMeasurer(Config{
|
||||
Domains: "example.com",
|
||||
Delay: 1, // millisecond
|
||||
Repetitions: expectedPings,
|
||||
})
|
||||
if m.ExperimentName() != "dnsping" {
|
||||
t.Fatal("invalid experiment name")
|
||||
}
|
||||
if m.ExperimentVersion() != "0.1.0" {
|
||||
t.Fatal("invalid experiment version")
|
||||
}
|
||||
ctx := context.Background()
|
||||
meas := &model.Measurement{
|
||||
Input: model.MeasurementTarget(input),
|
||||
}
|
||||
sess := &mockable.Session{
|
||||
MockableLogger: model.DiscardLogger,
|
||||
}
|
||||
callbacks := model.NewPrinterCallbacks(model.DiscardLogger)
|
||||
err := m.Run(ctx, sess, meas, callbacks)
|
||||
return meas, m, err
|
||||
}
|
||||
|
||||
t.Run("with empty input", func(t *testing.T) {
|
||||
_, _, err := runHelper("")
|
||||
if !errors.Is(err, errNoInputProvided) {
|
||||
t.Fatal("unexpected error", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with invalid URL", func(t *testing.T) {
|
||||
_, _, err := runHelper("\t")
|
||||
if !errors.Is(err, errInputIsNotAnURL) {
|
||||
t.Fatal("unexpected error", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with invalid scheme", func(t *testing.T) {
|
||||
_, _, err := runHelper("https://8.8.8.8:443/")
|
||||
if !errors.Is(err, errInvalidScheme) {
|
||||
t.Fatal("unexpected error", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with missing port", func(t *testing.T) {
|
||||
_, _, err := runHelper("udp://8.8.8.8")
|
||||
if !errors.Is(err, errMissingPort) {
|
||||
t.Fatal("unexpected error", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with local listener", func(t *testing.T) {
|
||||
srvrURL, dnsListener, err := startDNSServer()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer dnsListener.Close()
|
||||
meas, m, err := runHelper(srvrURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tk := meas.TestKeys.(*TestKeys)
|
||||
if len(tk.Pings) != expectedPings*2 { // account for A & AAAA pings
|
||||
t.Fatal("unexpected number of pings")
|
||||
}
|
||||
ask, err := m.GetSummaryKeys(meas)
|
||||
if err != nil {
|
||||
t.Fatal("cannot obtain summary")
|
||||
}
|
||||
summary := ask.(SummaryKeys)
|
||||
if summary.IsAnomaly {
|
||||
t.Fatal("expected no anomaly")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// startDNSServer starts a local DNS server.
|
||||
func startDNSServer() (string, net.PacketConn, error) {
|
||||
dnsListener, err := net.ListenPacket("udp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
go runDNSServer(dnsListener)
|
||||
URL := &url.URL{
|
||||
Scheme: "udp",
|
||||
Host: dnsListener.LocalAddr().String(),
|
||||
Path: "/",
|
||||
}
|
||||
return URL.String(), dnsListener, nil
|
||||
}
|
||||
|
||||
// runDNSServer runs the DNS server.
|
||||
func runDNSServer(dnsListener net.PacketConn) {
|
||||
ds := &dns.Server{
|
||||
Handler: &dnsHandler{},
|
||||
Net: "udp",
|
||||
PacketConn: dnsListener,
|
||||
}
|
||||
err := ds.ActivateAndServe()
|
||||
if !errors.Is(err, net.ErrClosed) {
|
||||
runtimex.PanicOnError(err, "ActivateAndServe failed")
|
||||
}
|
||||
}
|
||||
|
||||
// dnsHandler handles DNS requests.
|
||||
type dnsHandler struct{}
|
||||
|
||||
// ServeDNS serves a DNS request
|
||||
func (h *dnsHandler) ServeDNS(rw dns.ResponseWriter, req *dns.Msg) {
|
||||
m := new(dns.Msg)
|
||||
m.Compress = true
|
||||
m.MsgHdr.RecursionAvailable = true
|
||||
m.SetRcode(req, dns.RcodeServerFailure)
|
||||
rw.WriteMsg(m)
|
||||
}
|
||||
Reference in New Issue
Block a user