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
parent 47aa773731
commit 8fe4e5410d
17 changed files with 166 additions and 105 deletions
+20 -1
View File
@@ -2,8 +2,10 @@ package urlgetter
import (
"context"
"fmt"
"net/url"
"path/filepath"
"sync"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/model"
@@ -80,6 +82,15 @@ func (g Getter) Get(ctx context.Context) (TestKeys, error) {
return tk, err
}
var (
// tunnelDirCount counts the number of tunnels started by
// the urlgetter package so far.
tunnelDirCount int64
// tunnelDirMu protects tunnelDirCount
tunnelDirMu sync.Mutex
)
func (g Getter) get(ctx context.Context, saver *trace.Saver) (TestKeys, error) {
tk := TestKeys{
Agent: "redirect",
@@ -94,12 +105,20 @@ func (g Getter) get(ctx context.Context, saver *trace.Saver) (TestKeys, error) {
// start tunnel
var proxyURL *url.URL
if g.Config.Tunnel != "" {
// Every new instance of the tunnel goes into a separate
// directory within the temporary directory. Calling
// Session.Close will delete such a directory.
tunnelDirMu.Lock()
count := tunnelDirCount
tunnelDirCount++
tunnelDirMu.Unlock()
tun, err := tunnel.Start(ctx, &tunnel.Config{
Name: g.Config.Tunnel,
Session: g.Session,
TorArgs: g.Session.TorArgs(),
TorBinary: g.Session.TorBinary(),
WorkDir: filepath.Join(g.Session.TempDir(), "urlgetter-tunnel"),
TunnelDir: filepath.Join(
g.Session.TempDir(), fmt.Sprintf("urlgetter-tunnel-%d", count)),
})
if err != nil {
return tk, err