ooni-probe-cli/nettests/web_connectivity.go
Simone Basso 03e7d2ccac
circumvention: add the tor experiment (#100)
* go.mod go.sum: update all non-probe-engine deps

For each line in the go.mod, run `go get -u -v $package` if the
line is not an indirect dependency and is not probe-engine.

Upgrading probe-engine is going to require the same spell that
is used in probe-engine to update psiphon.

* go get -v github.com/ooni/probe-engine@v0.5.0

This just pins to the latest probe-engine but we've not manually
pinned all the other dependencieds yet.

Take care of the trivial API changes in probe-engine as well, such
that we can have a working build after this commit.

* go.mod go.sum: pin to probe-engine dependencies

Basically: remove all indirect dependencies. Merge this go.mod with
the one of probe-engine, to pin dependencies. Run `go mod tidy`.

* circumvention: add basic implementation of tor

This needs to be polished further, of course. But at least we have
now added support for running tor in the circumvention group.

* Readme.md: document how to update dependencies

* go get -v github.com/ooni/probe-engine@fcc9ee0a7afb

* go get -v github.com/ooni/probe-engine@4d254f5b2

* nettests/tor.go: implement summary test keys
2020-01-28 10:05:54 +01:00

103 lines
2.4 KiB
Go

package nettests
import (
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/database"
engine "github.com/ooni/probe-engine"
)
func lookupURLs(ctl *Controller, limit int64) ([]string, map[int64]int64, error) {
var urls []string
urlIDMap := make(map[int64]int64)
testlist, err := ctl.Ctx.Session.QueryTestListsURLs(&engine.TestListsURLsConfig{
Limit: limit,
})
if err != nil {
return nil, nil, err
}
for idx, url := range testlist.Result {
log.Debugf("Going over URL %d", idx)
urlID, err := database.CreateOrUpdateURL(
ctl.Ctx.DB, url.URL, url.CategoryCode, url.CountryCode,
)
if err != nil {
log.Error("failed to add to the URL table")
return nil, nil, err
}
log.Debugf("Mapped URL %s to idx %d and urlID %d", url.URL, idx, urlID)
urlIDMap[int64(idx)] = urlID
urls = append(urls, url.URL)
}
return urls, urlIDMap, nil
}
// WebConnectivity test implementation
type WebConnectivity struct {
}
// Run starts the test
func (n WebConnectivity) Run(ctl *Controller) error {
urls, urlIDMap, err := lookupURLs(ctl, ctl.Ctx.Config.Nettests.WebsitesURLLimit)
if err != nil {
return err
}
ctl.SetInputIdxMap(urlIDMap)
builder, err := ctl.Ctx.Session.NewExperimentBuilder(
"web_connectivity",
)
if err != nil {
return err
}
if err := builder.SetOptionString("LogLevel", "INFO"); err != nil {
return err
}
return ctl.Run(builder, urls)
}
// WebConnectivityTestKeys for the test
type WebConnectivityTestKeys struct {
Accessible bool `json:"accessible"`
Blocking string `json:"blocking"`
IsAnomaly bool `json:"-"`
}
// GetTestKeys generates a summary for a test run
func (n WebConnectivity) GetTestKeys(tk map[string]interface{}) (interface{}, error) {
var (
blocked bool
blocking string
accessible bool
)
// We need to do these complicated type assertions, because some of the fields
// are "nullable" and/or can be of different types
switch v := tk["blocking"].(type) {
case bool:
blocked = false
blocking = "none"
case string:
blocked = true
blocking = v
default:
blocked = false
blocking = "none"
}
if tk["accessible"] == nil {
accessible = false
} else {
accessible = tk["accessible"].(bool)
}
return WebConnectivityTestKeys{
Accessible: accessible,
Blocking: blocking,
IsAnomaly: blocked,
}, nil
}
// LogSummary writes the summary to the standard output
func (n WebConnectivity) LogSummary(s string) error {
return nil
}