2021-02-02 12:05:47 +01:00
|
|
|
package engine
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
//
|
|
|
|
// Experiment definition and implementation.
|
|
|
|
//
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2021-10-13 10:31:46 +02:00
|
|
|
"runtime"
|
2021-02-02 12:05:47 +01:00
|
|
|
"time"
|
|
|
|
|
2021-06-22 13:00:29 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/bytecounter"
|
2021-02-02 12:05:47 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
|
2022-01-03 13:53:23 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-02-04 11:00:27 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/version"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const dateFormat = "2006-01-02 15:04:05"
|
|
|
|
|
|
|
|
func formatTimeNowUTC() string {
|
|
|
|
return time.Now().UTC().Format(dateFormat)
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// experiment implements Experiment.
|
|
|
|
type experiment struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
byteCounter *bytecounter.Counter
|
|
|
|
callbacks model.ExperimentCallbacks
|
|
|
|
measurer model.ExperimentMeasurer
|
|
|
|
report probeservices.ReportChannel
|
|
|
|
session *Session
|
|
|
|
testName string
|
|
|
|
testStartTime string
|
|
|
|
testVersion string
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// newExperiment creates a new experiment given a measurer.
|
|
|
|
func newExperiment(sess *Session, measurer model.ExperimentMeasurer) *experiment {
|
|
|
|
return &experiment{
|
2021-02-02 12:05:47 +01:00
|
|
|
byteCounter: bytecounter.New(),
|
|
|
|
callbacks: model.NewPrinterCallbacks(sess.Logger()),
|
|
|
|
measurer: measurer,
|
|
|
|
session: sess,
|
|
|
|
testName: measurer.ExperimentName(),
|
|
|
|
testStartTime: formatTimeNowUTC(),
|
|
|
|
testVersion: measurer.ExperimentVersion(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// KibiBytesReceived implements Experiment.KibiBytesReceived.
|
|
|
|
func (e *experiment) KibiBytesReceived() float64 {
|
2021-02-02 12:05:47 +01:00
|
|
|
return e.byteCounter.KibiBytesReceived()
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// KibiBytesSent implements Experiment.KibiBytesSent.
|
|
|
|
func (e *experiment) KibiBytesSent() float64 {
|
2021-02-02 12:05:47 +01:00
|
|
|
return e.byteCounter.KibiBytesSent()
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// Name implements Experiment.Name.
|
|
|
|
func (e *experiment) Name() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
return e.testName
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// GetSummaryKeys implements Experiment.GetSummaryKeys.
|
|
|
|
func (e *experiment) GetSummaryKeys(m *model.Measurement) (interface{}, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
return e.measurer.GetSummaryKeys(m)
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// ReportID implements Experiment.ReportID.
|
|
|
|
func (e *experiment) ReportID() string {
|
2021-02-02 12:05:47 +01:00
|
|
|
if e.report == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return e.report.ReportID()
|
|
|
|
}
|
|
|
|
|
2021-09-30 00:54:52 +02:00
|
|
|
// experimentAsyncWrapper makes a sync experiment behave like it was async
|
|
|
|
type experimentAsyncWrapper struct {
|
2022-07-08 12:29:23 +02:00
|
|
|
*experiment
|
2021-09-30 00:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ model.ExperimentMeasurerAsync = &experimentAsyncWrapper{}
|
|
|
|
|
|
|
|
// RunAsync implements ExperimentMeasurerAsync.RunAsync.
|
|
|
|
func (eaw *experimentAsyncWrapper) RunAsync(
|
|
|
|
ctx context.Context, sess model.ExperimentSession, input string,
|
|
|
|
callbacks model.ExperimentCallbacks) (<-chan *model.ExperimentAsyncTestKeys, error) {
|
|
|
|
out := make(chan *model.ExperimentAsyncTestKeys)
|
2022-07-08 12:29:23 +02:00
|
|
|
measurement := eaw.experiment.newMeasurement(input)
|
2021-09-30 00:54:52 +02:00
|
|
|
start := time.Now()
|
2022-07-08 12:29:23 +02:00
|
|
|
err := eaw.experiment.measurer.Run(ctx, eaw.session, measurement, eaw.callbacks)
|
2021-09-30 00:54:52 +02:00
|
|
|
stop := time.Now()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer close(out) // signal the reader we're done!
|
|
|
|
out <- &model.ExperimentAsyncTestKeys{
|
|
|
|
Extensions: measurement.Extensions,
|
2021-10-05 12:29:00 +02:00
|
|
|
Input: measurement.Input,
|
2021-09-30 00:54:52 +02:00
|
|
|
MeasurementRuntime: stop.Sub(start).Seconds(),
|
|
|
|
TestKeys: measurement.TestKeys,
|
2022-05-18 15:46:08 +02:00
|
|
|
TestHelpers: measurement.TestHelpers,
|
2021-09-30 00:54:52 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// MeasureAsync implements Experiment.MeasureAsync.
|
|
|
|
func (e *experiment) MeasureAsync(
|
2021-09-30 00:54:52 +02:00
|
|
|
ctx context.Context, input string) (<-chan *model.Measurement, error) {
|
|
|
|
err := e.session.MaybeLookupLocationContext(ctx) // this already tracks session bytes
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
feat(torsf): collect tor logs, select rendezvous method, count bytes (#683)
This diff contains significant improvements over the previous
implementation of the torsf experiment.
We add support for configuring different rendezvous methods after
the convo at https://github.com/ooni/probe/issues/2004. In doing
that, I've tried to use a terminology that is consistent with the
names being actually used by tor developers.
In terms of what to do next, this diff basically instruments
torsf to always rendezvous using domain fronting. Yet, it's also
possible to change the rendezvous method from the command line,
when using miniooni, which allows to experiment a bit more. In the
same vein, by default we use a persistent tor datadir, but it's
also possible to use a temporary datadir using the cmdline.
Here's how a generic invocation of `torsf` looks like:
```bash
./miniooni -O DisablePersistentDatadir=true \
-O RendezvousMethod=amp \
-O DisableProgress=true \
torsf
```
(The default is `DisablePersistentDatadir=false` and
`RendezvousMethod=domain_fronting`.)
With this implementation, we can start measuring whether snowflake
and tor together can boostrap, which seems the most important thing
to focus on at the beginning. Understanding why the bootstrap most
often does not converge with a temporary datadir on Android devices
remains instead an open problem for now. (I'll also update the
relevant issues or create new issues after commit this.)
We also address some methodology improvements that were proposed
in https://github.com/ooni/probe/issues/1686. Namely:
1. we record the tor version;
2. we include the bootstrap percentage by reading the logs;
3. we set the anomaly key correctly;
4. we measure the bytes send and received (by `tor` not by `snowflake`, since
doing it for snowflake seems more complex at this stage).
What remains to be done is the possibility of including Snowflake
events into the measurement, which is not possible until the new
improvements at common/event in snowflake.git are included into a
tagged version of snowflake itself. (I'll make sure to mention
this aspect to @cohosh in https://github.com/ooni/probe/issues/2004.)
2022-02-07 17:05:36 +01:00
|
|
|
ctx = bytecounter.WithSessionByteCounter(ctx, e.session.byteCounter)
|
|
|
|
ctx = bytecounter.WithExperimentByteCounter(ctx, e.byteCounter)
|
2021-09-30 00:54:52 +02:00
|
|
|
var async model.ExperimentMeasurerAsync
|
|
|
|
if v, okay := e.measurer.(model.ExperimentMeasurerAsync); okay {
|
|
|
|
async = v
|
|
|
|
} else {
|
|
|
|
async = &experimentAsyncWrapper{e}
|
|
|
|
}
|
|
|
|
in, err := async.RunAsync(ctx, e.session, input, e.callbacks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out := make(chan *model.Measurement)
|
|
|
|
go func() {
|
|
|
|
defer close(out) // we need to signal the consumer we're done
|
|
|
|
for tk := range in {
|
|
|
|
measurement := e.newMeasurement(input)
|
|
|
|
measurement.Extensions = tk.Extensions
|
2021-10-05 12:29:00 +02:00
|
|
|
measurement.Input = tk.Input
|
2021-09-30 00:54:52 +02:00
|
|
|
measurement.MeasurementRuntime = tk.MeasurementRuntime
|
2022-05-18 15:46:08 +02:00
|
|
|
measurement.TestHelpers = tk.TestHelpers
|
2021-09-30 00:54:52 +02:00
|
|
|
measurement.TestKeys = tk.TestKeys
|
2022-09-12 22:22:25 +02:00
|
|
|
if err := model.ScrubMeasurement(measurement, e.session.ProbeIP()); err != nil {
|
2021-09-30 00:54:52 +02:00
|
|
|
// If we fail to scrub the measurement then we are not going to
|
|
|
|
// submit it. Most likely causes of error here are unlikely,
|
|
|
|
// e.g., the TestKeys being not serializable.
|
|
|
|
e.session.Logger().Warnf("can't scrub measurement: %s", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
out <- measurement
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// MeasureWithContext implements Experiment.MeasureWithContext.
|
|
|
|
func (e *experiment) MeasureWithContext(
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx context.Context, input string,
|
|
|
|
) (measurement *model.Measurement, err error) {
|
2021-09-30 00:54:52 +02:00
|
|
|
out, err := e.MeasureAsync(ctx, input)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
2021-09-30 00:54:52 +02:00
|
|
|
return nil, err
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-09-30 00:54:52 +02:00
|
|
|
for m := range out {
|
|
|
|
if measurement == nil {
|
|
|
|
measurement = m // as documented just return the first one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if measurement == nil {
|
|
|
|
err = errors.New("experiment returned no measurements")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// SaveMeasurement implements Experiment.SaveMeasurement.
|
|
|
|
func (e *experiment) SaveMeasurement(measurement *model.Measurement, filePath string) error {
|
2021-02-02 12:05:47 +01:00
|
|
|
return e.saveMeasurement(
|
|
|
|
measurement, filePath, json.Marshal, os.OpenFile,
|
|
|
|
func(fp *os.File, b []byte) (int, error) {
|
|
|
|
return fp.Write(b)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// SubmitAndUpdateMeasurementContext implements Experiment.SubmitAndUpdateMeasurementContext.
|
|
|
|
func (e *experiment) SubmitAndUpdateMeasurementContext(
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx context.Context, measurement *model.Measurement) error {
|
|
|
|
if e.report == nil {
|
2021-09-30 00:54:52 +02:00
|
|
|
return errors.New("report is not open")
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
return e.report.SubmitMeasurement(ctx, measurement)
|
|
|
|
}
|
|
|
|
|
2021-09-30 00:54:52 +02:00
|
|
|
// newMeasurement creates a new measurement for this experiment with the given input.
|
2022-07-08 12:29:23 +02:00
|
|
|
func (e *experiment) newMeasurement(input string) *model.Measurement {
|
2021-02-02 12:05:47 +01:00
|
|
|
utctimenow := time.Now().UTC()
|
|
|
|
m := &model.Measurement{
|
|
|
|
DataFormatVersion: probeservices.DefaultDataFormatVersion,
|
|
|
|
Input: model.MeasurementTarget(input),
|
|
|
|
MeasurementStartTime: utctimenow.Format(dateFormat),
|
|
|
|
MeasurementStartTimeSaved: utctimenow,
|
2022-08-28 20:00:25 +02:00
|
|
|
ProbeIP: model.DefaultProbeIP,
|
2021-02-02 12:05:47 +01:00
|
|
|
ProbeASN: e.session.ProbeASNString(),
|
|
|
|
ProbeCC: e.session.ProbeCC(),
|
|
|
|
ProbeNetworkName: e.session.ProbeNetworkName(),
|
|
|
|
ReportID: e.ReportID(),
|
|
|
|
ResolverASN: e.session.ResolverASNString(),
|
|
|
|
ResolverIP: e.session.ResolverIP(),
|
|
|
|
ResolverNetworkName: e.session.ResolverNetworkName(),
|
|
|
|
SoftwareName: e.session.SoftwareName(),
|
|
|
|
SoftwareVersion: e.session.SoftwareVersion(),
|
|
|
|
TestName: e.testName,
|
|
|
|
TestStartTime: e.testStartTime,
|
|
|
|
TestVersion: e.testVersion,
|
|
|
|
}
|
|
|
|
m.AddAnnotation("engine_name", "ooniprobe-engine")
|
|
|
|
m.AddAnnotation("engine_version", version.Version)
|
refactor: flatten and separate (#353)
* refactor(atomicx): move outside the engine package
After merging probe-engine into probe-cli, my impression is that we have
too much unnecessary nesting of packages in this repository.
The idea of this commit and of a bunch of following commits will instead
be to reduce the nesting and simplify the structure.
While there, improve the documentation.
* fix: always use the atomicx package
For consistency, never use sync/atomic and always use ./internal/atomicx
so we can just grep and make sure we're not risking to crash if we make
a subtle mistake on a 32 bit platform.
While there, mention in the contributing guidelines that we want to
always prefer the ./internal/atomicx package over sync/atomic.
* fix(atomicx): remove unnecessary constructor
We don't need a constructor here. The default constructed `&Int64{}`
instance is already usable and the constructor does not add anything to
what we are doing, rather it just creates extra confusion.
* cleanup(atomicx): we are not using Float64
Because atomicx.Float64 is unused, we can safely zap it.
* cleanup(atomicx): simplify impl and improve tests
We can simplify the implementation by using defer and by letting
the Load() method call Add(0).
We can improve tests by making many goroutines updated the
atomic int64 value concurrently.
* refactor(fsx): can live in the ./internal pkg
Let us reduce the amount of nesting. While there, ensure that the
package only exports the bare minimum, and improve the documentation
of the tests, to ease reading the code.
* refactor: move runtimex to ./internal
* refactor: move shellx into the ./internal package
While there, remove unnecessary dependency between packages.
While there, specify in the contributing guidelines that
one should use x/sys/execabs instead of os/exec.
* refactor: move ooapi into the ./internal pkg
* refactor(humanize): move to ./internal and better docs
* refactor: move platform to ./internal
* refactor(randx): move to ./internal
* refactor(multierror): move into the ./internal pkg
* refactor(kvstore): all kvstores in ./internal
Rather than having part of the kvstore inside ./internal/engine/kvstore
and part in ./internal/engine/kvstore.go, let us put every piece of code
that is kvstore related into the ./internal/kvstore package.
* fix(kvstore): always return ErrNoSuchKey on Get() error
It should help to use the kvstore everywhere removing all the
copies that are lingering around the tree.
* sessionresolver: make KVStore mandatory
Simplifies implementation. While there, use the ./internal/kvstore
package rather than having our private implementation.
* fix(ooapi): use the ./internal/kvstore package
* fix(platform): better documentation
2021-06-04 10:34:18 +02:00
|
|
|
m.AddAnnotation("platform", e.session.Platform())
|
2021-10-13 10:31:46 +02:00
|
|
|
m.AddAnnotation("architecture", runtime.GOARCH)
|
2021-02-02 12:05:47 +01:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
// OpenReportContext implements Experiment.OpenReportContext.
|
|
|
|
func (e *experiment) OpenReportContext(ctx context.Context) error {
|
2021-02-02 12:05:47 +01:00
|
|
|
if e.report != nil {
|
|
|
|
return nil // already open
|
|
|
|
}
|
|
|
|
// use custom client to have proper byte accounting
|
|
|
|
httpClient := &http.Client{
|
2022-06-05 21:22:27 +02:00
|
|
|
Transport: bytecounter.WrapHTTPTransport(
|
|
|
|
e.session.httpDefaultTransport, // proxy is OK
|
|
|
|
e.byteCounter,
|
|
|
|
),
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
feat(session): expose CheckIn method (#266)
* feat(session): expose CheckIn method
It seems to me the right thing to do is to query the CheckIn API
from the Session rather than querying it from InputLoader.
Then, InputLoader could just take a reference to a Session-like
interface that allows this functionality.
So, this diff exposes the Session.CheckIn method.
Doing that, in turn, required some refactoring to allow for
more and better unit tests.
While doing that, I also noticed that Session required a mutex
to be a well-behaving type, so I did that.
While doing that, I also tried to cover all the lines in session.go
and, as part of that, I have removed unused code.
Reference issue: https://github.com/ooni/probe/issues/1299.
* fix: reinstate comment I shan't have removed
* fix: repair broken test
* fix: a bit more coverage, annotations, etc.
* Update internal/engine/session.go
* Update internal/engine/session_integration_test.go
* Update internal/engine/session_internal_test.go
2021-03-29 15:04:41 +02:00
|
|
|
client, err := e.session.NewProbeServicesClient(ctx)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
e.session.logger.Debugf("%+v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
client.HTTPClient = httpClient // patch HTTP client to use
|
|
|
|
template := e.newReportTemplate()
|
|
|
|
e.report, err = client.OpenReport(ctx, template)
|
|
|
|
if err != nil {
|
|
|
|
e.session.logger.Debugf("experiment: probe services error: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
func (e *experiment) newReportTemplate() probeservices.ReportTemplate {
|
2021-02-02 12:05:47 +01:00
|
|
|
return probeservices.ReportTemplate{
|
|
|
|
DataFormatVersion: probeservices.DefaultDataFormatVersion,
|
|
|
|
Format: probeservices.DefaultFormat,
|
|
|
|
ProbeASN: e.session.ProbeASNString(),
|
|
|
|
ProbeCC: e.session.ProbeCC(),
|
|
|
|
SoftwareName: e.session.SoftwareName(),
|
|
|
|
SoftwareVersion: e.session.SoftwareVersion(),
|
|
|
|
TestName: e.testName,
|
|
|
|
TestStartTime: e.testStartTime,
|
|
|
|
TestVersion: e.testVersion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-08 12:29:23 +02:00
|
|
|
func (e *experiment) saveMeasurement(
|
2021-02-02 12:05:47 +01:00
|
|
|
measurement *model.Measurement, filePath string,
|
|
|
|
marshal func(v interface{}) ([]byte, error),
|
|
|
|
openFile func(name string, flag int, perm os.FileMode) (*os.File, error),
|
|
|
|
write func(fp *os.File, b []byte) (n int, err error),
|
|
|
|
) error {
|
|
|
|
data, err := marshal(measurement)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data = append(data, byte('\n'))
|
|
|
|
filep, err := openFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := write(filep, data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return filep.Close()
|
|
|
|
}
|