feat: create tunnel inside NewSession (#286)
* feat: create tunnel inside NewSession We want to create the tunnel when we create the session. This change allows us to nicely ignore the problem of creating a tunnel when we already have a proxy, as well as the problem of locking. Everything is happening, in fact, inside of the NewSession factory. Modify miniooni such that --tunnel is just syntactic sugar for --proxy, at least for now. We want, in the future, to teach the tunnel to possibly use a socks5 proxy. Because starting a tunnel is a slow operation, we need a context in NewSession. This causes a bunch of places to change. Not really a big deal except we need to propagate the changes. Make sure that the mobile code can create a new session using a proxy for all the APIs we support. Make sure all tests are still green and we don't loose coverage of the various ways in which this code could be used. This change is part of https://github.com/ooni/probe/issues/985. * changes after merge * fix: only keep tests that can hopefully work While there, identify other places where we should add more tests or fix integration tests. Part of https://github.com/ooni/probe/issues/985
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package geoip
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/alecthomas/kingpin"
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/cmd/ooniprobe/internal/cli/root"
|
||||
@@ -34,7 +36,7 @@ func dogeoip(config dogeoipconfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
engine, err := probeCLI.NewProbeEngine()
|
||||
engine, err := probeCLI.NewProbeEngine(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package nettests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"testing"
|
||||
@@ -34,7 +35,7 @@ func TestCreateContext(t *testing.T) {
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
probe := newOONIProbe(t)
|
||||
sess, err := probe.NewSession()
|
||||
sess, err := probe.NewSession(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package nettests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -57,7 +58,7 @@ func RunGroup(config RunGroupConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
sess, err := config.Probe.NewSession()
|
||||
sess, err := config.Probe.NewSession(context.Background())
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to create a measurement session")
|
||||
return err
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ooni
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed" // because we embed a file
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -26,7 +27,7 @@ type ProbeCLI interface {
|
||||
IsBatch() bool
|
||||
Home() string
|
||||
TempDir() string
|
||||
NewProbeEngine() (ProbeEngine, error)
|
||||
NewProbeEngine(ctx context.Context) (ProbeEngine, error)
|
||||
}
|
||||
|
||||
// ProbeEngine is an instance of the OONI Probe engine.
|
||||
@@ -201,7 +202,7 @@ func (p *Probe) Init(softwareName, softwareVersion string) error {
|
||||
// NewSession creates a new ooni/probe-engine session using the
|
||||
// current configuration inside the context. The caller must close
|
||||
// the session when done using it, by calling sess.Close().
|
||||
func (p *Probe) NewSession() (*engine.Session, error) {
|
||||
func (p *Probe) NewSession(ctx context.Context) (*engine.Session, error) {
|
||||
kvstore, err := engine.NewFileSystemKVStore(
|
||||
utils.EngineDir(p.home),
|
||||
)
|
||||
@@ -211,7 +212,7 @@ func (p *Probe) NewSession() (*engine.Session, error) {
|
||||
if err := os.MkdirAll(utils.TunnelDir(p.home), 0700); err != nil {
|
||||
return nil, errors.Wrap(err, "creating tunnel dir")
|
||||
}
|
||||
return engine.NewSession(engine.SessionConfig{
|
||||
return engine.NewSession(ctx, engine.SessionConfig{
|
||||
KVStore: kvstore,
|
||||
Logger: enginex.Logger,
|
||||
SoftwareName: p.softwareName,
|
||||
@@ -222,8 +223,8 @@ func (p *Probe) NewSession() (*engine.Session, error) {
|
||||
}
|
||||
|
||||
// NewProbeEngine creates a new ProbeEngine instance.
|
||||
func (p *Probe) NewProbeEngine() (ProbeEngine, error) {
|
||||
sess, err := p.NewSession()
|
||||
func (p *Probe) NewProbeEngine(ctx context.Context) (ProbeEngine, error) {
|
||||
sess, err := p.NewSession(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package oonitest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/apex/log"
|
||||
@@ -60,7 +61,7 @@ func (cli *FakeProbeCLI) TempDir() string {
|
||||
}
|
||||
|
||||
// NewProbeEngine implements ProbeCLI.NewProbeEngine
|
||||
func (cli *FakeProbeCLI) NewProbeEngine() (ooni.ProbeEngine, error) {
|
||||
func (cli *FakeProbeCLI) NewProbeEngine(ctx context.Context) (ooni.ProbeEngine, error) {
|
||||
return cli.FakeProbeEnginePtr, cli.FakeProbeEngineErr
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user