2021-02-02 12:05:47 +01:00
|
|
|
package webconnectivity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
|
2022-01-05 17:17:20 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/httpx"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-09-28 12:42:01 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ControlRequest is the request that we send to the control
|
|
|
|
type ControlRequest struct {
|
|
|
|
HTTPRequest string `json:"http_request"`
|
|
|
|
HTTPRequestHeaders map[string][]string `json:"http_request_headers"`
|
|
|
|
TCPConnect []string `json:"tcp_connect"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ControlTCPConnectResult is the result of the TCP connect
|
|
|
|
// attempt performed by the control vantage point.
|
|
|
|
type ControlTCPConnectResult struct {
|
|
|
|
Status bool `json:"status"`
|
|
|
|
Failure *string `json:"failure"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ControlHTTPRequestResult is the result of the HTTP request
|
|
|
|
// performed by the control vantage point.
|
|
|
|
type ControlHTTPRequestResult struct {
|
|
|
|
BodyLength int64 `json:"body_length"`
|
|
|
|
Failure *string `json:"failure"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Headers map[string]string `json:"headers"`
|
|
|
|
StatusCode int64 `json:"status_code"`
|
|
|
|
}
|
|
|
|
|
2022-08-28 13:49:24 +02:00
|
|
|
// TODO(bassosimone): ASNs and FillASNs are private implementation details of v0.4
|
|
|
|
// that are actually ~annoying because we are mixing the data model with fields used
|
|
|
|
// by just the v0.4 client implementation. We should avoid repeating this mistake
|
|
|
|
// when implementing v0.5 of the client.
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// ControlDNSResult is the result of the DNS lookup
|
|
|
|
// performed by the control vantage point.
|
|
|
|
type ControlDNSResult struct {
|
|
|
|
Failure *string `json:"failure"`
|
|
|
|
Addrs []string `json:"addrs"`
|
|
|
|
ASNs []int64 `json:"-"` // not visible from the JSON
|
|
|
|
}
|
|
|
|
|
2022-08-28 13:49:24 +02:00
|
|
|
// ControlIPInfo contains information about IP addresses resolved either
|
|
|
|
// by the probe or by the TH and processed by the TH.
|
|
|
|
type ControlIPInfo struct {
|
|
|
|
// ASN contains the address' AS number.
|
|
|
|
ASN int64 `json:"asn"`
|
|
|
|
|
|
|
|
// Flags contains flags describing this address.
|
|
|
|
Flags int64 `json:"flags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ControlIPInfoFlagResolvedByProbe indicates that the probe has
|
|
|
|
// resolved this IP address.
|
|
|
|
ControlIPInfoFlagResolvedByProbe = 1 << iota
|
|
|
|
|
|
|
|
// ControlIPInfoFlagResolvedByTH indicates that the test helper
|
|
|
|
// has resolved this IP address.
|
|
|
|
ControlIPInfoFlagResolvedByTH
|
|
|
|
|
|
|
|
// ControlIPInfoFlagIsBogon indicates that the address is a bogon
|
|
|
|
ControlIPInfoFlagIsBogon
|
|
|
|
)
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// ControlResponse is the response from the control service.
|
|
|
|
type ControlResponse struct {
|
|
|
|
TCPConnect map[string]ControlTCPConnectResult `json:"tcp_connect"`
|
|
|
|
HTTPRequest ControlHTTPRequestResult `json:"http_request"`
|
|
|
|
DNS ControlDNSResult `json:"dns"`
|
2022-08-28 13:49:24 +02:00
|
|
|
IPInfo map[string]*ControlIPInfo `json:"ip_info"`
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Control performs the control request and returns the response.
|
|
|
|
func Control(
|
|
|
|
ctx context.Context, sess model.ExperimentSession,
|
|
|
|
thAddr string, creq ControlRequest) (out ControlResponse, err error) {
|
2022-01-05 14:15:42 +01:00
|
|
|
clnt := &httpx.APIClientTemplate{
|
2021-02-02 12:05:47 +01:00
|
|
|
BaseURL: thAddr,
|
|
|
|
HTTPClient: sess.DefaultHTTPClient(),
|
|
|
|
Logger: sess.Logger(),
|
2021-11-26 19:20:24 +01:00
|
|
|
UserAgent: sess.UserAgent(),
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-03-08 12:05:43 +01:00
|
|
|
sess.Logger().Infof("control for %s...", creq.HTTPRequest)
|
2021-02-02 12:05:47 +01:00
|
|
|
// make sure error is wrapped
|
2022-01-07 17:31:21 +01:00
|
|
|
err = clnt.WithBodyLogging().Build().PostJSON(ctx, "/", creq, &out)
|
|
|
|
if err != nil {
|
|
|
|
err = netxlite.NewTopLevelGenericErrWrapper(err)
|
|
|
|
}
|
2022-03-08 11:59:44 +01:00
|
|
|
sess.Logger().Infof("control for %s... %+v", creq.HTTPRequest, model.ErrorToStringOrOK(err))
|
2021-02-02 12:05:47 +01:00
|
|
|
(&out.DNS).FillASNs(sess)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// FillASNs fills the ASNs array of ControlDNSResult. For each Addr inside
|
|
|
|
// of the ControlDNSResult structure, we obtain the corresponding ASN.
|
|
|
|
//
|
|
|
|
// This is very useful to know what ASNs were the IP addresses returned by
|
|
|
|
// the control according to the probe's ASN database.
|
|
|
|
func (dns *ControlDNSResult) FillASNs(sess model.ExperimentSession) {
|
|
|
|
dns.ASNs = []int64{}
|
|
|
|
for _, ip := range dns.Addrs {
|
|
|
|
// TODO(bassosimone): this would be more efficient if we'd open just
|
|
|
|
// once the database and then reuse it for every address.
|
2021-04-01 16:57:31 +02:00
|
|
|
asn, _, _ := geolocate.LookupASN(ip)
|
2021-02-02 12:05:47 +01:00
|
|
|
dns.ASNs = append(dns.ASNs, int64(asn))
|
|
|
|
}
|
|
|
|
}
|