Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PoC: running experiments bypassing internal/engine #1075

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions cmd/ooniprobe/internal/cli/geoip/geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output"
"github.com/ooni/probe-cli/v3/internal/model"
)

func init() {
Expand Down Expand Up @@ -37,23 +36,26 @@ func dogeoip(config dogeoipconfig) error {
return err
}

engine, err := probeCLI.NewProbeEngine(context.Background(), model.RunTypeManual)
if err != nil {
sess := probeCLI.NewSession(context.Background(), "manual")
defer sess.Close()

if err := sess.Bootstrap(context.Background()); err != nil {
log.WithError(err).Error("Failed to bootstrap the measurement session")
return err
}
defer engine.Close()

err = engine.MaybeLookupLocation()
location, err := sess.Geolocate(context.Background())
if err != nil {
log.WithError(err).Error("Failed to lookup the location of the probe")
return err
}

config.Logger.WithFields(log.Fields{
"type": "table",
"asn": engine.ProbeASNString(),
"network_name": engine.ProbeNetworkName(),
"country_code": engine.ProbeCC(),
"ip": engine.ProbeIP(),
"asn": location.ProbeASNString(),
"network_name": location.ProbeNetworkName(),
"country_code": location.ProbeCC(),
"ip": location.ProbeIP(),
}).Info("Looked up your location")

return nil
Expand Down
8 changes: 3 additions & 5 deletions cmd/ooniprobe/internal/nettests/dash.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package nettests

import "errors"

// Dash test implementation
type Dash struct {
}

// Run starts the test
func (d Dash) Run(ctl *Controller) error {
builder, err := ctl.Session.NewExperimentBuilder("dash")
if err != nil {
return err
}
return ctl.Run(builder, []string{""})
return errors.New("not implemented")
}
33 changes: 2 additions & 31 deletions cmd/ooniprobe/internal/nettests/dnscheck.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
package nettests

import (
"context"

engine "github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/model"
"errors"
)

// DNSCheck nettest implementation.
type DNSCheck struct{}

func (n DNSCheck) lookupURLs(ctl *Controller) ([]string, error) {
inputloader := &engine.InputLoader{
CheckInConfig: &model.OOAPICheckInConfig{
// not needed because we have default static input in the engine
},
ExperimentName: "dnscheck",
InputPolicy: model.InputOrStaticDefault,
Session: ctl.Session,
SourceFiles: ctl.InputFiles,
StaticInputs: ctl.Inputs,
}
testlist, err := inputloader.Load(context.Background())
if err != nil {
return nil, err
}
return ctl.BuildAndSetInputIdxMap(testlist)
}

// Run starts the nettest.
func (n DNSCheck) Run(ctl *Controller) error {
builder, err := ctl.Session.NewExperimentBuilder("dnscheck")
if err != nil {
return err
}
urls, err := n.lookupURLs(ctl)
if err != nil {
return err
}
return ctl.Run(builder, urls)
return errors.New("not implemented")
}
10 changes: 3 additions & 7 deletions cmd/ooniprobe/internal/nettests/facebook_messenger.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package nettests

import "errors"

// FacebookMessenger test implementation
type FacebookMessenger struct {
}

// Run starts the test
func (h FacebookMessenger) Run(ctl *Controller) error {
builder, err := ctl.Session.NewExperimentBuilder(
"facebook_messenger",
)
if err != nil {
return err
}
return ctl.Run(builder, []string{""})
return errors.New("not implemented")
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package nettests

import "errors"

// HTTPHeaderFieldManipulation test implementation
type HTTPHeaderFieldManipulation struct {
}

// Run starts the test
func (h HTTPHeaderFieldManipulation) Run(ctl *Controller) error {
builder, err := ctl.Session.NewExperimentBuilder(
"http_header_field_manipulation",
)
if err != nil {
return err
}
return ctl.Run(builder, []string{""})
return errors.New("not implemented")
}
10 changes: 3 additions & 7 deletions cmd/ooniprobe/internal/nettests/http_invalid_request_line.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package nettests

import "errors"

// HTTPInvalidRequestLine test implementation
type HTTPInvalidRequestLine struct {
}

// Run starts the test
func (h HTTPInvalidRequestLine) Run(ctl *Controller) error {
builder, err := ctl.Session.NewExperimentBuilder(
"http_invalid_request_line",
)
if err != nil {
return err
}
return ctl.Run(builder, []string{""})
return errors.New("not implemented")
}
9 changes: 3 additions & 6 deletions cmd/ooniprobe/internal/nettests/ndt.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package nettests

import "errors"

// NDT test implementation. We use v7 of NDT since 2020-03-12.
type NDT struct {
}

// Run starts the test
func (n NDT) Run(ctl *Controller) error {
// Since 2020-03-18 probe-engine exports v7 as "ndt".
builder, err := ctl.Session.NewExperimentBuilder("ndt")
if err != nil {
return err
}
return ctl.Run(builder, []string{""})
return errors.New("not implemented")
}
46 changes: 20 additions & 26 deletions cmd/ooniprobe/internal/nettests/nettests.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/fatih/color"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/ooni"
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/output"
engine "github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/model"
"github.com/ooni/probe-cli/v3/internal/nettests"
"github.com/pkg/errors"
)

Expand All @@ -22,7 +22,8 @@ type Nettest interface {

// NewController creates a nettest controller
func NewController(
nt Nettest, probe *ooni.Probe, res *model.DatabaseResult, sess *engine.Session) *Controller {
nt Nettest, probe *ooni.Probe, res *model.DatabaseResult,
sess *nettests.Session) *Controller {
return &Controller{
Probe: probe,
nt: nt,
Expand All @@ -34,15 +35,16 @@ func NewController(
// Controller is passed to the run method of every Nettest
// each nettest instance has one controller
type Controller struct {
Probe *ooni.Probe
Session *engine.Session
res *model.DatabaseResult
nt Nettest
ntCount int
ntIndex int
ntStartTime time.Time // used to calculate the eta
msmts map[int64]*model.DatabaseMeasurement
inputIdxMap map[int64]int64 // Used to map mk idx to database id
CheckInResult *model.OOAPICheckInResult
Probe *ooni.Probe
Session *nettests.Session
res *model.DatabaseResult
nt Nettest
ntCount int
ntIndex int
ntStartTime time.Time // used to calculate the eta
msmts map[int64]*model.DatabaseMeasurement
inputIdxMap map[int64]int64 // Used to map mk idx to database id

// InputFiles optionally contains the names of the input
// files to read inputs from (only for nettests that take
Expand Down Expand Up @@ -120,13 +122,12 @@ func (c *Controller) SetNettestIndex(i, n int) {
//
// This function will continue to run in most cases but will
// immediately halt if something's wrong with the file system.
func (c *Controller) Run(builder model.ExperimentBuilder, inputs []string) error {
func (c *Controller) Run(factory nettests.ExperimentFactory, inputs []string) error {
db := c.Probe.DB()
c.numInputs = len(inputs)
// This will configure the controller as handler for the callbacks
// called by ooni/probe-engine/experiment.Experiment.
builder.SetCallbacks(model.ExperimentCallbacks(c))
c.numInputs = len(inputs)
exp := builder.NewExperiment()
exp := factory.NewExperiment(c)
defer func() {
c.res.DataUsageDown += exp.KibiBytesReceived()
c.res.DataUsageUp += exp.KibiBytesSent()
Expand All @@ -142,14 +143,7 @@ func (c *Controller) Run(builder model.ExperimentBuilder, inputs []string) error
log.Debug(color.RedString("status.started"))

if c.Probe.Config().Sharing.UploadResults {
if err := exp.OpenReportContext(context.Background()); err != nil {
log.Debugf(
"%s: %s", color.RedString("failure.report_create"), err.Error(),
)
} else {
log.Debugf(color.RedString("status.report_create"))
reportID = sql.NullString{String: exp.ReportID(), Valid: true}
}
reportID = sql.NullString{String: exp.ReportID(), Valid: true}
}

maxRuntime := time.Duration(c.Probe.Config().Nettests.WebsitesMaxRuntime) * time.Second
Expand Down Expand Up @@ -196,7 +190,7 @@ func (c *Controller) Run(builder model.ExperimentBuilder, inputs []string) error
if input != "" {
c.OnProgress(0, fmt.Sprintf("processing input: %s", input))
}
measurement, err := exp.MeasureWithContext(context.Background(), input)
measurement, err := exp.Measure(context.Background(), input)
if err != nil {
log.WithError(err).Debug(color.RedString("failure.measurement"))
if err := db.Failed(c.msmts[idx64], err.Error()); err != nil {
Expand All @@ -217,7 +211,7 @@ func (c *Controller) Run(builder model.ExperimentBuilder, inputs []string) error
// Implementation note: SubmitMeasurement will fail here if we did fail
// to open the report but we still want to continue. There will be a
// bit of a spew in the logs, perhaps, but stopping seems less efficient.
if err := exp.SubmitAndUpdateMeasurementContext(context.Background(), measurement); err != nil {
if err := c.Session.Submit(context.Background(), measurement); err != nil {
log.Debug(color.RedString("failure.measurement_submission"))
if err := db.UploadFailed(c.msmts[idx64], err.Error()); err != nil {
return errors.Wrap(err, "failed to mark upload as failed")
Expand All @@ -231,7 +225,7 @@ func (c *Controller) Run(builder model.ExperimentBuilder, inputs []string) error
}
// We only save the measurement to disk if we failed to upload the measurement
if saveToDisk {
if err := exp.SaveMeasurement(measurement, msmt.MeasurementFilePath.String); err != nil {
if err := nettests.SaveMeasurement(measurement, msmt.MeasurementFilePath.String); err != nil {
return errors.Wrap(err, "failed to save measurement on disk")
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/ooniprobe/internal/nettests/nettests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestRun(t *testing.T) {
t.Skip("skip test in short mode")
}
probe := newOONIProbe(t)
sess, err := probe.NewSession(context.Background(), model.RunTypeManual)
sess, err := probe.NewProbeEngine(context.Background(), model.RunTypeManual)
if err != nil {
t.Fatal(err)
}
Expand Down
10 changes: 3 additions & 7 deletions cmd/ooniprobe/internal/nettests/psiphon.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package nettests

import "errors"

// Psiphon test implementation
type Psiphon struct {
}

// Run starts the test
func (h Psiphon) Run(ctl *Controller) error {
builder, err := ctl.Session.NewExperimentBuilder(
"psiphon",
)
if err != nil {
return err
}
return ctl.Run(builder, []string{""})
return errors.New("not implemented")
}
11 changes: 3 additions & 8 deletions cmd/ooniprobe/internal/nettests/riseupvpn.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package nettests

import "errors"

// RiseupVPN test implementation
type RiseupVPN struct {
}

// Run starts the test
func (h RiseupVPN) Run(ctl *Controller) error {
builder, err := ctl.Session.NewExperimentBuilder(
"riseupvpn",
)
if err != nil {
return err
}

return ctl.Run(builder, []string{""})
return errors.New("not implemented")
}
Loading