2021-04-03 19:57:21 +02:00
|
|
|
package tunnel
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-04-05 11:27:41 +02:00
|
|
|
"io"
|
2021-02-02 12:05:47 +01:00
|
|
|
"net/url"
|
2021-04-05 11:27:41 +02:00
|
|
|
"path/filepath"
|
2021-02-02 12:05:47 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cretz/bine/tor"
|
|
|
|
)
|
|
|
|
|
2021-04-05 11:27:41 +02:00
|
|
|
// torProcess is a running tor process.
|
2021-04-03 19:57:21 +02:00
|
|
|
type torProcess interface {
|
2021-04-05 11:27:41 +02:00
|
|
|
io.Closer
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 19:57:21 +02:00
|
|
|
// torTunnel is the Tor tunnel
|
|
|
|
type torTunnel struct {
|
2021-04-03 21:25:08 +02:00
|
|
|
// bootstrapTime is the duration of the bootstrap
|
2021-02-02 12:05:47 +01:00
|
|
|
bootstrapTime time.Duration
|
2021-04-03 21:25:08 +02:00
|
|
|
|
|
|
|
// instance is the running tor instance
|
|
|
|
instance torProcess
|
|
|
|
|
|
|
|
// proxy is the SOCKS5 proxy URL
|
|
|
|
proxy *url.URL
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 21:25:08 +02:00
|
|
|
// BootstrapTime returns the bootstrap time
|
2021-04-05 16:08:16 +02:00
|
|
|
func (tt *torTunnel) BootstrapTime() time.Duration {
|
|
|
|
return tt.bootstrapTime
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// SOCKS5ProxyURL returns the URL of the SOCKS5 proxy
|
2021-04-05 16:08:16 +02:00
|
|
|
func (tt *torTunnel) SOCKS5ProxyURL() *url.URL {
|
|
|
|
return tt.proxy
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the Tor tunnel
|
2021-04-03 19:57:21 +02:00
|
|
|
func (tt *torTunnel) Stop() {
|
2021-04-05 16:08:16 +02:00
|
|
|
tt.instance.Close()
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-04-05 11:27:41 +02:00
|
|
|
// TODO(bassosimone): the current design is such that we have a bunch of
|
|
|
|
// torrc-$number and a growing tor.log file inside of stateDir.
|
|
|
|
|
2021-04-03 21:25:08 +02:00
|
|
|
// torStart starts the tor tunnel.
|
|
|
|
func torStart(ctx context.Context, config *Config) (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:
|
|
|
|
}
|
2021-04-05 11:27:41 +02:00
|
|
|
if config.TunnelDir == "" {
|
|
|
|
return nil, ErrEmptyTunnelDir
|
|
|
|
}
|
|
|
|
stateDir := filepath.Join(config.TunnelDir, "tor")
|
|
|
|
logfile := filepath.Join(stateDir, "tor.log")
|
2021-04-04 12:08:13 +02:00
|
|
|
extraArgs := append([]string{}, config.TorArgs...)
|
2021-02-02 12:05:47 +01:00
|
|
|
extraArgs = append(extraArgs, "Log")
|
|
|
|
extraArgs = append(extraArgs, "notice stderr")
|
|
|
|
extraArgs = append(extraArgs, "Log")
|
|
|
|
extraArgs = append(extraArgs, fmt.Sprintf(`notice file %s`, logfile))
|
2021-04-03 21:25:08 +02:00
|
|
|
instance, err := config.torStart(ctx, &tor.StartConf{
|
2021-04-05 11:27:41 +02:00
|
|
|
DataDir: stateDir,
|
2021-02-02 12:05:47 +01:00
|
|
|
ExtraArgs: extraArgs,
|
2021-04-04 12:08:13 +02:00
|
|
|
ExePath: config.TorBinary,
|
2021-02-02 12:05:47 +01:00
|
|
|
NoHush: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
instance.StopProcessOnClose = true
|
|
|
|
start := time.Now()
|
2021-04-03 21:25:08 +02:00
|
|
|
if err := config.torEnableNetwork(ctx, instance, true); err != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
instance.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stop := time.Now()
|
|
|
|
// Adapted from <https://git.io/Jfc7N>
|
2021-04-03 21:25:08 +02:00
|
|
|
info, err := config.torGetInfo(instance.Control, "net/listeners/socks")
|
2021-02-02 12:05:47 +01:00
|
|
|
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
|
|
|
|
}
|