refactor: merge psiphonx and torx into tunnel (#287)

* refactor: merge psiphonx and torx into tunnel

This is a case where it seems that merging these three packages into
a single package will enable us to better the implementation.

The goal is still https://github.com/ooni/probe/issues/985.

The roadblock I'm trying to overcome is
https://github.com/ooni/probe-cli/pull/286#pullrequestreview-627460104.

* avoid duplicating logger for now
This commit is contained in:
Simone Basso 2021-04-03 19:57:21 +02:00 committed by GitHub
parent d7cd1ebcaf
commit ecb2aae1e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 256 additions and 270 deletions

View File

@ -6,11 +6,11 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/ooni/probe-cli/v3/internal/engine/internal/tunnel"
"github.com/ooni/probe-cli/v3/internal/engine/model" "github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/netx/archival" "github.com/ooni/probe-cli/v3/internal/engine/netx/archival"
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx" "github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace" "github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
) )
// The Getter gets the specified target in the context of the // The Getter gets the specified target in the context of the

View File

@ -6,9 +6,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"github.com/ooni/probe-cli/v3/internal/engine/internal/psiphonx"
"github.com/ooni/probe-cli/v3/internal/engine/internal/torx"
"github.com/ooni/probe-cli/v3/internal/engine/internal/tunnel"
"github.com/ooni/probe-cli/v3/internal/engine/kvstore" "github.com/ooni/probe-cli/v3/internal/engine/kvstore"
"github.com/ooni/probe-cli/v3/internal/engine/model" "github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices" "github.com/ooni/probe-cli/v3/internal/engine/probeservices"
@ -145,6 +142,3 @@ func (sess *Session) UserAgent() string {
var _ model.ExperimentSession = &Session{} var _ model.ExperimentSession = &Session{}
var _ probeservices.Session = &Session{} var _ probeservices.Session = &Session{}
var _ psiphonx.Session = &Session{}
var _ tunnel.Session = &Session{}
var _ torx.Session = &Session{}

View File

@ -1 +0,0 @@
/psiphon.gz

View File

@ -1,160 +0,0 @@
package psiphonx_test
import (
"context"
"errors"
"os"
"testing"
"github.com/apex/log"
engine "github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
"github.com/ooni/probe-cli/v3/internal/engine/internal/psiphonx"
"github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary/clientlib"
)
func TestStartWithCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
sess, err := engine.NewSession(engine.SessionConfig{
Logger: log.Log,
SoftwareName: "ooniprobe-engine",
SoftwareVersion: "0.0.1",
})
if err != nil {
t.Fatal(err)
}
tunnel, err := psiphonx.Start(ctx, sess, psiphonx.Config{})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartStop(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
sess, err := engine.NewSession(engine.SessionConfig{
Logger: log.Log,
SoftwareName: "ooniprobe-engine",
SoftwareVersion: "0.0.1",
})
if err != nil {
t.Fatal(err)
}
tunnel, err := psiphonx.Start(context.Background(), sess, psiphonx.Config{})
if err != nil {
t.Fatal(err)
}
if tunnel.SOCKS5ProxyURL() == nil {
t.Fatal("expected non nil URL here")
}
if tunnel.BootstrapTime() <= 0 {
t.Fatal("expected positive bootstrap time here")
}
tunnel.Stop()
}
func TestFetchPsiphonConfigFailure(t *testing.T) {
expected := errors.New("mocked error")
sess := &mockable.Session{
MockableFetchPsiphonConfigErr: expected,
}
tunnel, err := psiphonx.Start(context.Background(), sess, psiphonx.Config{})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestMakeMkdirAllFailure(t *testing.T) {
expected := errors.New("mocked error")
dependencies := FakeDependencies{
MkdirAllErr: expected,
}
sess := &mockable.Session{
MockableFetchPsiphonConfigResult: []byte(`{}`),
}
tunnel, err := psiphonx.Start(context.Background(), sess, psiphonx.Config{
Dependencies: dependencies,
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestMakeRemoveAllFailure(t *testing.T) {
expected := errors.New("mocked error")
dependencies := FakeDependencies{
RemoveAllErr: expected,
}
sess := &mockable.Session{
MockableFetchPsiphonConfigResult: []byte(`{}`),
}
tunnel, err := psiphonx.Start(context.Background(), sess, psiphonx.Config{
Dependencies: dependencies,
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestMakeStartFailure(t *testing.T) {
expected := errors.New("mocked error")
dependencies := FakeDependencies{
StartErr: expected,
}
sess := &mockable.Session{
MockableFetchPsiphonConfigResult: []byte(`{}`),
}
tunnel, err := psiphonx.Start(context.Background(), sess, psiphonx.Config{
Dependencies: dependencies,
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestNilTunnel(t *testing.T) {
var tunnel *psiphonx.Tunnel
if tunnel.BootstrapTime() != 0 {
t.Fatal("expected zero bootstrap time")
}
if tunnel.SOCKS5ProxyURL() != nil {
t.Fatal("expected nil SOCKS Proxy URL")
}
tunnel.Stop() // must not crash
}
type FakeDependencies struct {
MkdirAllErr error
RemoveAllErr error
StartErr error
}
func (fd FakeDependencies) MkdirAll(path string, perm os.FileMode) error {
return fd.MkdirAllErr
}
func (fd FakeDependencies) RemoveAll(path string) error {
return fd.RemoveAllErr
}
func (fd FakeDependencies) Start(
ctx context.Context, config []byte, workdir string) (*clientlib.PsiphonTunnel, error) {
return nil, fd.StartErr
}

View File

@ -1,14 +0,0 @@
package torx
import (
"net/url"
"time"
)
func NewTunnel(bootstrapTime time.Duration, instance TorProcess, proxy *url.URL) *Tunnel {
return &Tunnel{
bootstrapTime: bootstrapTime,
instance: instance,
proxy: proxy,
}
}

View File

@ -14,12 +14,12 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/geolocate" "github.com/ooni/probe-cli/v3/internal/engine/geolocate"
"github.com/ooni/probe-cli/v3/internal/engine/internal/platform" "github.com/ooni/probe-cli/v3/internal/engine/internal/platform"
"github.com/ooni/probe-cli/v3/internal/engine/internal/sessionresolver" "github.com/ooni/probe-cli/v3/internal/engine/internal/sessionresolver"
"github.com/ooni/probe-cli/v3/internal/engine/internal/tunnel"
"github.com/ooni/probe-cli/v3/internal/engine/kvstore" "github.com/ooni/probe-cli/v3/internal/engine/kvstore"
"github.com/ooni/probe-cli/v3/internal/engine/model" "github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/netx" "github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter" "github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices" "github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
"github.com/ooni/probe-cli/v3/internal/version" "github.com/ooni/probe-cli/v3/internal/version"
) )

View File

@ -0,0 +1,62 @@
package tunnel_test
import (
"context"
"errors"
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine"
"github.com/ooni/probe-cli/v3/internal/engine/tunnel"
)
func TestPsiphonStartWithCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
sess, err := engine.NewSession(engine.SessionConfig{
Logger: log.Log,
SoftwareName: "ooniprobe-engine",
SoftwareVersion: "0.0.1",
})
if err != nil {
t.Fatal(err)
}
tunnel, err := tunnel.Start(ctx, tunnel.Config{
Name: "psiphon",
Session: sess,
})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestPsiphonStartStop(t *testing.T) {
if testing.Short() {
t.Skip("skip test in short mode")
}
sess, err := engine.NewSession(engine.SessionConfig{
Logger: log.Log,
SoftwareName: "ooniprobe-engine",
SoftwareVersion: "0.0.1",
})
if err != nil {
t.Fatal(err)
}
tunnel, err := tunnel.Start(context.Background(), tunnel.Config{
Name: "psiphon",
Session: sess,
})
if err != nil {
t.Fatal(err)
}
if tunnel.SOCKS5ProxyURL() == nil {
t.Fatal("expected non nil URL here")
}
if tunnel.BootstrapTime() <= 0 {
t.Fatal("expected positive bootstrap time here")
}
tunnel.Stop()
}

View File

@ -1,5 +1,4 @@
// Package psiphonx is a wrapper around the psiphon-tunnel-core. package tunnel
package psiphonx
import ( import (
"context" "context"
@ -13,14 +12,8 @@ import (
"github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary/clientlib" "github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary/clientlib"
) )
// Session is the way in which this package sees a Session. // psiphonDependencies contains dependencies for psiphonStart
type Session interface { type psiphonDependencies interface {
FetchPsiphonConfig(ctx context.Context) ([]byte, error)
TempDir() string
}
// Dependencies contains dependencies for Start
type Dependencies interface {
MkdirAll(path string, perm os.FileMode) error MkdirAll(path string, perm os.FileMode) error
RemoveAll(path string) error RemoveAll(path string) error
Start(ctx context.Context, config []byte, Start(ctx context.Context, config []byte,
@ -43,24 +36,24 @@ func (defaultDependencies) Start(
DataRootDirectory: &workdir}, nil, nil) DataRootDirectory: &workdir}, nil, nil)
} }
// Config contains the settings for Start. The empty config object implies // psiphonConfig contains the settings for psiphonStart. The empty config object implies
// that we will be using default settings for starting the tunnel. // that we will be using default settings for starting the tunnel.
type Config struct { type psiphonConfig struct {
// Dependencies contains dependencies for Start. // Dependencies contains dependencies for Start.
Dependencies Dependencies Dependencies psiphonDependencies
// WorkDir is the directory where Psiphon should store // WorkDir is the directory where Psiphon should store
// its configuration database. // its configuration database.
WorkDir string WorkDir string
} }
// Tunnel is a psiphon tunnel // psiphonTunnel is a psiphon tunnel
type Tunnel struct { type psiphonTunnel struct {
tunnel *clientlib.PsiphonTunnel tunnel *clientlib.PsiphonTunnel
duration time.Duration duration time.Duration
} }
func makeworkingdir(config Config) (string, error) { func makeworkingdir(config psiphonConfig) (string, error) {
const testdirname = "oonipsiphon" const testdirname = "oonipsiphon"
workdir := filepath.Join(config.WorkDir, testdirname) workdir := filepath.Join(config.WorkDir, testdirname)
if err := config.Dependencies.RemoveAll(workdir); err != nil { if err := config.Dependencies.RemoveAll(workdir); err != nil {
@ -72,9 +65,9 @@ func makeworkingdir(config Config) (string, error) {
return workdir, nil return workdir, nil
} }
// Start starts the psiphon tunnel. // psiphonStart starts the psiphon tunnel.
func Start( func psiphonStart(
ctx context.Context, sess Session, config Config) (*Tunnel, error) { ctx context.Context, sess Session, config psiphonConfig) (Tunnel, error) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil, ctx.Err() // simplifies unit testing this code return nil, ctx.Err() // simplifies unit testing this code
@ -100,18 +93,18 @@ func Start(
return nil, err return nil, err
} }
stop := time.Now() stop := time.Now()
return &Tunnel{tunnel: tunnel, duration: stop.Sub(start)}, nil return &psiphonTunnel{tunnel: tunnel, duration: stop.Sub(start)}, nil
} }
// Stop is an idempotent method that shuts down the tunnel // Stop is an idempotent method that shuts down the tunnel
func (t *Tunnel) Stop() { func (t *psiphonTunnel) Stop() {
if t != nil { if t != nil {
t.tunnel.Stop() t.tunnel.Stop()
} }
} }
// SOCKS5ProxyURL returns the SOCKS5 proxy URL. // SOCKS5ProxyURL returns the SOCKS5 proxy URL.
func (t *Tunnel) SOCKS5ProxyURL() (proxyURL *url.URL) { func (t *psiphonTunnel) SOCKS5ProxyURL() (proxyURL *url.URL) {
if t != nil { if t != nil {
proxyURL = &url.URL{ proxyURL = &url.URL{
Scheme: "socks5", Scheme: "socks5",
@ -123,7 +116,7 @@ func (t *Tunnel) SOCKS5ProxyURL() (proxyURL *url.URL) {
} }
// BootstrapTime returns the bootstrap time // BootstrapTime returns the bootstrap time
func (t *Tunnel) BootstrapTime() (duration time.Duration) { func (t *psiphonTunnel) BootstrapTime() (duration time.Duration) {
if t != nil { if t != nil {
duration = t.duration duration = t.duration
} }

View File

@ -0,0 +1,112 @@
package tunnel
import (
"context"
"errors"
"os"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
"github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary/clientlib"
)
func TestPsiphonFetchPsiphonConfigFailure(t *testing.T) {
expected := errors.New("mocked error")
sess := &mockable.Session{
MockableFetchPsiphonConfigErr: expected,
}
tunnel, err := psiphonStart(context.Background(), sess, psiphonConfig{})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestPsiphonMakeMkdirAllFailure(t *testing.T) {
expected := errors.New("mocked error")
dependencies := psiphonFakeDependencies{
MkdirAllErr: expected,
}
sess := &mockable.Session{
MockableFetchPsiphonConfigResult: []byte(`{}`),
}
tunnel, err := psiphonStart(context.Background(), sess, psiphonConfig{
Dependencies: dependencies,
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestPsiphonMakeRemoveAllFailure(t *testing.T) {
expected := errors.New("mocked error")
dependencies := psiphonFakeDependencies{
RemoveAllErr: expected,
}
sess := &mockable.Session{
MockableFetchPsiphonConfigResult: []byte(`{}`),
}
tunnel, err := psiphonStart(context.Background(), sess, psiphonConfig{
Dependencies: dependencies,
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestPsiphonMakeStartFailure(t *testing.T) {
expected := errors.New("mocked error")
dependencies := psiphonFakeDependencies{
StartErr: expected,
}
sess := &mockable.Session{
MockableFetchPsiphonConfigResult: []byte(`{}`),
}
tunnel, err := psiphonStart(context.Background(), sess, psiphonConfig{
Dependencies: dependencies,
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestPsiphonNilTunnel(t *testing.T) {
var tunnel *psiphonTunnel
if tunnel.BootstrapTime() != 0 {
t.Fatal("expected zero bootstrap time")
}
if tunnel.SOCKS5ProxyURL() != nil {
t.Fatal("expected nil SOCKS Proxy URL")
}
tunnel.Stop() // must not crash
}
type psiphonFakeDependencies struct {
MkdirAllErr error
RemoveAllErr error
StartErr error
}
func (fd psiphonFakeDependencies) MkdirAll(path string, perm os.FileMode) error {
return fd.MkdirAllErr
}
func (fd psiphonFakeDependencies) RemoveAll(path string) error {
return fd.RemoveAllErr
}
func (fd psiphonFakeDependencies) Start(
ctx context.Context, config []byte, workdir string) (*clientlib.PsiphonTunnel, error) {
return nil, fd.StartErr
}

View File

@ -1,5 +1,4 @@
// Package torx contains code to control tor. package tunnel
package torx
import ( import (
"context" "context"
@ -13,27 +12,20 @@ import (
"github.com/cretz/bine/tor" "github.com/cretz/bine/tor"
) )
// Session is the way in which this package sees a Session. // torProcess is a running tor process
type Session interface { type torProcess interface {
TempDir() string
TorArgs() []string
TorBinary() string
}
// TorProcess is a running tor process
type TorProcess interface {
Close() error Close() error
} }
// Tunnel is the Tor tunnel // torTunnel is the Tor tunnel
type Tunnel struct { type torTunnel struct {
bootstrapTime time.Duration bootstrapTime time.Duration
instance TorProcess instance torProcess
proxy *url.URL proxy *url.URL
} }
// BootstrapTime is the bootstrsap time // BootstrapTime is the bootstrsap time
func (tt *Tunnel) BootstrapTime() (duration time.Duration) { func (tt *torTunnel) BootstrapTime() (duration time.Duration) {
if tt != nil { if tt != nil {
duration = tt.bootstrapTime duration = tt.bootstrapTime
} }
@ -41,7 +33,7 @@ func (tt *Tunnel) BootstrapTime() (duration time.Duration) {
} }
// SOCKS5ProxyURL returns the URL of the SOCKS5 proxy // SOCKS5ProxyURL returns the URL of the SOCKS5 proxy
func (tt *Tunnel) SOCKS5ProxyURL() (url *url.URL) { func (tt *torTunnel) SOCKS5ProxyURL() (url *url.URL) {
if tt != nil { if tt != nil {
url = tt.proxy url = tt.proxy
} }
@ -49,23 +41,23 @@ func (tt *Tunnel) SOCKS5ProxyURL() (url *url.URL) {
} }
// Stop stops the Tor tunnel // Stop stops the Tor tunnel
func (tt *Tunnel) Stop() { func (tt *torTunnel) Stop() {
if tt != nil { if tt != nil {
tt.instance.Close() tt.instance.Close()
} }
} }
// StartConfig contains the configuration for StartWithConfig // torStartConfig contains the configuration for StartWithConfig
type StartConfig struct { type torStartConfig struct {
Sess Session Sess Session
Start func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) Start func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error)
EnableNetwork func(ctx context.Context, tor *tor.Tor, wait bool) error EnableNetwork func(ctx context.Context, tor *tor.Tor, wait bool) error
GetInfo func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) GetInfo func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error)
} }
// Start starts the tor tunnel // torStart starts the tor tunnel
func Start(ctx context.Context, sess Session) (*Tunnel, error) { func torStart(ctx context.Context, sess Session) (Tunnel, error) {
return StartWithConfig(ctx, StartConfig{ return torStartWithConfig(ctx, torStartConfig{
Sess: sess, Sess: sess,
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return tor.Start(ctx, conf) return tor.Start(ctx, conf)
@ -79,8 +71,8 @@ func Start(ctx context.Context, sess Session) (*Tunnel, error) {
}) })
} }
// StartWithConfig is a configurable Start for testing // torStartWithConfig is a configurable torStart for testing
func StartWithConfig(ctx context.Context, config StartConfig) (*Tunnel, error) { func torStartWithConfig(ctx context.Context, config torStartConfig) (Tunnel, error) {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil, ctx.Err() // allows to write unit tests using this code return nil, ctx.Err() // allows to write unit tests using this code
@ -123,7 +115,7 @@ func StartWithConfig(ctx context.Context, config StartConfig) (*Tunnel, error) {
instance.Close() instance.Close()
return nil, fmt.Errorf("tor returned unsupported proxy") return nil, fmt.Errorf("tor returned unsupported proxy")
} }
return &Tunnel{ return &torTunnel{
bootstrapTime: stop.Sub(start), bootstrapTime: stop.Sub(start),
instance: instance, instance: instance,
proxy: &url.URL{Scheme: "socks5", Host: proxyAddress}, proxy: &url.URL{Scheme: "socks5", Host: proxyAddress},
@ -135,3 +127,12 @@ func StartWithConfig(ctx context.Context, config StartConfig) (*Tunnel, error) {
func LogFile(sess Session) string { func LogFile(sess Session) string {
return path.Join(sess.TempDir(), "tor.log") return path.Join(sess.TempDir(), "tor.log")
} }
// newTorTunnel creates a new torTunnel
func newTorTunnel(bootstrapTime time.Duration, instance torProcess, proxy *url.URL) *torTunnel {
return &torTunnel{
bootstrapTime: bootstrapTime,
instance: instance,
proxy: proxy,
}
}

View File

@ -1,4 +1,4 @@
package torx_test package tunnel
import ( import (
"context" "context"
@ -9,7 +9,6 @@ import (
"github.com/cretz/bine/control" "github.com/cretz/bine/control"
"github.com/cretz/bine/tor" "github.com/cretz/bine/tor"
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable" "github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
"github.com/ooni/probe-cli/v3/internal/engine/internal/torx"
) )
type Closer struct { type Closer struct {
@ -21,10 +20,10 @@ func (c *Closer) Close() error {
return errors.New("mocked mocked mocked") return errors.New("mocked mocked mocked")
} }
func TestTunnelNonNil(t *testing.T) { func TestTorTunnelNonNil(t *testing.T) {
closer := new(Closer) closer := new(Closer)
proxy := &url.URL{Scheme: "x", Host: "10.0.0.1:443"} proxy := &url.URL{Scheme: "x", Host: "10.0.0.1:443"}
tun := torx.NewTunnel(128, closer, proxy) tun := newTorTunnel(128, closer, proxy)
if tun.BootstrapTime() != 128 { if tun.BootstrapTime() != 128 {
t.Fatal("not the bootstrap time we expected") t.Fatal("not the bootstrap time we expected")
} }
@ -37,8 +36,8 @@ func TestTunnelNonNil(t *testing.T) {
} }
} }
func TestTunnelNil(t *testing.T) { func TestTorTunnelNil(t *testing.T) {
var tun *torx.Tunnel var tun *torTunnel
if tun.BootstrapTime() != 0 { if tun.BootstrapTime() != 0 {
t.Fatal("not the bootstrap time we expected") t.Fatal("not the bootstrap time we expected")
} }
@ -48,10 +47,10 @@ func TestTunnelNil(t *testing.T) {
tun.Stop() // ensure we don't crash tun.Stop() // ensure we don't crash
} }
func TestStartWithCancelledContext(t *testing.T) { func TestTorStartWithCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
tun, err := torx.Start(ctx, &mockable.Session{}) tun, err := torStart(ctx, &mockable.Session{})
if !errors.Is(err, context.Canceled) { if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected") t.Fatal("not the error we expected")
} }
@ -60,10 +59,10 @@ func TestStartWithCancelledContext(t *testing.T) {
} }
} }
func TestStartWithConfigStartFailure(t *testing.T) { func TestTorStartWithConfigStartFailure(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
ctx := context.Background() ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{ tun, err := torStartWithConfig(ctx, torStartConfig{
Sess: &mockable.Session{}, Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return nil, expected return nil, expected
@ -77,10 +76,10 @@ func TestStartWithConfigStartFailure(t *testing.T) {
} }
} }
func TestStartWithConfigEnableNetworkFailure(t *testing.T) { func TestTorStartWithConfigEnableNetworkFailure(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
ctx := context.Background() ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{ tun, err := torStartWithConfig(ctx, torStartConfig{
Sess: &mockable.Session{}, Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil return &tor.Tor{}, nil
@ -97,10 +96,10 @@ func TestStartWithConfigEnableNetworkFailure(t *testing.T) {
} }
} }
func TestStartWithConfigGetInfoFailure(t *testing.T) { func TestTorStartWithConfigGetInfoFailure(t *testing.T) {
expected := errors.New("mocked error") expected := errors.New("mocked error")
ctx := context.Background() ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{ tun, err := torStartWithConfig(ctx, torStartConfig{
Sess: &mockable.Session{}, Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil return &tor.Tor{}, nil
@ -120,9 +119,9 @@ func TestStartWithConfigGetInfoFailure(t *testing.T) {
} }
} }
func TestStartWithConfigGetInfoInvalidNumberOfKeys(t *testing.T) { func TestTorStartWithConfigGetInfoInvalidNumberOfKeys(t *testing.T) {
ctx := context.Background() ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{ tun, err := torStartWithConfig(ctx, torStartConfig{
Sess: &mockable.Session{}, Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil return &tor.Tor{}, nil
@ -142,9 +141,9 @@ func TestStartWithConfigGetInfoInvalidNumberOfKeys(t *testing.T) {
} }
} }
func TestStartWithConfigGetInfoInvalidKey(t *testing.T) { func TestTorStartWithConfigGetInfoInvalidKey(t *testing.T) {
ctx := context.Background() ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{ tun, err := torStartWithConfig(ctx, torStartConfig{
Sess: &mockable.Session{}, Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil return &tor.Tor{}, nil
@ -164,9 +163,9 @@ func TestStartWithConfigGetInfoInvalidKey(t *testing.T) {
} }
} }
func TestStartWithConfigGetInfoInvalidProxyType(t *testing.T) { func TestTorStartWithConfigGetInfoInvalidProxyType(t *testing.T) {
ctx := context.Background() ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{ tun, err := torStartWithConfig(ctx, torStartConfig{
Sess: &mockable.Session{}, Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil return &tor.Tor{}, nil
@ -186,9 +185,9 @@ func TestStartWithConfigGetInfoInvalidProxyType(t *testing.T) {
} }
} }
func TestStartWithConfigSuccess(t *testing.T) { func TestTorStartWithConfigSuccess(t *testing.T) {
ctx := context.Background() ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{ tun, err := torStartWithConfig(ctx, torStartConfig{
Sess: &mockable.Session{}, Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) { Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil return &tor.Tor{}, nil

View File

@ -1,4 +1,5 @@
// Package tunnel contains code to create a psiphon or tor tunnel. // Package tunnel allows to create tunnels to speak
// with OONI backends and other services.
package tunnel package tunnel
import ( import (
@ -7,15 +8,15 @@ import (
"net/url" "net/url"
"time" "time"
"github.com/ooni/probe-cli/v3/internal/engine/internal/psiphonx"
"github.com/ooni/probe-cli/v3/internal/engine/internal/torx"
"github.com/ooni/probe-cli/v3/internal/engine/model" "github.com/ooni/probe-cli/v3/internal/engine/model"
) )
// Session is the way in which this package sees a Session. // Session is the way in which this package sees a Session.
type Session interface { type Session interface {
psiphonx.Session FetchPsiphonConfig(ctx context.Context) ([]byte, error)
torx.Session TempDir() string
TorArgs() []string
TorBinary() string
Logger() model.Logger Logger() model.Logger
} }
@ -43,13 +44,13 @@ func Start(ctx context.Context, config Config) (Tunnel, error) {
return enforceNilContract(nil, nil) return enforceNilContract(nil, nil)
case "psiphon": case "psiphon":
logger.Infof("starting %s tunnel; please be patient...", config.Name) logger.Infof("starting %s tunnel; please be patient...", config.Name)
tun, err := psiphonx.Start(ctx, config.Session, psiphonx.Config{ tun, err := psiphonStart(ctx, config.Session, psiphonConfig{
WorkDir: config.WorkDir, WorkDir: config.WorkDir,
}) })
return enforceNilContract(tun, err) return enforceNilContract(tun, err)
case "tor": case "tor":
logger.Infof("starting %s tunnel; please be patient...", config.Name) logger.Infof("starting %s tunnel; please be patient...", config.Name)
tun, err := torx.Start(ctx, config.Session) tun, err := torStart(ctx, config.Session)
return enforceNilContract(tun, err) return enforceNilContract(tun, err)
default: default:
return nil, errors.New("unsupported tunnel") return nil, errors.New("unsupported tunnel")

View File

@ -1,4 +1,4 @@
package tunnel_test package tunnel
import ( import (
"context" "context"
@ -7,13 +7,12 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable" "github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
"github.com/ooni/probe-cli/v3/internal/engine/internal/tunnel"
) )
func TestNoTunnel(t *testing.T) { func TestStartNoTunnel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
tunnel, err := tunnel.Start(ctx, tunnel.Config{ tunnel, err := Start(ctx, Config{
Name: "", Name: "",
Session: &mockable.Session{ Session: &mockable.Session{
MockableLogger: log.Log, MockableLogger: log.Log,
@ -27,10 +26,10 @@ func TestNoTunnel(t *testing.T) {
} }
} }
func TestPsiphonTunnel(t *testing.T) { func TestStartPsiphonTunnel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
tunnel, err := tunnel.Start(ctx, tunnel.Config{ tunnel, err := Start(ctx, Config{
Name: "psiphon", Name: "psiphon",
Session: &mockable.Session{ Session: &mockable.Session{
MockableLogger: log.Log, MockableLogger: log.Log,
@ -44,10 +43,10 @@ func TestPsiphonTunnel(t *testing.T) {
} }
} }
func TestTorTunnel(t *testing.T) { func TestStartTorTunnel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
tunnel, err := tunnel.Start(ctx, tunnel.Config{ tunnel, err := Start(ctx, Config{
Name: "tor", Name: "tor",
Session: &mockable.Session{ Session: &mockable.Session{
MockableLogger: log.Log, MockableLogger: log.Log,
@ -61,10 +60,10 @@ func TestTorTunnel(t *testing.T) {
} }
} }
func TestInvalidTunnel(t *testing.T) { func TestStartInvalidTunnel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
tunnel, err := tunnel.Start(ctx, tunnel.Config{ tunnel, err := Start(ctx, Config{
Name: "antani", Name: "antani",
Session: &mockable.Session{ Session: &mockable.Session{
MockableLogger: log.Log, MockableLogger: log.Log,