2021-08-17 11:23:53 +02:00
|
|
|
package webconnectivity
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
|
2022-01-07 18:33:37 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-10-13 16:37:02 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2022-06-02 00:50:55 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/tracex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// newfailure is a convenience shortcut to save typing
|
2022-05-31 21:53:01 +02:00
|
|
|
var newfailure = tracex.NewFailure
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
// CtrlDNSResult is the result of the DNS check performed by
|
|
|
|
// the Web Connectivity test helper.
|
|
|
|
type CtrlDNSResult = webconnectivity.ControlDNSResult
|
|
|
|
|
|
|
|
// DNSConfig configures the DNS check.
|
|
|
|
type DNSConfig struct {
|
2022-07-05 18:41:35 +02:00
|
|
|
Domain string
|
|
|
|
NewResolver func() model.Resolver
|
|
|
|
Out chan CtrlDNSResult
|
|
|
|
Wg *sync.WaitGroup
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DNSDo performs the DNS check.
|
|
|
|
func DNSDo(ctx context.Context, config *DNSConfig) {
|
|
|
|
defer config.Wg.Done()
|
2022-07-05 18:41:35 +02:00
|
|
|
reso := config.NewResolver()
|
|
|
|
defer reso.CloseIdleConnections()
|
|
|
|
addrs, err := reso.LookupHost(ctx, config.Domain)
|
2021-09-27 08:13:30 +02:00
|
|
|
if addrs == nil {
|
|
|
|
addrs = []string{} // fix: the old test helper did that
|
|
|
|
}
|
2021-10-13 16:37:02 +02:00
|
|
|
failure := dnsMapFailure(newfailure(err))
|
|
|
|
config.Out <- CtrlDNSResult{Failure: failure, Addrs: addrs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dnsMapFailure attempts to map netxlite failures to the strings
|
|
|
|
// used by the original OONI test helper.
|
|
|
|
//
|
|
|
|
// See https://github.com/ooni/backend/blob/6ec4fda5b18/oonib/testhelpers/http_helpers.py#L430
|
|
|
|
func dnsMapFailure(failure *string) *string {
|
|
|
|
switch failure {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
switch *failure {
|
|
|
|
case netxlite.FailureDNSNXDOMAINError:
|
|
|
|
// We have a name for this string because dnsanalysis.go is
|
|
|
|
// already checking for this specific error string.
|
|
|
|
s := webconnectivity.DNSNameError
|
|
|
|
return &s
|
2021-10-15 16:20:07 +02:00
|
|
|
case netxlite.FailureDNSNoAnswer:
|
|
|
|
// In this case the legacy TH would produce an empty
|
|
|
|
// reply that is not attached to any error.
|
|
|
|
//
|
|
|
|
// See https://github.com/ooni/probe/issues/1707#issuecomment-944322725
|
|
|
|
return nil
|
|
|
|
case netxlite.FailureDNSNonRecoverableFailure,
|
2021-10-13 16:37:02 +02:00
|
|
|
netxlite.FailureDNSRefusedError,
|
|
|
|
netxlite.FailureDNSServerMisbehaving,
|
|
|
|
netxlite.FailureDNSTemporaryFailure:
|
|
|
|
s := "dns_server_failure"
|
|
|
|
return &s
|
|
|
|
default:
|
|
|
|
s := "unknown_error"
|
|
|
|
return &s
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|