ooni-probe-cli/nettests/websites/web_connectivity.go

120 lines
2.5 KiB
Go
Raw Normal View History

2018-02-13 10:48:46 +01:00
package websites
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
2018-02-13 16:16:23 +01:00
"github.com/measurement-kit/go-measurement-kit"
2018-02-13 10:48:46 +01:00
"github.com/openobservatory/gooni/nettests"
"github.com/pkg/errors"
2018-02-13 10:48:46 +01:00
)
// URLInfo contains the URL and the citizenlab category code for that URL
type URLInfo struct {
URL string `json:"url"`
CategoryCode string `json:"category_code"`
}
// URLResponse is the orchestrate url response containing a list of URLs
type URLResponse struct {
Results []URLInfo `json:"results"`
}
const orchestrateBaseURL = "https://events.proteus.test.ooni.io"
func lookupURLs(ctl *nettests.Controller) ([]string, error) {
var (
parsed = new(URLResponse)
urls []string
)
reqURL := fmt.Sprintf("%s/api/v1/urls?probe_cc=%s",
orchestrateBaseURL,
ctl.Ctx.Location.CountryCode)
resp, err := http.Get(reqURL)
if err != nil {
return urls, errors.Wrap(err, "failed to perform request")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return urls, errors.Wrap(err, "failed to read response body")
}
err = json.Unmarshal([]byte(body), &parsed)
if err != nil {
return urls, errors.Wrap(err, "failed to parse json")
}
for _, url := range parsed.Results {
urls = append(urls, url.URL)
}
return urls, nil
}
2018-02-13 10:48:46 +01:00
// WebConnectivity test implementation
type WebConnectivity struct {
}
// Run starts the test
func (n WebConnectivity) Run(ctl *nettests.Controller) error {
nt := mk.NewNettest("WebConnectivity")
ctl.Init(nt)
urls, err := lookupURLs(ctl)
if err != nil {
return err
}
nt.Options.Inputs = urls
2018-02-13 16:16:23 +01:00
return nt.Run()
2018-02-13 10:48:46 +01:00
}
// WebConnectivitySummary for the test
type WebConnectivitySummary struct {
Accessible bool
Blocking string
Blocked bool
}
2018-02-13 10:48:46 +01:00
// Summary generates a summary for a test run
2018-02-13 16:16:23 +01:00
func (n WebConnectivity) Summary(tk map[string]interface{}) interface{} {
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 WebConnectivitySummary{
Accessible: accessible,
Blocking: blocking,
Blocked: blocked,
}
2018-02-13 10:48:46 +01:00
}
// LogSummary writes the summary to the standard output
func (n WebConnectivity) LogSummary(s string) error {
return nil
}