2022-06-08 22:01:51 +02:00
|
|
|
package sessionresolver
|
|
|
|
|
2021-03-03 11:28:39 +01:00
|
|
|
//
|
2022-06-08 22:01:51 +02:00
|
|
|
// Implementation of Resolver
|
2021-03-25 15:18:29 +01:00
|
|
|
//
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
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"
|
2022-05-16 10:46:53 +02:00
|
|
|
"net"
|
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-06-22 13:00:29 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/bytecounter"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/multierror"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-03-25 15:18:29 +01:00
|
|
|
// Resolver is the session resolver. Resolver will try to use
|
|
|
|
// a bunch of DoT/DoH resolvers before falling back to the
|
|
|
|
// system resolver. The relative priorities of the resolver
|
|
|
|
// are stored onto the KVStore such that we can remember them
|
|
|
|
// and therefore we can generally give preference to underlying
|
|
|
|
// DoT/DoH resolvers that work better.
|
|
|
|
//
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
// Make sure you fill the mandatory fields (indicated below)
|
|
|
|
// before using this data structure.
|
|
|
|
//
|
2021-03-25 15:18:29 +01:00
|
|
|
// You MUST NOT modify public fields of this structure once it
|
|
|
|
// has been created, because that MAY lead to data races.
|
2021-02-02 12:05:47 +01:00
|
|
|
type Resolver struct {
|
2022-06-08 22:01:51 +02:00
|
|
|
// ByteCounter is the OPTIONAL byte counter. It will count
|
2021-03-25 15:18:29 +01:00
|
|
|
// the bytes used by any child resolver except for the
|
|
|
|
// system resolver, whose bytes ARE NOT counted. If this
|
|
|
|
// field is not set, then we won't count the bytes.
|
|
|
|
ByteCounter *bytecounter.Counter
|
|
|
|
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
// KVStore is the MANDATORY key-value store where you
|
2021-03-25 15:18:29 +01:00
|
|
|
// want us to write statistics about which resolver is
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
// working better in your network.
|
2022-01-03 13:53:23 +01:00
|
|
|
KVStore model.KeyValueStore
|
2021-03-25 15:18:29 +01:00
|
|
|
|
2022-06-08 22:01:51 +02:00
|
|
|
// Logger is the OPTIONAL logger you want us to use
|
2021-03-25 15:18:29 +01:00
|
|
|
// to emit log messages.
|
2022-01-03 13:53:23 +01:00
|
|
|
Logger model.Logger
|
2021-03-25 15:18:29 +01:00
|
|
|
|
2022-06-08 22:01:51 +02:00
|
|
|
// ProxyURL is the OPTIONAL URL of the socks5 proxy
|
2021-03-25 15:18:29 +01:00
|
|
|
// we should be using. If not set, then we WON'T use
|
|
|
|
// any proxy. If set, then we WON'T use any http3
|
|
|
|
// based resolvers and we WON'T use the system resolver.
|
|
|
|
ProxyURL *url.URL
|
|
|
|
|
2022-06-08 22:01:51 +02:00
|
|
|
// jsonCodec is the OPTIONAL JSON Codec to use. If not set,
|
|
|
|
// we will construct a default codec.
|
|
|
|
jsonCodec jsonCodec
|
2021-03-25 15:18:29 +01:00
|
|
|
|
|
|
|
// mu provides synchronisation of internal fields.
|
|
|
|
mu sync.Mutex
|
|
|
|
|
2022-06-08 22:29:01 +02:00
|
|
|
// newChildResolverFn is the OPTIONAL function to override
|
|
|
|
// the construction of a new resolver in unit tests
|
|
|
|
newChildResolverFn func(h3 bool, URL string) (model.Resolver, error)
|
|
|
|
|
2021-03-25 15:18:29 +01:00
|
|
|
// once ensures that CloseIdleConnection is
|
|
|
|
// run just once.
|
|
|
|
once sync.Once
|
|
|
|
|
|
|
|
// res maps a URL to a child resolver. We will
|
|
|
|
// construct child resolvers just once and we
|
|
|
|
// will track them into this field.
|
2022-06-08 14:06:22 +02:00
|
|
|
res map[string]model.Resolver
|
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
|
|
|
}
|
|
|
|
|
2022-06-08 22:01:51 +02:00
|
|
|
// errLookupNotImplemented indicates a given lookup type is not implemented.
|
|
|
|
var errLookupNotImplemented = errors.New("sessionresolver: lookup not implemented")
|
2022-05-16 10:46:53 +02:00
|
|
|
|
2022-01-07 18:33:37 +01:00
|
|
|
// LookupHTTPS implements Resolver.LookupHTTPS.
|
|
|
|
func (r *Resolver) LookupHTTPS(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
2022-06-08 22:01:51 +02:00
|
|
|
return nil, errLookupNotImplemented
|
2022-05-16 10:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LookupNS implements Resolver.LookupNS.
|
|
|
|
func (r *Resolver) LookupNS(ctx context.Context, domain string) ([]*net.NS, error) {
|
2022-06-08 22:01:51 +02:00
|
|
|
return nil, errLookupNotImplemented
|
2022-01-07 18:33:37 +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-25 15:18:29 +01:00
|
|
|
if r.ProxyURL != nil && r.shouldSkipWithProxy(e) {
|
|
|
|
r.logger().Infof("sessionresolver: skipping with proxy: %+v", e)
|
2021-03-10 10:39:57 +01:00
|
|
|
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
|
|
|
}
|
2022-06-08 22:01:51 +02:00
|
|
|
me.Add(newErrWrapper(err, e.URL))
|
2021-03-03 11:28:39 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2022-06-08 14:06:22 +02:00
|
|
|
addrs, err := timeLimitedLookup(ctx, re, hostname)
|
2021-03-03 11:28:39 +01:00
|
|
|
if err == nil {
|
2022-03-08 11:59:44 +01:00
|
|
|
r.logger().Infof("sessionresolver: %s... %v", ri.URL, model.ErrorToStringOrOK(nil))
|
2021-03-03 11:28:39 +01:00
|
|
|
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 ""
|
|
|
|
}
|