2021-04-03 19:57:21 +02:00
|
|
|
// Package tunnel allows to create tunnels to speak
|
|
|
|
// with OONI backends and other services.
|
2021-02-02 12:05:47 +01:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-04-05 11:27:41 +02:00
|
|
|
"fmt"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Session is the way in which this package sees a Session.
|
|
|
|
type Session interface {
|
2021-04-03 19:57:21 +02:00
|
|
|
FetchPsiphonConfig(ctx context.Context) ([]byte, error)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tunnel is a tunnel used by the session
|
|
|
|
type Tunnel interface {
|
2021-04-05 11:27:41 +02:00
|
|
|
// BootstrapTime returns the time it required to
|
|
|
|
// create an instance of the tunnel
|
2021-02-02 12:05:47 +01:00
|
|
|
BootstrapTime() time.Duration
|
2021-04-05 11:27:41 +02:00
|
|
|
|
|
|
|
// SOCKS5ProxyURL returns the SOCSK5 proxy URL
|
2021-02-02 12:05:47 +01:00
|
|
|
SOCKS5ProxyURL() *url.URL
|
2021-04-05 11:27:41 +02:00
|
|
|
|
|
|
|
// Stop stops the tunnel. This method is idempotent.
|
2021-02-02 12:05:47 +01:00
|
|
|
Stop()
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:27:41 +02:00
|
|
|
// 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")
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// 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.
|
2021-04-03 20:12:56 +02:00
|
|
|
func Start(ctx context.Context, config *Config) (Tunnel, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
switch config.Name {
|
|
|
|
case "":
|
|
|
|
return enforceNilContract(nil, nil)
|
|
|
|
case "psiphon":
|
2021-04-03 21:09:34 +02:00
|
|
|
tun, err := psiphonStart(ctx, config)
|
2021-02-02 12:05:47 +01:00
|
|
|
return enforceNilContract(tun, err)
|
|
|
|
case "tor":
|
2021-04-03 21:25:08 +02:00
|
|
|
tun, err := torStart(ctx, config)
|
2021-02-02 12:05:47 +01:00
|
|
|
return enforceNilContract(tun, err)
|
|
|
|
default:
|
2021-04-05 11:27:41 +02:00
|
|
|
return nil, fmt.Errorf("%w: %s", ErrUnsupportedTunnelName, config.Name)
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:27:41 +02:00
|
|
|
// enforceNilContract ensures that either the tunnel is nil
|
|
|
|
// or the error is nil.
|
2021-02-02 12:05:47 +01:00
|
|
|
func enforceNilContract(tun Tunnel, err error) (Tunnel, error) {
|
2021-04-05 11:27:41 +02:00
|
|
|
// TODO(bassosimone): we currently allow returning nil, nil but
|
|
|
|
// we want to change this to return a fake NilTunnel.
|
2021-02-02 12:05:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tun, nil
|
|
|
|
}
|