2021-02-02 12:05:47 +01:00
|
|
|
// Package sessionresolver contains the resolver used by the session. This
|
2021-03-03 11:28:39 +01:00
|
|
|
// resolver will try to figure out which is the best service for running
|
|
|
|
// domain name resolutions and will consistently use it.
|
|
|
|
//
|
|
|
|
// Occasionally this code will also swap the best resolver with other
|
|
|
|
// ~good resolvers to give them a chance to perform.
|
|
|
|
//
|
|
|
|
// The penalty/reward mechanism is strongly derivative, so the code should
|
|
|
|
// adapt ~quickly to changing network conditions. Occasionally, we will
|
|
|
|
// have longer resolutions when trying out other resolvers.
|
|
|
|
//
|
|
|
|
// At the beginning we randomize the known resolvers so that we do not
|
|
|
|
// have any preferential ordering. The initial resolutions may be slower
|
|
|
|
// if there are many issues with resolvers.
|
|
|
|
//
|
|
|
|
// The system resolver is given the lowest priority at the beginning
|
|
|
|
// but it will of course be the most popular resolver if anything else
|
|
|
|
// is failing us. (We will still occasionally probe for other working
|
|
|
|
// resolvers and increase their score on success.)
|
2021-02-02 12:05:47 +01:00
|
|
|
package sessionresolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-03 11:28:39 +01:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-02-02 12:05:47 +01:00
|
|
|
"fmt"
|
2021-03-03 11:28:39 +01:00
|
|
|
"math/rand"
|
2021-03-10 10:39:57 +01:00
|
|
|
"net/url"
|
2021-03-03 11:28:39 +01:00
|
|
|
"sync"
|
2021-02-02 12:05:47 +01:00
|
|
|
"time"
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/internal/multierror"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
|
2021-02-04 11:00:27 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/runtimex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
// Resolver is the session resolver. You should create an instance of
|
|
|
|
// this structure and use it in session.go.
|
2021-02-02 12:05:47 +01:00
|
|
|
type Resolver struct {
|
2021-03-03 11:28:39 +01:00
|
|
|
ByteCounter *bytecounter.Counter // optional
|
|
|
|
KVStore KVStore // optional
|
|
|
|
Logger Logger // optional
|
2021-03-10 10:39:57 +01:00
|
|
|
ProxyURL *url.URL // optional
|
2021-03-03 11:28:39 +01:00
|
|
|
codec codec
|
|
|
|
dnsClientMaker dnsclientmaker
|
|
|
|
mu sync.Mutex
|
|
|
|
once sync.Once
|
|
|
|
res map[string]childResolver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
// CloseIdleConnections closes the idle connections, if any. This
|
|
|
|
// function is guaranteed to be idempotent.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (r *Resolver) CloseIdleConnections() {
|
2021-03-03 11:28:39 +01:00
|
|
|
r.once.Do(r.closeall)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stats returns stats about the session resolver.
|
|
|
|
func (r *Resolver) Stats() string {
|
2021-03-03 11:28:39 +01:00
|
|
|
data, err := json.Marshal(r.readstatedefault())
|
|
|
|
runtimex.PanicOnError(err, "json.Marshal should not fail here")
|
|
|
|
return fmt.Sprintf("sessionresolver: %s", string(data))
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
// ErrLookupHost indicates that LookupHost failed.
|
|
|
|
var ErrLookupHost = errors.New("sessionresolver: LookupHost failed")
|
|
|
|
|
|
|
|
// LookupHost implements Resolver.LookupHost. This function returns a
|
|
|
|
// multierror.Union error on failure, so you can see individual errors
|
|
|
|
// and get a better picture of what's been going wrong.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (r *Resolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
2021-03-03 11:28:39 +01:00
|
|
|
state := r.readstatedefault()
|
|
|
|
r.maybeConfusion(state, time.Now().UnixNano())
|
|
|
|
defer r.writestate(state)
|
|
|
|
me := multierror.New(ErrLookupHost)
|
|
|
|
for _, e := range state {
|
2021-03-10 10:39:57 +01:00
|
|
|
if r.shouldSkipWithProxy(e) {
|
|
|
|
continue // we cannot proxy this URL so ignore it
|
|
|
|
}
|
2021-03-03 11:28:39 +01:00
|
|
|
addrs, err := r.lookupHost(ctx, e, hostname)
|
|
|
|
if err == nil {
|
|
|
|
return addrs, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-03-03 11:28:39 +01:00
|
|
|
me.Add(&errwrapper{error: err, URL: e.URL})
|
|
|
|
}
|
|
|
|
return nil, me
|
|
|
|
}
|
|
|
|
|
2021-03-10 10:39:57 +01:00
|
|
|
func (r *Resolver) shouldSkipWithProxy(e *resolverinfo) bool {
|
|
|
|
URL, err := url.Parse(e.URL)
|
|
|
|
if err != nil {
|
|
|
|
return true // please skip
|
|
|
|
}
|
|
|
|
switch URL.Scheme {
|
|
|
|
case "https", "dot", "tcp":
|
|
|
|
return false // we can handle this
|
|
|
|
default:
|
|
|
|
return true // please skip
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
func (r *Resolver) lookupHost(ctx context.Context, ri *resolverinfo, hostname string) ([]string, error) {
|
|
|
|
const ewma = 0.9 // the last sample is very important
|
|
|
|
re, err := r.getresolver(ri.URL)
|
|
|
|
if err != nil {
|
|
|
|
r.logger().Warnf("sessionresolver: getresolver: %s", err.Error())
|
|
|
|
ri.Score = 0 // this is a hard error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addrs, err := r.timeLimitedLookup(ctx, re, hostname)
|
|
|
|
if err == nil {
|
|
|
|
r.logger().Infof("sessionresolver: %s... %v", ri.URL, nil)
|
|
|
|
ri.Score = ewma*1.0 + (1-ewma)*ri.Score // increase score
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
r.logger().Warnf("sessionresolver: %s... %s", ri.URL, err.Error())
|
|
|
|
ri.Score = ewma*0.0 + (1-ewma)*ri.Score // decrease score
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybeConfusion will rearrange the first elements of the vector
|
|
|
|
// with low probability, so giving other resolvers a chance
|
|
|
|
// to run and show that they are also viable. We do not fully
|
|
|
|
// reorder the vector because that could lead to long runtimes.
|
|
|
|
//
|
|
|
|
// The return value is only meaningful for testing.
|
|
|
|
func (r *Resolver) maybeConfusion(state []*resolverinfo, seed int64) int {
|
|
|
|
rng := rand.New(rand.NewSource(seed))
|
|
|
|
const confusion = 0.3
|
|
|
|
if rng.Float64() >= confusion {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
switch len(state) {
|
|
|
|
case 0, 1: // nothing to do
|
|
|
|
return 0
|
|
|
|
case 2:
|
|
|
|
state[0], state[1] = state[1], state[0]
|
|
|
|
return 2
|
|
|
|
default:
|
|
|
|
state[0], state[2] = state[2], state[0]
|
|
|
|
return 3
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
// Network implements Resolver.Network.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (r *Resolver) Network() string {
|
|
|
|
return "sessionresolver"
|
|
|
|
}
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
// Address implements Resolver.Address.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (r *Resolver) Address() string {
|
|
|
|
return ""
|
|
|
|
}
|