Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ddr experiment #1675

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions internal/experiment/ddr/ddr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package ddr

import (
"context"
"errors"
"fmt"
"net"
"time"

"github.com/apex/log"
"github.com/miekg/dns"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)

const (
testName = "ddr"
testVersion = "0.1.0"
)

type Config struct {
// CustomResolver is the custom resolver to use.
// If empty, the system resolver is used.
CustomResolver *string
}

// Measurer performs the measurement.
type Measurer struct {
config Config
}

// ExperimentName implements ExperimentMeasurer.ExperimentName.
func (m *Measurer) ExperimentName() string {
return testName
}

// ExperimentVersion implements ExperimentMeasurer.ExperimentVersion.
func (m *Measurer) ExperimentVersion() string {
return testVersion
}

type DDRResponse struct {
Priority int `json:"priority"`
Target string `json:"target"`
Keys map[string]string `json:"keys"`
}

type TestKeys struct {
// DDRResponse is the DDR response.
DDRResponse []DDRResponse `json:"ddr_responses"`

// SupportsDDR is true if DDR is supported.
SupportsDDR bool `json:"supports_ddr"`

// Resolver is the resolver used (the system resolver of the host).
Resolver string `json:"resolver"`

// Failure is the failure that occurred, or nil.
Failure *string `json:"failure"`
}

func (m *Measurer) Run(
ctx context.Context,
args *model.ExperimentArgs) error {

log.SetLevel(log.DebugLevel)
measurement := args.Measurement

tk := &TestKeys{}
measurement.TestKeys = tk

timeout := 60 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()

if m.config.CustomResolver == nil {
systemResolver := getSystemResolverAddress()
if systemResolver == "" {
return errors.New("could not get system resolver")
}
log.Infof("Using system resolver: %s", systemResolver)
tk.Resolver = systemResolver
} else {
tk.Resolver = *m.config.CustomResolver
}

// DDR queries are queries of the SVCB type for the _dns.resolver.arpa. domain.

netx := &netxlite.Netx{}
dialer := netx.NewDialerWithoutResolver(log.Log)
transport := netxlite.NewUnwrappedDNSOverUDPTransport(
dialer, tk.Resolver)
encoder := &netxlite.DNSEncoderMiekg{}
query := encoder.Encode(
"_dns.resolver.arpa.", // As specified in RFC 9462
dns.TypeSVCB,
true)
resp, err := transport.RoundTrip(ctx, query)
if err != nil {
failure := err.Error()
tk.Failure = &failure
return nil
}

reply := &dns.Msg{}
err = reply.Unpack(resp.Bytes())
if err != nil {
unpackError := err.Error()
tk.Failure = &unpackError
return nil
}

ddrResponse, err := decodeResponse(reply.Answer)

if err != nil {
decodingError := err.Error()
tk.Failure = &decodingError
} else {
tk.DDRResponse = ddrResponse
}

tk.SupportsDDR = len(tk.DDRResponse) > 0

log.Infof("Gathered DDR Responses: %+v", tk.DDRResponse)
return nil
}

// decodeResponse decodes the response from the DNS query.
// DDR is only concerned with SVCB records, so we only decode those.
func decodeResponse(responseFields []dns.RR) ([]DDRResponse, error) {
responses := make([]DDRResponse, 0)
for _, rr := range responseFields {
switch rr := rr.(type) {
case *dns.SVCB:
parsed, err := parseSvcb(rr)
if err != nil {
return nil, err
}
responses = append(responses, parsed)
default:
return nil, fmt.Errorf("unknown RR type: %T", rr)
}
}
return responses, nil
}

func parseSvcb(rr *dns.SVCB) (DDRResponse, error) {
keys := make(map[string]string)
for _, kv := range rr.Value {
value := kv.String()
key := kv.Key().String()
keys[key] = value
}

return DDRResponse{
Priority: int(rr.Priority),
Target: rr.Target,
Keys: keys,
}, nil
}

// Get the system resolver address from /etc/resolv.conf
// This should also be possible via querying the system resolver and checking the response
func getSystemResolverAddress() string {
resolverConfig, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
return ""
}

if len(resolverConfig.Servers) > 0 {
return net.JoinHostPort(resolverConfig.Servers[0], resolverConfig.Port)
}

return ""
}

func NewExperimentMeasurer(config Config) model.ExperimentMeasurer {
return &Measurer{
config: config,
}
}
93 changes: 93 additions & 0 deletions internal/experiment/ddr/ddr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package ddr

import (
"context"
"testing"

"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/mocks"
"github.com/ooni/probe-cli/v3/internal/model"
)

func TestMeasurerExperimentNameVersion(t *testing.T) {
measurer := NewExperimentMeasurer(Config{})
if measurer.ExperimentName() != "ddr" {
t.Fatal("unexpected ExperimentName")
}
if measurer.ExperimentVersion() != "0.1.0" {
t.Fatal("unexpected ExperimentVersion")
}
}

func TestMeasurerRun(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}

oneOneOneOneResolver := "1.1.1.1:53"

measurer := NewExperimentMeasurer(Config{
CustomResolver: &oneOneOneOneResolver,
})
args := &model.ExperimentArgs{
Callbacks: model.NewPrinterCallbacks(log.Log),
Measurement: new(model.Measurement),
Session: &mocks.Session{
MockLogger: func() model.Logger {
return log.Log
},
},
}
if err := measurer.Run(context.Background(), args); err != nil {
t.Fatal(err)
}
tk := args.Measurement.TestKeys.(*TestKeys)
if tk.Failure != nil {
t.Fatal("unexpected Failure")
}

if tk.Resolver != oneOneOneOneResolver {
t.Fatal("Resolver should be written to TestKeys")
}

// 1.1.1.1 supports DDR
if tk.SupportsDDR != true {
t.Fatal("unexpected value for Supports DDR")
}
}

// This test fails because the resolver is a domain name and not an IP address.
func TestMeasurerFailsWithDomainResolver(t *testing.T) {
invalidResolver := "invalid-resolver.example:53"

tk, _ := runExperiment(invalidResolver)
if tk.Failure == nil {
t.Fatal("expected Failure")
}
}

func TestMeasurerFailsWithNoPort(t *testing.T) {
invalidResolver := "1.1.1.1"

tk, _ := runExperiment(invalidResolver)
if tk.Failure == nil {
t.Fatal("expected Failure")
}
}

func runExperiment(resolver string) (*TestKeys, error) {
measurer := NewExperimentMeasurer(Config{
CustomResolver: &resolver,
})
args := &model.ExperimentArgs{
Callbacks: model.NewPrinterCallbacks(log.Log),
Measurement: new(model.Measurement),
Session: &mocks.Session{
MockLogger: func() model.Logger {
return log.Log
},
},
}
err := measurer.Run(context.Background(), args)
return args.Measurement.TestKeys.(*TestKeys), err
}
28 changes: 28 additions & 0 deletions internal/registry/ddr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package registry

//
// Registers the `ddr' experiment.
//

import (
"github.com/ooni/probe-cli/v3/internal/experiment/ddr"
"github.com/ooni/probe-cli/v3/internal/model"
)

func init() {
const canonicalName = "ddr"
AllExperiments[canonicalName] = func() *Factory {
return &Factory{
build: func(config interface{}) model.ExperimentMeasurer {
return ddr.NewExperimentMeasurer(
*config.(*ddr.Config),
)
},
canonicalName: canonicalName,
config: &ddr.Config{},
enabledByDefault: true,
interruptible: true,
inputPolicy: model.InputNone,
}
}
}