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
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,9 +6,6 @@ import (
"net/http"
"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/model"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
@ -145,6 +142,3 @@ func (sess *Session) UserAgent() string {
var _ model.ExperimentSession = &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,131 +0,0 @@
// Package psiphonx is a wrapper around the psiphon-tunnel-core.
package psiphonx
import (
"context"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"time"
"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.
type Session interface {
FetchPsiphonConfig(ctx context.Context) ([]byte, error)
TempDir() string
}
// Dependencies contains dependencies for Start
type Dependencies interface {
MkdirAll(path string, perm os.FileMode) error
RemoveAll(path string) error
Start(ctx context.Context, config []byte,
workdir string) (*clientlib.PsiphonTunnel, error)
}
type defaultDependencies struct{}
func (defaultDependencies) MkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}
func (defaultDependencies) RemoveAll(path string) error {
return os.RemoveAll(path)
}
func (defaultDependencies) Start(
ctx context.Context, config []byte, workdir string) (*clientlib.PsiphonTunnel, error) {
return clientlib.StartTunnel(ctx, config, "", clientlib.Parameters{
DataRootDirectory: &workdir}, nil, nil)
}
// Config contains the settings for Start. The empty config object implies
// that we will be using default settings for starting the tunnel.
type Config struct {
// Dependencies contains dependencies for Start.
Dependencies Dependencies
// WorkDir is the directory where Psiphon should store
// its configuration database.
WorkDir string
}
// Tunnel is a psiphon tunnel
type Tunnel struct {
tunnel *clientlib.PsiphonTunnel
duration time.Duration
}
func makeworkingdir(config Config) (string, error) {
const testdirname = "oonipsiphon"
workdir := filepath.Join(config.WorkDir, testdirname)
if err := config.Dependencies.RemoveAll(workdir); err != nil {
return "", err
}
if err := config.Dependencies.MkdirAll(workdir, 0700); err != nil {
return "", err
}
return workdir, nil
}
// Start starts the psiphon tunnel.
func Start(
ctx context.Context, sess Session, config Config) (*Tunnel, error) {
select {
case <-ctx.Done():
return nil, ctx.Err() // simplifies unit testing this code
default:
}
if config.Dependencies == nil {
config.Dependencies = defaultDependencies{}
}
if config.WorkDir == "" {
config.WorkDir = sess.TempDir()
}
configJSON, err := sess.FetchPsiphonConfig(ctx)
if err != nil {
return nil, err
}
workdir, err := makeworkingdir(config)
if err != nil {
return nil, err
}
start := time.Now()
tunnel, err := config.Dependencies.Start(ctx, configJSON, workdir)
if err != nil {
return nil, err
}
stop := time.Now()
return &Tunnel{tunnel: tunnel, duration: stop.Sub(start)}, nil
}
// Stop is an idempotent method that shuts down the tunnel
func (t *Tunnel) Stop() {
if t != nil {
t.tunnel.Stop()
}
}
// SOCKS5ProxyURL returns the SOCKS5 proxy URL.
func (t *Tunnel) SOCKS5ProxyURL() (proxyURL *url.URL) {
if t != nil {
proxyURL = &url.URL{
Scheme: "socks5",
Host: net.JoinHostPort(
"127.0.0.1", fmt.Sprintf("%d", t.tunnel.SOCKSProxyPort)),
}
}
return
}
// BootstrapTime returns the bootstrap time
func (t *Tunnel) BootstrapTime() (duration time.Duration) {
if t != nil {
duration = t.duration
}
return
}

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,137 +0,0 @@
// Package torx contains code to control tor.
package torx
import (
"context"
"fmt"
"net/url"
"path"
"strings"
"time"
"github.com/cretz/bine/control"
"github.com/cretz/bine/tor"
)
// Session is the way in which this package sees a Session.
type Session interface {
TempDir() string
TorArgs() []string
TorBinary() string
}
// TorProcess is a running tor process
type TorProcess interface {
Close() error
}
// Tunnel is the Tor tunnel
type Tunnel struct {
bootstrapTime time.Duration
instance TorProcess
proxy *url.URL
}
// BootstrapTime is the bootstrsap time
func (tt *Tunnel) BootstrapTime() (duration time.Duration) {
if tt != nil {
duration = tt.bootstrapTime
}
return
}
// SOCKS5ProxyURL returns the URL of the SOCKS5 proxy
func (tt *Tunnel) SOCKS5ProxyURL() (url *url.URL) {
if tt != nil {
url = tt.proxy
}
return
}
// Stop stops the Tor tunnel
func (tt *Tunnel) Stop() {
if tt != nil {
tt.instance.Close()
}
}
// StartConfig contains the configuration for StartWithConfig
type StartConfig struct {
Sess Session
Start func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error)
EnableNetwork func(ctx context.Context, tor *tor.Tor, wait bool) error
GetInfo func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error)
}
// Start starts the tor tunnel
func Start(ctx context.Context, sess Session) (*Tunnel, error) {
return StartWithConfig(ctx, StartConfig{
Sess: sess,
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return tor.Start(ctx, conf)
},
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
return tor.EnableNetwork(ctx, wait)
},
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
return ctrl.GetInfo(keys...)
},
})
}
// StartWithConfig is a configurable Start for testing
func StartWithConfig(ctx context.Context, config StartConfig) (*Tunnel, error) {
select {
case <-ctx.Done():
return nil, ctx.Err() // allows to write unit tests using this code
default:
}
logfile := LogFile(config.Sess)
extraArgs := append([]string{}, config.Sess.TorArgs()...)
extraArgs = append(extraArgs, "Log")
extraArgs = append(extraArgs, "notice stderr")
extraArgs = append(extraArgs, "Log")
extraArgs = append(extraArgs, fmt.Sprintf(`notice file %s`, logfile))
instance, err := config.Start(ctx, &tor.StartConf{
DataDir: path.Join(config.Sess.TempDir(), "tor"),
ExtraArgs: extraArgs,
ExePath: config.Sess.TorBinary(),
NoHush: true,
})
if err != nil {
return nil, err
}
instance.StopProcessOnClose = true
start := time.Now()
if err := config.EnableNetwork(ctx, instance, true); err != nil {
instance.Close()
return nil, err
}
stop := time.Now()
// Adapted from <https://git.io/Jfc7N>
info, err := config.GetInfo(instance.Control, "net/listeners/socks")
if err != nil {
instance.Close()
return nil, err
}
if len(info) != 1 || info[0].Key != "net/listeners/socks" {
instance.Close()
return nil, fmt.Errorf("unable to get socks proxy address")
}
proxyAddress := info[0].Val
if strings.HasPrefix(proxyAddress, "unix:") {
instance.Close()
return nil, fmt.Errorf("tor returned unsupported proxy")
}
return &Tunnel{
bootstrapTime: stop.Sub(start),
instance: instance,
proxy: &url.URL{Scheme: "socks5", Host: proxyAddress},
}, nil
}
// LogFile returns the name of tor logs given a specific session. The file
// is always located somewhere inside the sess.TempDir() directory.
func LogFile(sess Session) string {
return path.Join(sess.TempDir(), "tor.log")
}

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

