2021-04-03 19:57:21 +02:00
|
|
|
package tunnel
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cretz/bine/control"
|
|
|
|
"github.com/cretz/bine/tor"
|
|
|
|
)
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
// torProcess is a running tor process
|
|
|
|
type torProcess interface {
|
2021-02-02 12:05:47 +01:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
// torTunnel is the Tor tunnel
|
|
|
|
type torTunnel struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
bootstrapTime time.Duration
|
2021-04-03 19:57:21 +02:00
|
|
|
instance torProcess
|
2021-02-02 12:05:47 +01:00
|
|
|
proxy *url.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
// BootstrapTime is the bootstrsap time
|
2021-04-03 19:57:21 +02:00
|
|
|
func (tt *torTunnel) BootstrapTime() (duration time.Duration) {
|
2021-02-02 12:05:47 +01:00
|
|
|
if tt != nil {
|
|
|
|
duration = tt.bootstrapTime
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// SOCKS5ProxyURL returns the URL of the SOCKS5 proxy
|
2021-04-03 19:57:21 +02:00
|
|
|
func (tt *torTunnel) SOCKS5ProxyURL() (url *url.URL) {
|
2021-02-02 12:05:47 +01:00
|
|
|
if tt != nil {
|
|
|
|
url = tt.proxy
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the Tor tunnel
|
2021-04-03 19:57:21 +02:00
|
|
|
func (tt *torTunnel) Stop() {
|
2021-02-02 12:05:47 +01:00
|
|
|
if tt != nil {
|
|
|
|
tt.instance.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
// torStartConfig contains the configuration for StartWithConfig
|
|
|
|
type torStartConfig struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess Session
|
|
|
|
Start func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error)
|
|
|
|
EnableNetwork func(ctx context.Context, tor *tor.Tor, wait bool) error
|
|
|
|
GetInfo func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error)
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
// torStart starts the tor tunnel
|
|
|
|
func torStart(ctx context.Context, sess Session) (Tunnel, error) {
|
|
|
|
return torStartWithConfig(ctx, torStartConfig{
|
2021-02-02 12:05:47 +01:00
|
|
|
Sess: sess,
|
|
|
|
Start: func(ctx context.Context, conf *tor.StartConf) (*tor.Tor, error) {
|
|
|
|
return tor.Start(ctx, conf)
|
|
|
|
},
|
|
|
|
EnableNetwork: func(ctx context.Context, tor *tor.Tor, wait bool) error {
|
|
|
|
return tor.EnableNetwork(ctx, wait)
|
|
|
|
},
|
|
|
|
GetInfo: func(ctrl *control.Conn, keys ...string) ([]*control.KeyVal, error) {
|
|
|
|
return ctrl.GetInfo(keys...)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
// torStartWithConfig is a configurable torStart for testing
|
|
|
|
func torStartWithConfig(ctx context.Context, config torStartConfig) (Tunnel, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err() // allows to write unit tests using this code
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
logfile := LogFile(config.Sess)
|
|
|
|
extraArgs := append([]string{}, config.Sess.TorArgs()...)
|
|
|
|
extraArgs = append(extraArgs, "Log")
|
|
|
|
extraArgs = append(extraArgs, "notice stderr")
|
|
|
|
extraArgs = append(extraArgs, "Log")
|
|
|
|
extraArgs = append(extraArgs, fmt.Sprintf(`notice file %s`, logfile))
|
|
|
|
instance, err := config.Start(ctx, &tor.StartConf{
|
|
|
|
DataDir: path.Join(config.Sess.TempDir(), "tor"),
|
|
|
|
ExtraArgs: extraArgs,
|
|
|
|
ExePath: config.Sess.TorBinary(),
|
|
|
|
NoHush: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
instance.StopProcessOnClose = true
|
|
|
|
start := time.Now()
|
|
|
|
if err := config.EnableNetwork(ctx, instance, true); err != nil {
|
|
|
|
instance.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stop := time.Now()
|
|
|
|
// Adapted from <https://git.io/Jfc7N>
|
|
|
|
info, err := config.GetInfo(instance.Control, "net/listeners/socks")
|
|
|
|
if err != nil {
|
|
|
|
instance.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(info) != 1 || info[0].Key != "net/listeners/socks" {
|
|
|
|
instance.Close()
|
|
|
|
return nil, fmt.Errorf("unable to get socks proxy address")
|
|
|
|
}
|
|
|
|
proxyAddress := info[0].Val
|
|
|
|
if strings.HasPrefix(proxyAddress, "unix:") {
|
|
|
|
instance.Close()
|
|
|
|
return nil, fmt.Errorf("tor returned unsupported proxy")
|
|
|
|
}
|
2021-04-03 19:57:21 +02:00
|
|
|
return &torTunnel{
|
2021-02-02 12:05:47 +01:00
|
|
|
bootstrapTime: stop.Sub(start),
|
|
|
|
instance: instance,
|
|
|
|
proxy: &url.URL{Scheme: "socks5", Host: proxyAddress},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogFile returns the name of tor logs given a specific session. The file
|
|
|
|
// is always located somewhere inside the sess.TempDir() directory.
|
|
|
|
func LogFile(sess Session) string {
|
|
|
|
return path.Join(sess.TempDir(), "tor.log")
|
|
|
|
}
|
2021-04-03 19:57:21 +02:00
|
|
|
|
|
|
|
// newTorTunnel creates a new torTunnel
|
|
|
|
func newTorTunnel(bootstrapTime time.Duration, instance torProcess, proxy *url.URL) *torTunnel {
|
|
|
|
return &torTunnel{
|
|
|
|
bootstrapTime: bootstrapTime,
|
|
|
|
instance: instance,
|
|
|
|
proxy: proxy,
|
|
|
|
}
|
|
|
|
}
|