* 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
139 lines
4.0 KiB
Go
139 lines
4.0 KiB
Go
package internal
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/apex/log"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/experiment/webconnectivity"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx"
|
|
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
|
"github.com/ooni/probe-cli/v3/internal/version"
|
|
)
|
|
|
|
type (
|
|
// CtrlResponse is the type of response returned by the test helper.
|
|
CtrlResponse = webconnectivity.ControlResponse
|
|
|
|
// ctrlRequest is the type of the request sent to the test helper.
|
|
ctrlRequest = webconnectivity.ControlRequest
|
|
)
|
|
|
|
// The following errors may be returned by this implementation.
|
|
var (
|
|
ErrHTTPStatusCode = errors.New("oohelper: http status code indicates failure")
|
|
ErrUnsupportedURLScheme = errors.New("oohelper: unsupported URL scheme")
|
|
ErrUnsupportedExplicitPort = errors.New("oohelper: unsupported explicit port")
|
|
ErrEmptyURL = errors.New("oohelper: empty server and/or target URL")
|
|
ErrInvalidURL = errors.New("oohelper: cannot parse URL")
|
|
ErrCannotCreateRequest = errors.New("oohelper: cannot create HTTP request")
|
|
ErrCannotParseJSONReply = errors.New("oohelper: cannot parse JSON reply")
|
|
)
|
|
|
|
// OOClient is a client for the OONI Web Connectivity test helper.
|
|
type OOClient struct {
|
|
// HTTPClient is the HTTP client to use.
|
|
HTTPClient *http.Client
|
|
|
|
// Resolver is the resolver to user.
|
|
Resolver netx.Resolver
|
|
}
|
|
|
|
// OOConfig contains configuration for the client.
|
|
type OOConfig struct {
|
|
// ServerURL is the URL of the test helper server.
|
|
ServerURL string
|
|
|
|
// TargetURL is the URL that we want to measure.
|
|
TargetURL string
|
|
}
|
|
|
|
// MakeTCPEndpoints constructs the list of TCP endpoints to send
|
|
// to the Web Connectivity test helper.
|
|
func MakeTCPEndpoints(URL *url.URL, addrs []string) ([]string, error) {
|
|
var (
|
|
port string
|
|
out []string
|
|
)
|
|
if URL.Host != URL.Hostname() {
|
|
return nil, ErrUnsupportedExplicitPort
|
|
}
|
|
switch URL.Scheme {
|
|
case "https":
|
|
port = "443"
|
|
case "http":
|
|
port = "80"
|
|
default:
|
|
return nil, ErrUnsupportedURLScheme
|
|
}
|
|
for _, addr := range addrs {
|
|
out = append(out, net.JoinHostPort(addr, port))
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Do sends a measurement request to the Web Connectivity test
|
|
// helper and receives the corresponding response.
|
|
func (oo OOClient) Do(ctx context.Context, config OOConfig) (*CtrlResponse, error) {
|
|
if config.TargetURL == "" || config.ServerURL == "" {
|
|
return nil, ErrEmptyURL
|
|
}
|
|
targetURL, err := url.Parse(config.TargetURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", ErrInvalidURL, err.Error())
|
|
}
|
|
addrs, err := oo.Resolver.LookupHost(ctx, targetURL.Hostname())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
endpoints, err := MakeTCPEndpoints(targetURL, addrs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
creq := ctrlRequest{
|
|
HTTPRequest: config.TargetURL,
|
|
HTTPRequestHeaders: map[string][]string{
|
|
"Accept": {httpheader.Accept()},
|
|
"Accept-Language": {httpheader.AcceptLanguage()},
|
|
"User-Agent": {httpheader.UserAgent()},
|
|
},
|
|
TCPConnect: endpoints,
|
|
}
|
|
data, err := json.Marshal(creq)
|
|
runtimex.PanicOnError(err, "oohelper: cannot marshal control request")
|
|
log.Debugf("out: %s", string(data))
|
|
req, err := http.NewRequestWithContext(ctx, "POST", config.ServerURL, bytes.NewReader(data))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: %s", ErrCannotCreateRequest, err.Error())
|
|
}
|
|
req.Header.Add("user-agent", fmt.Sprintf(
|
|
"oohelper/%s ooniprobe-engine/%s", version.Version, version.Version,
|
|
))
|
|
req.Header.Add("content-type", "application/json")
|
|
resp, err := oo.HTTPClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != 200 {
|
|
return nil, ErrHTTPStatusCode
|
|
}
|
|
data, err = ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var cresp CtrlResponse
|
|
if err := json.Unmarshal(data, &cresp); err != nil {
|
|
return nil, fmt.Errorf("%w: %s", ErrCannotParseJSONReply, err.Error())
|
|
}
|
|
return &cresp, nil
|
|
}
|