@ -1,209 +0,0 @@
package torx_test
import (
"context"
"errors"
"net/url"
"testing"
"github.com/cretz/bine/control"
"github.com/cretz/bine/tor"
"github.com/ooni/probe-cli/v3/internal/engine/internal/mockable"
"github.com/ooni/probe-cli/v3/internal/engine/internal/torx"
)
type Closer struct {
counter int
}
func (c *Closer) Close() error {
c.counter++
return errors.New("mocked mocked mocked")
}
func TestTunnelNonNil(t *testing.T) {
closer := new(Closer)
proxy := &url.URL{Scheme: "x", Host: "10.0.0.1:443"}
tun := torx.NewTunnel(128, closer, proxy)
if tun.BootstrapTime() != 128 {
t.Fatal("not the bootstrap time we expected")
}
if tun.SOCKS5ProxyURL() != proxy {
t.Fatal("not the url we expected")
}
tun.Stop()
if closer.counter != 1 {
t.Fatal("something went wrong while stopping the tunnel")
}
}
func TestTunnelNil(t *testing.T) {
var tun *torx.Tunnel
if tun.BootstrapTime() != 0 {
t.Fatal("not the bootstrap time we expected")
}
if tun.SOCKS5ProxyURL() != nil {
t.Fatal("not the url we expected")
}
tun.Stop() // ensure we don't crash
}
func TestStartWithCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
tun, err := torx.Start(ctx, &mockable.Session{})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
if tun != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartWithConfigStartFailure(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{
Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return nil, expected
},
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tun != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartWithConfigEnableNetworkFailure(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{
Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
return expected
},
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tun != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartWithConfigGetInfoFailure(t *testing.T) {
expected := errors.New("mocked error")
ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{
Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
return nil
},
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
return nil, expected
},
})
if !errors.Is(err, expected) {
t.Fatal("not the error we expected")
}
if tun != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartWithConfigGetInfoInvalidNumberOfKeys(t *testing.T) {
ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{
Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
return nil
},
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
return nil, nil
},
})
if err.Error() != "unable to get socks proxy address" {
t.Fatal("not the error we expected")
}
if tun != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartWithConfigGetInfoInvalidKey(t *testing.T) {
ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{
Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
return nil
},
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
return []*control.KeyVal{{}}, nil
},
})
if err.Error() != "unable to get socks proxy address" {
t.Fatal("not the error we expected")
}
if tun != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestStartWithConfigGetInfoInvalidProxyType(t *testing.T) {
ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{
Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
return nil
},
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
return []*control.KeyVal{{Key: "net/listeners/socks", Val: "127.0.0.1:9050"}}, nil
},
})
if err != nil {
t.Fatal(err)
}
if tun == nil {
t.Fatal("expected non-nil tunnel here")
}
}
func TestStartWithConfigSuccess(t *testing.T) {
ctx := context.Background()
tun, err := torx.StartWithConfig(ctx, torx.StartConfig{
Sess: &mockable.Session{},
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
return &tor.Tor{}, nil
},
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
return nil
},
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
return []*control.KeyVal{{Key: "net/listeners/socks", Val: "unix:/foo/bar"}}, nil
},
})
if err.Error() != "tor returned unsupported proxy" {
t.Fatal("not the error we expected")
}
if tun != nil {
t.Fatal("expected nil tunnel here")
}
}

