ooni-probe-cli/nettests/nettests.go

139 lines
3.2 KiB
Go
Raw Normal View History

2018-02-13 10:48:46 +01:00
package nettests
import (
"github.com/apex/log"
"github.com/measurement-kit/go-measurement-kit"
ooni "github.com/openobservatory/gooni"
2018-02-13 16:16:23 +01:00
"github.com/openobservatory/gooni/internal/cli/version"
"github.com/openobservatory/gooni/internal/database"
2018-02-13 10:48:46 +01:00
)
// Nettest interface. Every Nettest should implement this.
type Nettest interface {
Run(*Controller) error
2018-02-13 16:16:23 +01:00
Summary(map[string]interface{}) interface{}
2018-02-13 10:48:46 +01:00
LogSummary(string) error
}
// NettestGroup base structure
type NettestGroup struct {
Label string
Nettests []Nettest
Summary func(s string) string
}
2018-02-13 16:16:23 +01:00
// NewController creates a nettest controller
func NewController(ctx *ooni.Context, res *database.Result, msmtPath string) *Controller {
2018-02-13 10:48:46 +01:00
return &Controller{
ctx,
res,
msmtPath,
2018-02-13 10:48:46 +01:00
}
}
2018-02-13 16:16:23 +01:00
// Controller is passed to the run method of every Nettest
// each nettest instance has one controller
2018-02-13 16:16:23 +01:00
type Controller struct {
Ctx *ooni.Context
res *database.Result
msmtPath string
2018-02-13 16:16:23 +01:00
}
2018-02-13 10:48:46 +01:00
// Init should be called once to initialise the nettest
func (c *Controller) Init(nt *mk.Nettest) error {
2018-03-08 11:53:04 +01:00
log.Debugf("Init: %v", nt)
log.Debugf("OutputPath: %s", c.msmtPath)
2018-02-13 16:16:23 +01:00
nt.Options = mk.NettestOptions{
2018-03-14 14:44:37 +01:00
IncludeIP: c.Ctx.Config.Sharing.IncludeIP,
IncludeASN: c.Ctx.Config.Sharing.IncludeASN,
IncludeCountry: c.Ctx.Config.Advanced.IncludeCountry,
2018-02-13 16:16:23 +01:00
DisableCollector: false,
SoftwareName: "ooniprobe",
SoftwareVersion: version.Version,
// XXX
GeoIPCountryPath: "",
2018-03-08 13:46:21 +01:00
GeoIPASNPath: "",
OutputPath: c.msmtPath,
2018-03-08 13:46:21 +01:00
CaBundlePath: "/etc/ssl/cert.pem",
2018-02-13 16:16:23 +01:00
}
2018-03-08 13:46:21 +01:00
nt.On("log", func(e mk.Event) {
level := e.Value["verbosity"].(string)
msg := e.Value["message"].(string)
switch level {
case "ERROR":
log.Error(msg)
case "INFO":
log.Info(msg)
default:
log.Debug(msg)
}
2018-03-08 13:46:21 +01:00
})
nt.On("status.queued", func(e mk.Event) {
log.Debugf("%s", e.Key)
})
nt.On("status.started", func(e mk.Event) {
log.Debugf("%s", e.Key)
2018-02-13 16:16:23 +01:00
})
2018-03-08 13:46:21 +01:00
nt.On("status.report_created", func(e mk.Event) {
log.Debugf("%s", e.Key)
})
nt.On("status.geoip_lookup", func(e mk.Event) {
log.Debugf("%s", e.Key)
})
nt.On("status.progress", func(e mk.Event) {
perc := e.Value["percentage"].(float64)
msg := e.Value["message"].(string)
c.OnProgress(perc, msg)
})
nt.On("status.update.*", func(e mk.Event) {
log.Debugf("%s", e.Key)
})
nt.On("failure.measurement", func(e mk.Event) {
log.Debugf("%s", e.Key)
})
nt.On("failure.report_submission", func(e mk.Event) {
log.Debugf("%s", e.Key)
})
nt.On("measurement", func(e mk.Event) {
c.OnEntry(e.Value["json_str"].(string))
})
nt.On("end", func(e mk.Event) {
c.OnEntry(e.Value["json_str"].(string))
})
return nil
2018-02-13 10:48:46 +01:00
}
// OnProgress should be called when a new progress event is available.
2018-03-08 13:46:21 +01:00
func (c *Controller) OnProgress(perc float64, msg string) {
2018-02-13 10:48:46 +01:00
log.Debugf("OnProgress: %f - %s", perc, msg)
}
// OnEntry should be called every time there is a new entry
2018-03-08 13:46:21 +01:00
func (c *Controller) OnEntry(jsonStr string) {
log.Debugf("OnEntry: %s", jsonStr)
2018-02-13 10:48:46 +01:00
}
// MKStart is the interface for the mk.Nettest Start() function
type MKStart func(name string) (chan bool, error)
// Start should be called to start the test
2018-02-13 10:48:46 +01:00
func (c *Controller) Start(f MKStart) {
log.Debugf("MKStart: %s", f)
}