2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
//
|
|
|
|
// DNS lookup and round trip
|
|
|
|
//
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
import (
|
|
|
|
"context"
|
2022-06-01 07:44:54 +02:00
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"time"
|
|
|
|
|
2022-01-07 18:33:37 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2022-08-27 15:47:48 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
// ResolverSaver is a resolver that saves events.
|
|
|
|
type ResolverSaver struct {
|
2022-06-01 07:44:54 +02:00
|
|
|
// Resolver is the underlying resolver.
|
|
|
|
Resolver model.Resolver
|
|
|
|
|
|
|
|
// Saver saves events.
|
2022-05-31 21:53:01 +02:00
|
|
|
Saver *Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
// WrapResolver wraps a model.Resolver with a SaverResolver that will save
|
|
|
|
// the DNS lookup results into this Saver.
|
|
|
|
//
|
|
|
|
// When this function is invoked on a nil Saver, it will directly return
|
|
|
|
// the original Resolver without any wrapping.
|
|
|
|
func (s *Saver) WrapResolver(r model.Resolver) model.Resolver {
|
|
|
|
if s == nil {
|
|
|
|
return r
|
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
return &ResolverSaver{
|
2022-06-01 07:44:54 +02:00
|
|
|
Resolver: r,
|
|
|
|
Saver: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// LookupHost implements Resolver.LookupHost
|
2022-06-01 23:15:47 +02:00
|
|
|
func (r *ResolverSaver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
2022-06-01 14:32:16 +02:00
|
|
|
r.Saver.Write(&EventResolveStart{&EventValue{
|
2021-02-02 12:05:47 +01:00
|
|
|
Address: r.Resolver.Address(),
|
|
|
|
Hostname: hostname,
|
2022-08-27 15:47:48 +02:00
|
|
|
Proto: r.Network(),
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: start,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2021-02-02 12:05:47 +01:00
|
|
|
addrs, err := r.Resolver.LookupHost(ctx, hostname)
|
|
|
|
stop := time.Now()
|
2022-06-01 14:32:16 +02:00
|
|
|
r.Saver.Write(&EventResolveDone{&EventValue{
|
2021-02-02 12:05:47 +01:00
|
|
|
Addresses: addrs,
|
|
|
|
Address: r.Resolver.Address(),
|
|
|
|
Duration: stop.Sub(start),
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
Err: NewFailureStr(err),
|
2021-02-02 12:05:47 +01:00
|
|
|
Hostname: hostname,
|
2022-08-27 15:47:48 +02:00
|
|
|
Proto: r.Network(),
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2021-02-02 12:05:47 +01:00
|
|
|
return addrs, err
|
|
|
|
}
|
|
|
|
|
2022-08-27 15:47:48 +02:00
|
|
|
// ResolverNetworkAdaptNames makes sure we map the [netxlite.StdlibResolverGolangNetResolver] and
|
|
|
|
// [netxlite.StdlibResolverGetaddrinfo] resolver names to [netxlite.StdlibResolverSystem]. You MUST
|
|
|
|
// call this function when your resolver splits the "stdlib" resolver results into two fake AAAA
|
|
|
|
// and A queries rather than faking a single ANY query.
|
|
|
|
//
|
|
|
|
// See https://github.com/ooni/spec/pull/257 for more information.
|
|
|
|
func ResolverNetworkAdaptNames(input string) string {
|
|
|
|
switch input {
|
|
|
|
case netxlite.StdlibResolverGetaddrinfo, netxlite.StdlibResolverGolangNetResolver:
|
|
|
|
return netxlite.StdlibResolverSystem
|
|
|
|
default:
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (r *ResolverSaver) Network() string {
|
2022-08-27 15:47:48 +02:00
|
|
|
return ResolverNetworkAdaptNames(r.Resolver.Network())
|
2022-06-01 07:44:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (r *ResolverSaver) Address() string {
|
2022-06-01 07:44:54 +02:00
|
|
|
return r.Resolver.Address()
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (r *ResolverSaver) CloseIdleConnections() {
|
2022-06-01 07:44:54 +02:00
|
|
|
r.Resolver.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (r *ResolverSaver) LookupHTTPS(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
2022-06-01 07:44:54 +02:00
|
|
|
// TODO(bassosimone): we should probably implement this method
|
|
|
|
return r.Resolver.LookupHTTPS(ctx, domain)
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (r *ResolverSaver) LookupNS(ctx context.Context, domain string) ([]*net.NS, error) {
|
2022-06-01 07:44:54 +02:00
|
|
|
// TODO(bassosimone): we should probably implement this method
|
|
|
|
return r.Resolver.LookupNS(ctx, domain)
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
// DNSTransportSaver is a DNS transport that saves events.
|
|
|
|
type DNSTransportSaver struct {
|
2022-06-01 07:44:54 +02:00
|
|
|
// DNSTransport is the underlying DNS transport.
|
|
|
|
DNSTransport model.DNSTransport
|
|
|
|
|
|
|
|
// Saver saves events.
|
2022-05-31 21:53:01 +02:00
|
|
|
Saver *Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
// WrapDNSTransport wraps a model.DNSTransport with a SaverDNSTransport that
|
|
|
|
// will save the DNS round trip results into this Saver.
|
|
|
|
//
|
|
|
|
// When this function is invoked on a nil Saver, it will directly return
|
|
|
|
// the original DNSTransport without any wrapping.
|
|
|
|
func (s *Saver) WrapDNSTransport(txp model.DNSTransport) model.DNSTransport {
|
|
|
|
if s == nil {
|
|
|
|
return txp
|
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
return &DNSTransportSaver{
|
2022-06-01 07:44:54 +02:00
|
|
|
DNSTransport: txp,
|
|
|
|
Saver: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// RoundTrip implements RoundTripper.RoundTrip
|
2022-06-01 23:15:47 +02:00
|
|
|
func (txp *DNSTransportSaver) RoundTrip(
|
2022-05-25 17:03:58 +02:00
|
|
|
ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
2022-06-01 14:32:16 +02:00
|
|
|
txp.Saver.Write(&EventDNSRoundTripStart{&EventValue{
|
2022-06-01 07:44:54 +02:00
|
|
|
Address: txp.DNSTransport.Address(),
|
|
|
|
DNSQuery: dnsMaybeQueryBytes(query),
|
2022-08-27 15:47:48 +02:00
|
|
|
Proto: txp.Network(),
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: start,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2022-05-25 17:03:58 +02:00
|
|
|
response, err := txp.DNSTransport.RoundTrip(ctx, query)
|
2021-02-02 12:05:47 +01:00
|
|
|
stop := time.Now()
|
2022-06-01 14:32:16 +02:00
|
|
|
txp.Saver.Write(&EventDNSRoundTripDone{&EventValue{
|
2022-06-01 15:20:28 +02:00
|
|
|
Address: txp.DNSTransport.Address(),
|
|
|
|
DNSQuery: dnsMaybeQueryBytes(query),
|
|
|
|
DNSResponse: dnsMaybeResponseBytes(response),
|
|
|
|
Duration: stop.Sub(start),
|
refactor(tracex): internally represent errors as strings (#786)
There are two reasons why this is beneficial:
1. github.com/google/go-cmp is more annoying to use for comparing
data structures when there are interfaces to compare. Sure, there's
a recipe for teaching it to compare errors, but how about making
the errors trivially comparable instead?
2. if we want to send errors over the network, JSON serialization
works but we cannot unmarshal the resulting string back to an error,
so how about making this representation trivial to serialize (we
are not going this now, but we need this property for websteps and
it may be sensible to try to avoid to have duplicate code because
of that -- measurex currently duplicates many tracex functionality
and this is quite unfortunate because it slows development down)
Additionally, if an error is a string:
3. we can very easily use a switch for comparing its possible
values with "" representing the absence of errors, while it is
more complex to do the same when using a nullable string or even
an error (i.e., an interface)
4. if a type is not nullable, it's easier to write safe code for
it and we may want to refactor experiments to use the internal
representation of measurements for more robust processing code
For all these reasons, let's internally use strings in tracex.
The overall aim here is to reduce the duplicated code between pre
and post-measurex measurements (see https://github.com/ooni/probe/issues/2035).
2022-06-02 10:37:07 +02:00
|
|
|
Err: NewFailureStr(err),
|
2022-08-27 15:47:48 +02:00
|
|
|
Proto: txp.Network(),
|
2022-06-01 15:20:28 +02:00
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2022-05-25 17:03:58 +02:00
|
|
|
return response, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (txp *DNSTransportSaver) Network() string {
|
2022-08-27 15:47:48 +02:00
|
|
|
return ResolverNetworkAdaptNames(txp.DNSTransport.Network())
|
2022-06-01 07:44:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (txp *DNSTransportSaver) Address() string {
|
2022-06-01 07:44:54 +02:00
|
|
|
return txp.DNSTransport.Address()
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (txp *DNSTransportSaver) CloseIdleConnections() {
|
2022-06-01 07:44:54 +02:00
|
|
|
txp.DNSTransport.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (txp *DNSTransportSaver) RequiresPadding() bool {
|
2022-06-01 07:44:54 +02:00
|
|
|
return txp.DNSTransport.RequiresPadding()
|
|
|
|
}
|
|
|
|
|
|
|
|
func dnsMaybeQueryBytes(query model.DNSQuery) []byte {
|
2022-05-25 17:03:58 +02:00
|
|
|
data, _ := query.Bytes()
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
func dnsMaybeResponseBytes(response model.DNSResponse) []byte {
|
2022-05-25 17:03:58 +02:00
|
|
|
if response == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return response.Bytes()
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
var _ model.Resolver = &ResolverSaver{}
|
|
|
|
var _ model.DNSTransport = &DNSTransportSaver{}
|