View file

@ -1,64 +0,0 @@
// Package tunnel contains code to create a psiphon or tor tunnel.
package tunnel
import (
"context"
"errors"
"net/url"
"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"
)
// Session is the way in which this package sees a Session.
type Session interface {
psiphonx.Session
torx.Session
Logger() model.Logger
}
// Tunnel is a tunnel used by the session
type Tunnel interface {
BootstrapTime() time.Duration
SOCKS5ProxyURL() *url.URL
Stop()
}
// Config contains config for the session tunnel.
type Config struct {
Name string
Session Session
WorkDir string
}
// Start starts a new tunnel by name or returns an error. Note that if you
// pass to this function the "" tunnel, you get back nil, nil.
func Start(ctx context.Context, config Config) (Tunnel, error) {
logger := config.Session.Logger()
switch config.Name {
case "":
logger.Debugf("no tunnel has been requested")
return enforceNilContract(nil, nil)
case "psiphon":
logger.Infof("starting %s tunnel; please be patient...", config.Name)
tun, err := psiphonx.Start(ctx, config.Session, psiphonx.Config{
WorkDir: config.WorkDir,
})
return enforceNilContract(tun, err)
case "tor":
logger.Infof("starting %s tunnel; please be patient...", config.Name)
tun, err := torx.Start(ctx, config.Session)
return enforceNilContract(tun, err)
default:
return nil, errors.New("unsupported tunnel")
}
}
func enforceNilContract(tun Tunnel, err error) (Tunnel, error) {
if err != nil {
return nil, err
}
return tun, nil
}

View file

@ -1,80 +0,0 @@
package tunnel_test
import (
"context"
"errors"
"testing"
"github.com/apex/log"
"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) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
tunnel, err := tunnel.Start(ctx, tunnel.Config{
Name: "",
Session: &mockable.Session{
MockableLogger: log.Log,
},
})
if err != nil {
t.Fatal(err)
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestPsiphonTunnel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
tunnel, err := tunnel.Start(ctx, tunnel.Config{
Name: "psiphon",
Session: &mockable.Session{
MockableLogger: log.Log,
},
})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestTorTunnel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
tunnel, err := tunnel.Start(ctx, tunnel.Config{
Name: "tor",
Session: &mockable.Session{
MockableLogger: log.Log,
},
})
if !errors.Is(err, context.Canceled) {
t.Fatal("not the error we expected")
}
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}
func TestInvalidTunnel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
tunnel, err := tunnel.Start(ctx, tunnel.Config{
Name: "antani",
Session: &mockable.Session{
MockableLogger: log.Log,
},
})
if err == nil || err.Error() != "unsupported tunnel" {
t.Fatal("not the error we expected")
}
t.Log(tunnel)
if tunnel != nil {
t.Fatal("expected nil tunnel here")
}
}