feat(tunnel): introduce persistent tunnel state dir (#294)

* feat(tunnel): introduce persistent tunnel state dir

This diff introduces a persistent state directory for tunnels, so that
we can bootstrap them more quickly after the first time.

Part of https://github.com/ooni/probe/issues/985

* fix: make tunnel dir optional

We have many tests where it does not make sense to explicitly
provide a tunnel dir because we're not using tunnels.

This should simplify setting up a session.

* fix(tunnel): repair tests

* final changes

* more cleanups
This commit is contained in:
Simone Basso 2021-04-05 11:27:41 +02:00 committed by GitHub
commit 8fe4e5410d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 166 additions and 105 deletions

View file

@ -13,29 +13,16 @@ import (
// psiphonTunnel is a psiphon tunnel
type psiphonTunnel struct {
// bootstrapTime is the bootstrapTime of the bootstrap
bootstrapTime time.Duration
// tunnel is the underlying psiphon tunnel
tunnel *clientlib.PsiphonTunnel
// duration is the duration of the bootstrap
duration time.Duration
}
// TODO(bassosimone): _always_ wiping the state directory
// here is absolutely wrong. This prevents us from reusing
// an existing psiphon cache existing on disk. We want to
// delete the directory _only_ in the psiphon nettest.
// psiphonMakeWorkingDir creates the working directory
func psiphonMakeWorkingDir(config *Config) (string, error) {
const testdirname = "oonipsiphon"
baseWorkDir := config.WorkDir
if baseWorkDir == "" {
baseWorkDir = config.Session.TempDir()
}
workdir := filepath.Join(baseWorkDir, testdirname)
if err := config.removeAll(workdir); err != nil {
return "", err
}
workdir := filepath.Join(config.TunnelDir, config.Name)
if err := config.mkdirAll(workdir, 0700); err != nil {
return "", err
}
@ -49,6 +36,9 @@ func psiphonStart(ctx context.Context, config *Config) (Tunnel, error) {
return nil, ctx.Err() // simplifies unit testing this code
default:
}
if config.TunnelDir == "" {
return nil, ErrEmptyTunnelDir
}
configJSON, err := config.Session.FetchPsiphonConfig(ctx)
if err != nil {
return nil, err
@ -63,7 +53,7 @@ func psiphonStart(ctx context.Context, config *Config) (Tunnel, error) {
return nil, err
}
stop := time.Now()
return &psiphonTunnel{tunnel: tunnel, duration: stop.Sub(start)}, nil
return &psiphonTunnel{tunnel: tunnel, bootstrapTime: stop.Sub(start)}, nil
}
// TODO(bassosimone): define the NullTunnel rather than relying on
@ -91,7 +81,7 @@ func (t *psiphonTunnel) SOCKS5ProxyURL() (proxyURL *url.URL) {
// BootstrapTime returns the bootstrap time
func (t *psiphonTunnel) BootstrapTime() (duration time.Duration) {
if t != nil {
duration = t.duration
duration = t.bootstrapTime
}
return
}