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
84
internal/engine/experiment/webconnectivity/control.go
Normal file
84
internal/engine/experiment/webconnectivity/control.go
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
package webconnectivity
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/internal/httpx"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
|
||||
)
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// 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) {
|
||||
clnt := httpx.Client{
|
||||
BaseURL: thAddr,
|
||||
HTTPClient: sess.DefaultHTTPClient(),
|
||||
Logger: sess.Logger(),
|
||||
}
|
||||
sess.Logger().Infof("control %s...", creq.HTTPRequest)
|
||||
// make sure error is wrapped
|
||||
err = errorx.SafeErrWrapperBuilder{
|
||||
Error: clnt.PostJSON(ctx, "/", creq, &out),
|
||||
Operation: errorx.TopLevelOperation,
|
||||
}.MaybeBuild()
|
||||
sess.Logger().Infof("control %s... %+v", creq.HTTPRequest, err)
|
||||
(&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.
|
||||
asn, _, _ := geolocate.LookupASN(sess.ASNDatabasePath(), ip)
|
||||
dns.ASNs = append(dns.ASNs, int64(asn))
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue