refactor(tunnel): simplify psiphon implementation (#289)

Simplify interaction within the package by avoiding to have
a psiphon specific config. Use a Config instead.

Part of https://github.com/ooni/probe/issues/985.
This commit is contained in:
Simone Basso 2021-04-03 21:09:34 +02:00 committed by GitHub
commit f739450370
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 99 deletions

View file

@ -5,81 +5,41 @@ import (
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"time"
"github.com/ooni/psiphon/oopsi/github.com/Psiphon-Labs/psiphon-tunnel-core/ClientLibrary/clientlib"
)
// psiphonDependencies contains dependencies for psiphonStart
type psiphonDependencies 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)
}
// psiphonConfig contains the settings for psiphonStart. The empty config object implies
// that we will be using default settings for starting the tunnel.
type psiphonConfig struct {
// Dependencies contains dependencies for Start.
Dependencies psiphonDependencies
// WorkDir is the directory where Psiphon should store
// its configuration database.
WorkDir string
}
// psiphonTunnel is a psiphon tunnel
type psiphonTunnel struct {
tunnel *clientlib.PsiphonTunnel
duration time.Duration
}
func makeworkingdir(config psiphonConfig) (string, error) {
func makeworkingdir(config *Config) (string, error) {
const testdirname = "oonipsiphon"
workdir := filepath.Join(config.WorkDir, testdirname)
if err := config.Dependencies.RemoveAll(workdir); err != nil {
if err := config.removeAll(workdir); err != nil {
return "", err
}
if err := config.Dependencies.MkdirAll(workdir, 0700); err != nil {
if err := config.mkdirAll(workdir, 0700); err != nil {
return "", err
}
return workdir, nil
}
// psiphonStart starts the psiphon tunnel.
func psiphonStart(
ctx context.Context, sess Session, config psiphonConfig) (Tunnel, error) {
func psiphonStart(ctx context.Context, 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()
config.WorkDir = config.Session.TempDir()
}
configJSON, err := sess.FetchPsiphonConfig(ctx)
configJSON, err := config.Session.FetchPsiphonConfig(ctx)
if err != nil {
return nil, err
}
@ -88,7 +48,7 @@ func psiphonStart(
return nil, err
}
start := time.Now()
tunnel, err := config.Dependencies.Start(ctx, configJSON, workdir)
tunnel, err := config.startPsiphon(ctx, configJSON, workdir)
if err != nil {
return nil, err
}
@ -96,6 +56,9 @@ func psiphonStart(
return &psiphonTunnel{tunnel: tunnel, duration: stop.Sub(start)}, nil
}
// TODO(bassosimone): define the NullTunnel rather than relying on
// this magic that a nil psiphonTunnel works.
// Stop is an idempotent method that shuts down the tunnel
func (t *psiphonTunnel) Stop() {
if t != nil {