83e3167ce2
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).
162 lines
4.1 KiB
Go
162 lines
4.1 KiB
Go
package tracex
|
|
|
|
//
|
|
// DNS lookup and round trip
|
|
//
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
)
|
|
|
|
// ResolverSaver is a resolver that saves events.
|
|
type ResolverSaver struct {
|
|
// Resolver is the underlying resolver.
|
|
Resolver model.Resolver
|
|
|
|
// Saver saves events.
|
|
Saver *Saver
|
|
}
|
|
|
|
// 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
|
|
}
|
|
return &ResolverSaver{
|
|
Resolver: r,
|
|
Saver: s,
|
|
}
|
|
}
|
|
|
|
// LookupHost implements Resolver.LookupHost
|
|
func (r *ResolverSaver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
|
start := time.Now()
|
|
r.Saver.Write(&EventResolveStart{&EventValue{
|
|
Address: r.Resolver.Address(),
|
|
Hostname: hostname,
|
|
Proto: r.Resolver.Network(),
|
|
Time: start,
|
|
}})
|
|
addrs, err := r.Resolver.LookupHost(ctx, hostname)
|
|
stop := time.Now()
|
|
r.Saver.Write(&EventResolveDone{&EventValue{
|
|
Addresses: addrs,
|
|
Address: r.Resolver.Address(),
|
|
Duration: stop.Sub(start),
|
|
Err: NewFailureStr(err),
|
|
Hostname: hostname,
|
|
Proto: r.Resolver.Network(),
|
|
Time: stop,
|
|
}})
|
|
return addrs, err
|
|
}
|
|
|
|
func (r *ResolverSaver) Network() string {
|
|
return r.Resolver.Network()
|
|
}
|
|
|
|
func (r *ResolverSaver) Address() string {
|
|
return r.Resolver.Address()
|
|
}
|
|
|
|
func (r *ResolverSaver) CloseIdleConnections() {
|
|
r.Resolver.CloseIdleConnections()
|
|
}
|
|
|
|
func (r *ResolverSaver) LookupHTTPS(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
|
// TODO(bassosimone): we should probably implement this method
|
|
return r.Resolver.LookupHTTPS(ctx, domain)
|
|
}
|
|
|
|
func (r *ResolverSaver) LookupNS(ctx context.Context, domain string) ([]*net.NS, error) {
|
|
// TODO(bassosimone): we should probably implement this method
|
|
return r.Resolver.LookupNS(ctx, domain)
|
|
}
|
|
|
|
// DNSTransportSaver is a DNS transport that saves events.
|
|
type DNSTransportSaver struct {
|
|
// DNSTransport is the underlying DNS transport.
|
|
DNSTransport model.DNSTransport
|
|
|
|
// Saver saves events.
|
|
Saver *Saver
|
|
}
|
|
|
|
// 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
|
|
}
|
|
return &DNSTransportSaver{
|
|
DNSTransport: txp,
|
|
Saver: s,
|
|
}
|
|
}
|
|
|
|
// RoundTrip implements RoundTripper.RoundTrip
|
|
func (txp *DNSTransportSaver) RoundTrip(
|
|
ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
start := time.Now()
|
|
txp.Saver.Write(&EventDNSRoundTripStart{&EventValue{
|
|
Address: txp.DNSTransport.Address(),
|
|
DNSQuery: dnsMaybeQueryBytes(query),
|
|
Proto: txp.DNSTransport.Network(),
|
|
Time: start,
|
|
}})
|
|
response, err := txp.DNSTransport.RoundTrip(ctx, query)
|
|
stop := time.Now()
|
|
txp.Saver.Write(&EventDNSRoundTripDone{&EventValue{
|
|
Address: txp.DNSTransport.Address(),
|
|
DNSQuery: dnsMaybeQueryBytes(query),
|
|
DNSResponse: dnsMaybeResponseBytes(response),
|
|
Duration: stop.Sub(start),
|
|
Err: NewFailureStr(err),
|
|
Proto: txp.DNSTransport.Network(),
|
|
Time: stop,
|
|
}})
|
|
return response, err
|
|
}
|
|
|
|
func (txp *DNSTransportSaver) Network() string {
|
|
return txp.DNSTransport.Network()
|
|
}
|
|
|
|
func (txp *DNSTransportSaver) Address() string {
|
|
return txp.DNSTransport.Address()
|
|
}
|
|
|
|
func (txp *DNSTransportSaver) CloseIdleConnections() {
|
|
txp.DNSTransport.CloseIdleConnections()
|
|
}
|
|
|
|
func (txp *DNSTransportSaver) RequiresPadding() bool {
|
|
return txp.DNSTransport.RequiresPadding()
|
|
}
|
|
|
|
func dnsMaybeQueryBytes(query model.DNSQuery) []byte {
|
|
data, _ := query.Bytes()
|
|
return data
|
|
}
|
|
|
|
func dnsMaybeResponseBytes(response model.DNSResponse) []byte {
|
|
if response == nil {
|
|
return nil
|
|
}
|
|
return response.Bytes()
|
|
}
|
|
|
|
var _ model.Resolver = &ResolverSaver{}
|
|
var _ model.DNSTransport = &DNSTransportSaver{}
|