ooni-probe-cli/internal/tutorial/netxlite/chapter06
DecFox 5501b2201a
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>
2022-07-08 19:42:24 +02:00
..
main.go feat: dnsping using step-by-step (#831) 2022-07-08 19:42:24 +02:00
README.md refactor: move tracex outside of engine/netx (#782) 2022-06-02 00:50:55 +02:00

Chapter I: Using a custom UDP resolver

In this chapter we will write together a main.go file that uses a custom UDP DNS resolver to lookup domain names.

This program is very similar to the one in the previous chapter except that we'll be configuring a custom resolver.

(This file is auto-generated from the corresponding source file, so make sure you don't edit it manually.)

The main.go file

We define main.go file using package main.

There's not much to say about the beginning of the program since it is equal to the one in the previous chapter.

package main

import (
	"context"
	"errors"
	"flag"
	"os"
	"time"

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

func main() {
	log.SetLevel(log.DebugLevel)
	hostname := flag.String("hostname", "dns.google", "Hostname to resolve")
	timeout := flag.Duration("timeout", 60*time.Second, "Timeout")
	serverAddr := flag.String("server-addr", "1.1.1.1:53", "DNS server address")
	flag.Parse()
	ctx, cancel := context.WithTimeout(context.Background(), *timeout)
	defer cancel()

Here's where we start to diverge. We create a dialer without a resolver, which is going to be used by the UDP resolver.

	dialer := netxlite.NewDialerWithoutResolver(log.Log)

Then, we create an UDP resolver. The arguments are the same as for creating a system resolver, except that we also need to specify the UDP endpoint address at which the server is listening.

	reso := netxlite.NewParallelResolverUDP(log.Log, dialer, *serverAddr)

The API we invoke is the same as in the previous chapter, though, and the rest of the program is equal to the one in the previous chapter.

	addrs, err := reso.LookupHost(ctx, *hostname)
	if err != nil {
		fatal(err)
	}
	log.Infof("resolver addrs: %+v", addrs)
}

func fatal(err error) {
	var ew *netxlite.ErrWrapper
	if !errors.As(err, &ew) {
		log.Fatal("cannot get ErrWrapper")
	}
	log.Warnf("error string    : %s", err.Error())
	log.Warnf("OONI failure    : %s", ew.Failure)
	log.Warnf("failed operation: %s", ew.Operation)
	log.Warnf("underlying error: %+v", ew.WrappedErr)
	os.Exit(1)
}

Running the code

Vanilla run

You can now run this code as follows:

go run -race ./internal/tutorial/netxlite/chapter06

You will see debug logs describing what is happening along with timing info.

NXDOMAIN

go run -race ./internal/tutorial/netxlite/chapter06 -hostname antani.ooni.io

should cause a dns_nxdomain_error, because the domain does not exist.

Timeout

go run -race ./internal/tutorial/netxlite/chapter06 -timeout 10us

should cause a timeout error, because the timeout is ridicolously small.

go run -race ./internal/tutorial/netxlite/chapter06 -server-addr 1.1.1.1:1

should also cause a timeout, because 1.1.1.1:1 is not an endpoint where a DNS-over-UDP resolver is listening.

Conclusions

We have seen how to use a custom DNS-over-UDP resolver.