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

@ -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
}