chore: merge probe-engine into probe-cli (#201)
This is how I did it: 1. `git clone https://github.com/ooni/probe-engine internal/engine` 2. ``` (cd internal/engine && git describe --tags) v0.23.0 ``` 3. `nvim go.mod` (merging `go.mod` with `internal/engine/go.mod` 4. `rm -rf internal/.git internal/engine/go.{mod,sum}` 5. `git add internal/engine` 6. `find . -type f -name \*.go -exec sed -i 's@/ooni/probe-engine@/ooni/probe-cli/v3/internal/engine@g' {} \;` 7. `go build ./...` (passes) 8. `go test -race ./...` (temporary failure on RiseupVPN) 9. `go mod tidy` 10. this commit message Once this piece of work is done, we can build a new version of `ooniprobe` that is using `internal/engine` directly. We need to do more work to ensure all the other functionality in `probe-engine` (e.g. making mobile packages) are still WAI. Part of https://github.com/ooni/probe/issues/1335
This commit is contained in:
parent
b1ce300c8d
commit
d57c78bc71
535 changed files with 66182 additions and 23 deletions
85
internal/engine/internal/sessionresolver/sessionresolver.go
Normal file
85
internal/engine/internal/sessionresolver/sessionresolver.go
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
// Package sessionresolver contains the resolver used by the session. This
|
||||
// resolver uses Powerdns DoH by default and falls back on the system
|
||||
// provided resolver if Powerdns DoH is not working.
|
||||
package sessionresolver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/runtimex"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
||||
)
|
||||
|
||||
// Resolver is the session resolver.
|
||||
type Resolver struct {
|
||||
Primary netx.DNSClient
|
||||
PrimaryFailure *atomicx.Int64
|
||||
PrimaryQuery *atomicx.Int64
|
||||
Fallback netx.DNSClient
|
||||
FallbackFailure *atomicx.Int64
|
||||
FallbackQuery *atomicx.Int64
|
||||
}
|
||||
|
||||
// New creates a new session resolver.
|
||||
func New(config netx.Config) *Resolver {
|
||||
primary, err := netx.NewDNSClientWithOverrides(config,
|
||||
"https://cloudflare.com/dns-query", "dns.cloudflare.com", "", "")
|
||||
runtimex.PanicOnError(err, "cannot create dns over https resolver")
|
||||
fallback, err := netx.NewDNSClient(config, "system:///")
|
||||
runtimex.PanicOnError(err, "cannot create system resolver")
|
||||
return &Resolver{
|
||||
Primary: primary,
|
||||
PrimaryFailure: atomicx.NewInt64(),
|
||||
PrimaryQuery: atomicx.NewInt64(),
|
||||
Fallback: fallback,
|
||||
FallbackFailure: atomicx.NewInt64(),
|
||||
FallbackQuery: atomicx.NewInt64(),
|
||||
}
|
||||
}
|
||||
|
||||
// CloseIdleConnections closes the idle connections, if any
|
||||
func (r *Resolver) CloseIdleConnections() {
|
||||
r.Primary.CloseIdleConnections()
|
||||
r.Fallback.CloseIdleConnections()
|
||||
}
|
||||
|
||||
// Stats returns stats about the session resolver.
|
||||
func (r *Resolver) Stats() string {
|
||||
return fmt.Sprintf("sessionresolver: failure rate: primary: %d/%d; fallback: %d/%d",
|
||||
r.PrimaryFailure.Load(), r.PrimaryQuery.Load(),
|
||||
r.FallbackFailure.Load(), r.FallbackQuery.Load())
|
||||
}
|
||||
|
||||
// LookupHost implements Resolver.LookupHost
|
||||
func (r *Resolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
||||
// Algorithm similar to Firefox TRR2 mode. See:
|
||||
// https://wiki.mozilla.org/Trusted_Recursive_Resolver#DNS-over-HTTPS_Prefs_in_Firefox
|
||||
// We use a higher timeout than Firefox's timeout (1.5s) to be on the safe side
|
||||
// and therefore see to use DoH more often.
|
||||
r.PrimaryQuery.Add(1)
|
||||
trr2, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
addrs, err := r.Primary.LookupHost(trr2, hostname)
|
||||
if err != nil {
|
||||
r.PrimaryFailure.Add(1)
|
||||
r.FallbackQuery.Add(1)
|
||||
addrs, err = r.Fallback.LookupHost(ctx, hostname)
|
||||
if err != nil {
|
||||
r.FallbackFailure.Add(1)
|
||||
}
|
||||
}
|
||||
return addrs, err
|
||||
}
|
||||
|
||||
// Network implements Resolver.Network
|
||||
func (r *Resolver) Network() string {
|
||||
return "sessionresolver"
|
||||
}
|
||||
|
||||
// Address implements Resolver.Address
|
||||
func (r *Resolver) Address() string {
|
||||
return ""
|
||||
}
|
||||
Loading…
Reference in a new issue