2021-02-02 12:05:47 +01:00
|
|
|
package webconnectivity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-03-08 12:05:43 +01:00
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2021-03-23 16:46:46 +01:00
|
|
|
"time"
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/experiment/urlgetter"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// HTTPGetConfig contains the config for HTTPGet
|
|
|
|
type HTTPGetConfig struct {
|
|
|
|
Addresses []string
|
2021-03-23 16:46:46 +01:00
|
|
|
Begin time.Time
|
2021-02-02 12:05:47 +01:00
|
|
|
Session model.ExperimentSession
|
|
|
|
TargetURL *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(bassosimone): we should normalize the timings
|
|
|
|
|
|
|
|
// HTTPGetResult contains the results of HTTPGet
|
|
|
|
type HTTPGetResult struct {
|
|
|
|
TestKeys urlgetter.TestKeys
|
|
|
|
Failure *string
|
|
|
|
}
|
|
|
|
|
2021-03-08 12:05:43 +01:00
|
|
|
// TODO(bassosimone): Web Connectivity uses too much external testing
|
|
|
|
// and we should actually expose much less to the outside by using
|
|
|
|
// internal testing and by making _many_ functions private.
|
|
|
|
|
|
|
|
// HTTPGetMakeDNSCache constructs the DNSCache option for HTTPGet
|
|
|
|
// by combining domain and addresses into a single string. As a
|
|
|
|
// corner case, if the domain is an IP address, we return an empty
|
|
|
|
// string. This corner case corresponds to Web Connectivity
|
|
|
|
// inputs like https://1.1.1.1.
|
|
|
|
func HTTPGetMakeDNSCache(domain, addresses string) string {
|
|
|
|
if net.ParseIP(domain) != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s", domain, addresses)
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// HTTPGet performs the HTTP/HTTPS part of Web Connectivity.
|
|
|
|
func HTTPGet(ctx context.Context, config HTTPGetConfig) (out HTTPGetResult) {
|
|
|
|
addresses := strings.Join(config.Addresses, " ")
|
|
|
|
if addresses == "" {
|
|
|
|
// TODO(bassosimone): what to do in this case? We clearly
|
|
|
|
// cannot fill the DNS cache...
|
|
|
|
return
|
|
|
|
}
|
|
|
|
target := config.TargetURL.String()
|
|
|
|
config.Session.Logger().Infof("GET %s...", target)
|
|
|
|
domain := config.TargetURL.Hostname()
|
|
|
|
result, err := urlgetter.Getter{
|
2021-03-23 16:46:46 +01:00
|
|
|
Begin: config.Begin,
|
2021-02-02 12:05:47 +01:00
|
|
|
Config: urlgetter.Config{
|
2021-03-08 12:05:43 +01:00
|
|
|
DNSCache: HTTPGetMakeDNSCache(domain, addresses),
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
|
|
|
Session: config.Session,
|
|
|
|
Target: target,
|
|
|
|
}.Get(ctx)
|
2022-03-08 11:59:44 +01:00
|
|
|
config.Session.Logger().Infof("GET %s... %+v", target, model.ErrorToStringOrOK(err))
|
2021-02-02 12:05:47 +01:00
|
|
|
out.Failure = result.Failure
|
|
|
|
out.TestKeys = result
|
|
|
|
return
|
|
|
|
}
|