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"
|
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,
|
|
|
|
Proto: r.Resolver.Network(),
|
|
|
|
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),
|
|
|
|
Err: err,
|
|
|
|
Hostname: hostname,
|
|
|
|
Proto: r.Resolver.Network(),
|
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2021-02-02 12:05:47 +01:00
|
|
|
return addrs, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (r *ResolverSaver) Network() string {
|
2022-06-01 07:44:54 +02:00
|
|
|
return r.Resolver.Network()
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
Proto: txp.DNSTransport.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),
|
|
|
|
Err: err,
|
|
|
|
Proto: txp.DNSTransport.Network(),
|
|
|
|
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-06-01 07:44:54 +02:00
|
|
|
return txp.DNSTransport.Network()
|
|
|
|
}
|
|
|
|
|
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{}
|