Merge pull request 'Move DataDir Caching Responsibility to Caller to allow Cached Consensus' (#27) from getinfo into master
continuous-integration/drone/push Build is pending Details
continuous-integration/drone/tag Build is pending Details

Reviewed-on: #27
This commit is contained in:
Dan Ballard 2022-01-18 20:46:56 +00:00
commit 3d36e7d4a5
3 changed files with 25 additions and 21 deletions

View File

@ -3,6 +3,7 @@ package testing
import ( import (
"git.openprivacy.ca/openprivacy/connectivity/tor" "git.openprivacy.ca/openprivacy/connectivity/tor"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path" "path"
@ -25,10 +26,15 @@ func TestLaunchTor(t *testing.T) {
t.Fatalf("failed to create torrc file: %v", err) t.Fatalf("failed to create torrc file: %v", err)
} }
dataDir := ""
if dataDir, err = ioutil.TempDir(path.Join("..", "testing"), "data-dir-"); err != nil {
t.Fatalf("could not create data dir")
}
// Get the current working director, clean the paths to remove relative references // Get the current working director, clean the paths to remove relative references
wd, _ := os.Getwd() wd, _ := os.Getwd()
t.Logf("Launching bundled tor at %v", path.Clean(wd+"/../tmp/tor")) t.Logf("Launching bundled tor at %v", path.Clean(wd+"/../tmp/tor"))
acn, err := tor.NewTorACNWithAuth(path.Clean(wd+"/../tmp/data"), path.Clean(wd+"/../tmp/tor"), controlPort, tor.HashedPasswordAuthenticator{Password: password}) acn, err := tor.NewTorACNWithAuth(path.Clean(wd+"/../tmp/data"), path.Clean(wd+"/../tmp/tor"), dataDir, controlPort, tor.HashedPasswordAuthenticator{Password: password})
if err != nil { if err != nil {
t.Fatalf("tor failed to start: %v", err) t.Fatalf("tor failed to start: %v", err)
} else { } else {

View File

@ -12,7 +12,6 @@ import (
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"golang.org/x/crypto/ed25519" "golang.org/x/crypto/ed25519"
"golang.org/x/crypto/sha3" "golang.org/x/crypto/sha3"
"io/ioutil"
"net" "net"
"net/textproto" "net/textproto"
"os" "os"
@ -73,6 +72,7 @@ type torProvider struct {
lastRestartTime time.Time lastRestartTime time.Time
authenticator tor.Authenticator authenticator tor.Authenticator
isClosed bool isClosed bool
dataDir string
} }
func (ols *onionListenService) AddressFull() string { func (ols *onionListenService) AddressFull() string {
@ -325,7 +325,7 @@ func (tp *torProvider) restart() {
tp.t = nil tp.t = nil
log.Debugf("Restarting Tor Process") log.Debugf("Restarting Tor Process")
newTp, err := startTor(tp.appDirectory, tp.bundeledTorPath, tp.controlPort, tp.authenticator) newTp, err := startTor(tp.appDirectory, tp.bundeledTorPath, tp.dataDir, tp.controlPort, tp.authenticator)
if err == nil { if err == nil {
// we need to reassign tor, dialer and callback which will have changed by swapping out // we need to reassign tor, dialer and callback which will have changed by swapping out
// the underlying connection. // the underlying connection.
@ -401,19 +401,14 @@ func (tp *torProvider) callStatusCallback(prog int, status string) {
} }
// NewTorACNWithAuth creates/starts a Tor ACN and returns a usable ACN object // NewTorACNWithAuth creates/starts a Tor ACN and returns a usable ACN object
func NewTorACNWithAuth(appDirectory string, bundledTorPath string, controlPort int, authenticator tor.Authenticator) (connectivity.ACN, error) { func NewTorACNWithAuth(appDirectory string, bundledTorPath string, dataDir string, controlPort int, authenticator tor.Authenticator) (connectivity.ACN, error) {
tp, err := startTor(appDirectory, bundledTorPath, controlPort, authenticator) tp, err := startTor(appDirectory, bundledTorPath, dataDir, controlPort, authenticator)
if err == nil { if err == nil {
go tp.monitorRestart() go tp.monitorRestart()
} }
return tp, err return tp, err
} }
// NewTorACN creates/starts a Tor ACN and returns a usable ACN object with a NullAuthenticator - this will fail.
func NewTorACN(appDirectory string, bundledTorPath string) (connectivity.ACN, error) {
return NewTorACNWithAuth(appDirectory, bundledTorPath, 9051, NullAuthenticator{})
}
// newHideCmd creates a Creator function for bine which generates a cmd that one windows will hide the dosbox // newHideCmd creates a Creator function for bine which generates a cmd that one windows will hide the dosbox
func newHideCmd(exePath string) process.Creator { func newHideCmd(exePath string) process.Creator {
return process.CmdCreatorFunc(func(ctx context.Context, args ...string) (*exec.Cmd, error) { return process.CmdCreatorFunc(func(ctx context.Context, args ...string) (*exec.Cmd, error) {
@ -450,15 +445,10 @@ func (tp *torProvider) checkVersion() error {
return err return err
} }
func startTor(appDirectory string, bundledTorPath string, controlPort int, authenticator tor.Authenticator) (*torProvider, error) { func startTor(appDirectory string, bundledTorPath string, dataDir string, controlPort int, authenticator tor.Authenticator) (*torProvider, error) {
torDir := path.Join(appDirectory, "tor") torDir := path.Join(appDirectory, "tor")
os.MkdirAll(torDir, 0700) os.MkdirAll(torDir, 0700)
dataDir := ""
var err error
if dataDir, err = ioutil.TempDir(torDir, "data-dir-"); err != nil {
return nil, fmt.Errorf("unable to create temp data dir: %v", err)
}
tp := &torProvider{authenticator: authenticator, controlPort: controlPort, appDirectory: appDirectory, bundeledTorPath: bundledTorPath, childListeners: make(map[string]*onionListenService), breakChan: make(chan bool), statusCallback: nil, lastRestartTime: time.Now().Add(-restartCooldown)} tp := &torProvider{authenticator: authenticator, controlPort: controlPort, appDirectory: appDirectory, bundeledTorPath: bundledTorPath, childListeners: make(map[string]*onionListenService), breakChan: make(chan bool), statusCallback: nil, lastRestartTime: time.Now().Add(-restartCooldown)}
@ -502,9 +492,9 @@ func startTor(appDirectory string, bundledTorPath string, controlPort int, authe
return nil, fmt.Errorf("could not connect to or start Tor that met requirements (min Tor version 0.3.5.x)") return nil, fmt.Errorf("could not connect to or start Tor that met requirements (min Tor version 0.3.5.x)")
} }
err = tp.checkVersion() err := tp.checkVersion()
if err == nil { if err == nil {
tp.t.DeleteDataDirOnClose = true tp.t.DeleteDataDirOnClose = false // caller is repsonsible for dealing with cached information...
tp.dialer, err = tp.t.Dialer(context.TODO(), &tor.DialConf{Authenticator: tp.authenticator}) tp.dialer, err = tp.t.Dialer(context.TODO(), &tor.DialConf{Authenticator: tp.authenticator})
return tp, err return tp, err
} }
@ -565,7 +555,7 @@ func createFromExisting(controlport *control.Conn, datadir string) *tor.Tor {
Control: controlport, Control: controlport,
ProcessCancelFunc: nil, ProcessCancelFunc: nil,
DataDir: datadir, DataDir: datadir,
DeleteDataDirOnClose: true, DeleteDataDirOnClose: false,
DebugWriter: nil, DebugWriter: nil,
StopProcessOnClose: false, StopProcessOnClose: false,
GeoIPCreatedFile: "", GeoIPCreatedFile: "",

View File

@ -3,6 +3,7 @@ package tor
import ( import (
"fmt" "fmt"
"git.openprivacy.ca/openprivacy/log" "git.openprivacy.ca/openprivacy/log"
"io/ioutil"
path "path/filepath" path "path/filepath"
"testing" "testing"
) )
@ -22,7 +23,14 @@ func TestTorProvider(t *testing.T) {
NewTorrc().WithControlPort(9051).WithHashedPassword("examplehashedpassword").Build(path.Join("..", "testing", "tor", "torrc")) NewTorrc().WithControlPort(9051).WithHashedPassword("examplehashedpassword").Build(path.Join("..", "testing", "tor", "torrc"))
log.Debugf("setting tor path %v", torpath) log.Debugf("setting tor path %v", torpath)
acn, err := NewTorACNWithAuth(path.Join("../testing/"), torpath, 9051, HashedPasswordAuthenticator{"examplehashedpassword"})
dataDir := ""
var err error
if dataDir, err = ioutil.TempDir(path.Join("..", "testing"), "data-dir-"); err != nil {
t.Fatalf("could not create data dir")
}
acn, err := NewTorACNWithAuth(path.Join("../testing/"), torpath, dataDir, 9051, HashedPasswordAuthenticator{"examplehashedpassword"})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -58,7 +66,7 @@ func TestTorProvider(t *testing.T) {
_, err = acn.GetInfo("not_a_real_onion") _, err = acn.GetInfo("not_a_real_onion")
if err == nil { if err == nil {
t.Fatalf("GetInfo for non existant onion should have errored") t.Fatalf("GetInfo for non existent onion should have errored")
} }
} else { } else {