2021-02-02 12:05:47 +01:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-03-29 19:37:32 +02:00
|
|
|
"io/fs"
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/internal/fsx"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/engine/model"
|
|
|
|
)
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// These errors are returned by the InputLoader.
|
2021-02-02 12:05:47 +01:00
|
|
|
var (
|
2021-03-29 18:46:26 +02:00
|
|
|
ErrNoURLsReturned = errors.New("no URLs returned")
|
2021-02-02 12:05:47 +01:00
|
|
|
ErrDetectedEmptyFile = errors.New("file did not contain any input")
|
2021-03-26 09:34:27 +01:00
|
|
|
ErrInputRequired = errors.New("no input provided")
|
|
|
|
ErrNoInputExpected = errors.New("we did not expect any input")
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// InputLoaderSession is the session according to an InputLoader. We
|
|
|
|
// introduce this abstraction because it helps us with testing.
|
2021-02-02 12:05:47 +01:00
|
|
|
type InputLoaderSession interface {
|
2021-03-29 18:46:26 +02:00
|
|
|
CheckIn(ctx context.Context,
|
|
|
|
config *model.CheckInConfig) (*model.CheckInInfo, error)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// InputLoader loads input according to the specified policy
|
2021-03-26 09:34:27 +01:00
|
|
|
// either from command line and input files or from OONI services. The
|
|
|
|
// behaviour depends on the input policy as described below.
|
2021-02-02 12:05:47 +01:00
|
|
|
//
|
|
|
|
// InputNone
|
|
|
|
//
|
|
|
|
// We fail if there is any StaticInput or any SourceFiles. If
|
|
|
|
// there's no input, we return a single, empty entry that causes
|
|
|
|
// experiments that don't require input to run once.
|
|
|
|
//
|
|
|
|
// InputOptional
|
|
|
|
//
|
|
|
|
// We gather input from StaticInput and SourceFiles. If there is
|
|
|
|
// input, we return it. Otherwise we return a single, empty entry
|
|
|
|
// that causes experiments that don't require input to run once.
|
|
|
|
//
|
2021-03-26 09:34:27 +01:00
|
|
|
// InputOrQueryBackend
|
2021-02-02 12:05:47 +01:00
|
|
|
//
|
|
|
|
// We gather input from StaticInput and SourceFiles. If there is
|
|
|
|
// input, we return it. Otherwise, we use OONI's probe services
|
2021-03-26 09:34:27 +01:00
|
|
|
// to gather input using the best API for the task.
|
2021-02-02 12:05:47 +01:00
|
|
|
//
|
|
|
|
// InputStrictlyRequired
|
|
|
|
//
|
2021-03-26 09:34:27 +01:00
|
|
|
// We gather input from StaticInput and SourceFiles. If there is
|
|
|
|
// input, we return it. Otherwise, we return an error.
|
2021-02-02 12:05:47 +01:00
|
|
|
type InputLoader interface {
|
|
|
|
// Load attempts to load input using the specified input loader. We will
|
|
|
|
// return a list of URLs because this is the only input we support.
|
|
|
|
Load(ctx context.Context) ([]model.URLInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputLoaderConfig contains config for InputLoader.
|
|
|
|
type InputLoaderConfig struct {
|
2021-03-29 18:46:26 +02:00
|
|
|
// CheckInConfig contains options for the CheckIn API. If
|
|
|
|
// not set, then we'll create a default config. If set but
|
|
|
|
// there are fields inside it that are not set, then we
|
|
|
|
// will set them to a default value.
|
|
|
|
CheckInConfig *model.CheckInConfig
|
|
|
|
|
|
|
|
// InputPolicy specifies the input policy for the
|
|
|
|
// current experiment. We will not load any input if
|
|
|
|
// the policy says we should not. You MUST fill in
|
|
|
|
// this field.
|
|
|
|
InputPolicy InputPolicy
|
|
|
|
|
|
|
|
// Session is the current measurement session. You
|
|
|
|
// MUST fill in this field.
|
|
|
|
Session InputLoaderSession
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// StaticInputs contains optional input to be added
|
|
|
|
// to the resulting input list if possible.
|
|
|
|
StaticInputs []string
|
|
|
|
|
|
|
|
// SourceFiles contains optional files to read input
|
|
|
|
// from. Each file should contain a single input string
|
2021-03-26 09:34:27 +01:00
|
|
|
// per line. We will fail if any file is unreadable
|
|
|
|
// as well as if any file is empty.
|
2021-02-02 12:05:47 +01:00
|
|
|
SourceFiles []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInputLoader creates a new InputLoader.
|
|
|
|
func NewInputLoader(config InputLoaderConfig) InputLoader {
|
|
|
|
// TODO(bassosimone): the current implementation stems from a
|
|
|
|
// simple refactoring from a previous implementation where
|
|
|
|
// we weren't using interfaces. Because now we're using interfaces,
|
|
|
|
// there is the opportunity to select behaviour here depending
|
|
|
|
// on the specified policy rather than later inside Load.
|
|
|
|
return inputLoader{InputLoaderConfig: config}
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// TODO(bassosimone): it seems there's no reason to return an
|
|
|
|
// interface from the constructor. Generally, "Effective Go"
|
|
|
|
// recommends that an interface is used by the receiver rather
|
|
|
|
// than by the sender. We should follow that rule of thumb.
|
|
|
|
|
|
|
|
// inputLoader is the concrete implementation of InputLoader.
|
2021-02-02 12:05:47 +01:00
|
|
|
type inputLoader struct {
|
|
|
|
InputLoaderConfig
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// verifies that inputLoader is an InputLoader.
|
2021-02-02 12:05:47 +01:00
|
|
|
var _ InputLoader = inputLoader{}
|
|
|
|
|
|
|
|
// Load attempts to load input using the specified input loader. We will
|
|
|
|
// return a list of URLs because this is the only input we support.
|
|
|
|
func (il inputLoader) Load(ctx context.Context) ([]model.URLInfo, error) {
|
|
|
|
switch il.InputPolicy {
|
|
|
|
case InputOptional:
|
|
|
|
return il.loadOptional()
|
2021-03-26 09:34:27 +01:00
|
|
|
case InputOrQueryBackend:
|
|
|
|
return il.loadOrQueryBackend(ctx)
|
2021-02-02 12:05:47 +01:00
|
|
|
case InputStrictlyRequired:
|
|
|
|
return il.loadStrictlyRequired(ctx)
|
|
|
|
default:
|
|
|
|
return il.loadNone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// loadNone implements the InputNone policy.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (il inputLoader) loadNone() ([]model.URLInfo, error) {
|
|
|
|
if len(il.StaticInputs) > 0 || len(il.SourceFiles) > 0 {
|
|
|
|
return nil, ErrNoInputExpected
|
|
|
|
}
|
2021-03-26 09:34:27 +01:00
|
|
|
// Note that we need to return a single empty entry.
|
2021-02-02 12:05:47 +01:00
|
|
|
return []model.URLInfo{{}}, nil
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// loadOptional implements the InputOptional policy.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (il inputLoader) loadOptional() ([]model.URLInfo, error) {
|
|
|
|
inputs, err := il.loadLocal()
|
|
|
|
if err == nil && len(inputs) <= 0 {
|
2021-03-26 09:34:27 +01:00
|
|
|
// Note that we need to return a single empty entry.
|
2021-02-02 12:05:47 +01:00
|
|
|
inputs = []model.URLInfo{{}}
|
|
|
|
}
|
|
|
|
return inputs, err
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// loadStrictlyRequired implements the InputStrictlyRequired policy.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (il inputLoader) loadStrictlyRequired(ctx context.Context) ([]model.URLInfo, error) {
|
|
|
|
inputs, err := il.loadLocal()
|
|
|
|
if err != nil || len(inputs) > 0 {
|
|
|
|
return inputs, err
|
|
|
|
}
|
|
|
|
return nil, ErrInputRequired
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// loadOrQueryBackend implements the InputOrQueryBackend policy.
|
|
|
|
func (il inputLoader) loadOrQueryBackend(ctx context.Context) ([]model.URLInfo, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
inputs, err := il.loadLocal()
|
|
|
|
if err != nil || len(inputs) > 0 {
|
|
|
|
return inputs, err
|
|
|
|
}
|
2021-03-26 09:34:27 +01:00
|
|
|
return il.loadRemote(inputLoaderLoadRemoteConfig{ctx: ctx, session: il.Session})
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// loadLocal loads inputs from StaticInputs and SourceFiles.
|
2021-02-02 12:05:47 +01:00
|
|
|
func (il inputLoader) loadLocal() ([]model.URLInfo, error) {
|
|
|
|
inputs := []model.URLInfo{}
|
|
|
|
for _, input := range il.StaticInputs {
|
|
|
|
inputs = append(inputs, model.URLInfo{URL: input})
|
|
|
|
}
|
|
|
|
for _, filepath := range il.SourceFiles {
|
|
|
|
extra, err := il.readfile(filepath, fsx.Open)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-26 10:16:34 +01:00
|
|
|
// See https://github.com/ooni/probe-engine/issues/1123.
|
2021-02-02 12:05:47 +01:00
|
|
|
if len(extra) <= 0 {
|
|
|
|
return nil, fmt.Errorf("%w: %s", ErrDetectedEmptyFile, filepath)
|
|
|
|
}
|
|
|
|
inputs = append(inputs, extra...)
|
|
|
|
}
|
|
|
|
return inputs, nil
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// inputLoaderOpenFn is the type of the function to open a file.
|
2021-03-29 19:37:32 +02:00
|
|
|
type inputLoaderOpenFn func(filepath string) (fs.File, error)
|
2021-03-26 09:34:27 +01:00
|
|
|
|
|
|
|
// readfile reads inputs from the specified file. The open argument should be
|
|
|
|
// compatibile with stdlib's fs.Open and helps us with unit testing.
|
|
|
|
func (il inputLoader) readfile(filepath string, open inputLoaderOpenFn) ([]model.URLInfo, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
inputs := []model.URLInfo{}
|
|
|
|
filep, err := open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer filep.Close()
|
|
|
|
// Implementation note: when you save file with vim, you have newline at
|
|
|
|
// end of file and you don't want to consider that an input line. While there
|
|
|
|
// ignore any other empty line that may occur inside the file.
|
|
|
|
scanner := bufio.NewScanner(filep)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line != "" {
|
|
|
|
inputs = append(inputs, model.URLInfo{URL: line})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if scanner.Err() != nil {
|
|
|
|
return nil, scanner.Err()
|
|
|
|
}
|
|
|
|
return inputs, nil
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// inputLoaderLoadRemoteConfig contains configuration for loading the input from
|
|
|
|
// a remote source (which currently is _only_ the OONI backend).
|
|
|
|
type inputLoaderLoadRemoteConfig struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
ctx context.Context
|
|
|
|
session InputLoaderSession
|
|
|
|
}
|
|
|
|
|
2021-03-26 09:34:27 +01:00
|
|
|
// loadRemote loads inputs from a remote source.
|
|
|
|
func (il inputLoader) loadRemote(conf inputLoaderLoadRemoteConfig) ([]model.URLInfo, error) {
|
2021-03-29 18:46:26 +02:00
|
|
|
config := il.CheckInConfig
|
|
|
|
if config == nil {
|
|
|
|
// Note: Session.CheckIn documentation says it will fill in
|
|
|
|
// any field with a required value with a reasonable default
|
|
|
|
// if such value is missing. So, here we just need to be
|
|
|
|
// concerned about NOT passing it a NULL pointer.
|
|
|
|
config = &model.CheckInConfig{}
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
2021-03-29 18:46:26 +02:00
|
|
|
reply, err := conf.session.CheckIn(conf.ctx, config)
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-29 18:46:26 +02:00
|
|
|
if reply.WebConnectivity == nil || len(reply.WebConnectivity.URLs) <= 0 {
|
|
|
|
return nil, ErrNoURLsReturned
|
|
|
|
}
|
|
|
|
return reply.WebConnectivity.URLs, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|