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:
parent
47aa773731
commit
8fe4e5410d
17 changed files with 166 additions and 105 deletions
|
|
@ -5,6 +5,7 @@ package tunnel
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -12,16 +13,28 @@ import (
|
|||
// Session is the way in which this package sees a Session.
|
||||
type Session interface {
|
||||
FetchPsiphonConfig(ctx context.Context) ([]byte, error)
|
||||
TempDir() string
|
||||
}
|
||||
|
||||
// Tunnel is a tunnel used by the session
|
||||
type Tunnel interface {
|
||||
// BootstrapTime returns the time it required to
|
||||
// create an instance of the tunnel
|
||||
BootstrapTime() time.Duration
|
||||
|
||||
// SOCKS5ProxyURL returns the SOCSK5 proxy URL
|
||||
SOCKS5ProxyURL() *url.URL
|
||||
|
||||
// Stop stops the tunnel. This method is idempotent.
|
||||
Stop()
|
||||
}
|
||||
|
||||
// ErrEmptyTunnelDir indicates that config.TunnelDir is empty.
|
||||
var ErrEmptyTunnelDir = errors.New("TunnelDir is empty")
|
||||
|
||||
// ErrUnsupportedTunnelName indicates that the given tunnel name
|
||||
// is not supported by this package.
|
||||
var ErrUnsupportedTunnelName = errors.New("unsupported tunnel name")
|
||||
|
||||
// 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) {
|
||||
|
|
@ -35,11 +48,15 @@ func Start(ctx context.Context, config *Config) (Tunnel, error) {
|
|||
tun, err := torStart(ctx, config)
|
||||
return enforceNilContract(tun, err)
|
||||
default:
|
||||
return nil, errors.New("unsupported tunnel")
|
||||
return nil, fmt.Errorf("%w: %s", ErrUnsupportedTunnelName, config.Name)
|
||||
}
|
||||
}
|
||||
|
||||
// enforceNilContract ensures that either the tunnel is nil
|
||||
// or the error is nil.
|
||||
func enforceNilContract(tun Tunnel, err error) (Tunnel, error) {
|
||||
// TODO(bassosimone): we currently allow returning nil, nil but
|
||||
// we want to change this to return a fake NilTunnel.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue