add versionCallback and use on reboot tor; change start preference for bundled tor; new go 1.17 build directives

This commit is contained in:
Dan Ballard 2022-07-31 08:11:30 -07:00
parent b280f7a1fe
commit 9d4e1e5ca5
9 changed files with 48 additions and 13 deletions

2
acn.go
View File

@ -38,6 +38,8 @@ type ACN interface {
WaitTillBootstrapped() error
// Sets the callback function to be called when ACN status changes
SetStatusCallback(callback func(int, string))
// Sets the callback function to be called when ACN reboots to emit the version
SetVersionCallback(callback func(string))
// Restarts the underlying connection
Restart()

View File

@ -11,7 +11,8 @@ const acnError = "error initializing anonymous communication network"
// ErrorACN - a status-callback safe errored ACN. Use this when ACN construction goes wrong
// and you need a safe substitute that can later be replaced with a working ACN without impacting calling clients.
type ErrorACN struct {
statusCallbackCache func(int, string)
statusCallbackCache func(int, string)
versionCallbackCache func(string)
}
func (e ErrorACN) Callback() func(int, string) {
@ -34,6 +35,10 @@ func (e *ErrorACN) SetStatusCallback(callback func(int, string)) {
e.statusCallbackCache = callback
}
func (e *ErrorACN) SetVersionCallback(callback func(string)) {
e.versionCallbackCache = callback
}
func (e ErrorACN) Restart() {
}

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.13
require (
filippo.io/edwards25519 v1.0.0-rc.1
git.openprivacy.ca/openprivacy/bine v0.0.4
git.openprivacy.ca/openprivacy/log v1.0.2
git.openprivacy.ca/openprivacy/log v1.0.3
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
)

2
go.sum
View File

@ -4,6 +4,8 @@ git.openprivacy.ca/openprivacy/bine v0.0.4 h1:CO7EkGyz+jegZ4ap8g5NWRuDHA/56KKvGy
git.openprivacy.ca/openprivacy/bine v0.0.4/go.mod h1:13ZqhKyqakDsN/ZkQkIGNULsmLyqtXc46XBcnuXm/mU=
git.openprivacy.ca/openprivacy/log v1.0.2 h1:HLP4wsw4ljczFAelYnbObIs821z+jgMPCe8uODPnGQM=
git.openprivacy.ca/openprivacy/log v1.0.2/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw=
git.openprivacy.ca/openprivacy/log v1.0.3 h1:E/PMm4LY+Q9s3aDpfySfEDq/vYQontlvNj/scrPaga0=
git.openprivacy.ca/openprivacy/log v1.0.3/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

View File

@ -51,6 +51,10 @@ func (lp *localProvider) SetStatusCallback(callback func(int, string)) {
// nop
}
func (lp *localProvider) SetVersionCallback(callback func(string)) {
// nop
}
func (lp *localProvider) GetPID() (int, error) {
return 0, nil
}

View File

@ -49,6 +49,10 @@ func (p *ProxyACN) SetStatusCallback(callback func(int, string)) {
p.acn.SetStatusCallback(callback)
}
func (p *ProxyACN) SetVersionCallback(callback func(string)) {
p.acn.SetVersionCallback(callback)
}
func (p *ProxyACN) Restart() {
p.acn.Restart()
}

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows
package tor

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows
package tor

View File

@ -70,6 +70,7 @@ type torProvider struct {
breakChan chan bool
childListeners map[string]*onionListenService
statusCallback func(int, string)
versionCallback func(string)
lastRestartTime time.Time
authenticator tor.Authenticator
isClosed bool
@ -391,6 +392,12 @@ func (tp *torProvider) SetStatusCallback(callback func(int, string)) {
tp.statusCallback = callback
}
func (tp *torProvider) SetVersionCallback(callback func(string)) {
tp.lock.Lock()
defer tp.lock.Unlock()
tp.versionCallback = callback
}
func (tp *torProvider) Callback() func(int, string) {
tp.lock.Lock()
defer tp.lock.Unlock()
@ -399,10 +406,18 @@ func (tp *torProvider) Callback() func(int, string) {
func (tp *torProvider) callStatusCallback(prog int, status string) {
tp.lock.Lock()
defer tp.lock.Unlock()
if tp.statusCallback != nil {
tp.statusCallback(prog, status)
}
tp.lock.Unlock()
}
func (tp *torProvider) callVersionCallback(version string) {
tp.lock.Lock()
defer tp.lock.Unlock()
if tp.versionCallback != nil {
tp.versionCallback(version)
}
}
// NewTorACNWithAuth creates/starts a Tor ACN and returns a usable ACN object
@ -476,16 +491,9 @@ func startTor(appDirectory string, bundledTorPath string, dataDir string, contro
return nil, err
}
// if not, try running system tor
log.Debugln("checking if we can run system installed tor or bundled tor")
if checkCmdlineTorVersion("tor") {
t, err := tor.Start(context.TODO(), &tor.StartConf{ControlPort: tp.controlPort, DisableCookieAuth: true, UseEmbeddedControlConn: false, DisableEagerAuth: true, EnableNetwork: true, DataDir: dataDir, TorrcFile: path.Join(torDir, "torrc"), DebugWriter: nil, ProcessCreator: newHideCmd("tor")})
if err != nil {
log.Debugf("Error connecting to self-run system tor: %v\n", err)
return nil, err
}
tp.t = t
} else if bundledTorPath != "" && checkCmdlineTorVersion(bundledTorPath) {
// if not, try bundled tor, then running system tor
log.Debugln("checking if we can run bundled tor or system installed tor")
if bundledTorPath != "" && checkCmdlineTorVersion(bundledTorPath) {
log.Debugln("bundled tor appears viable, attempting to use '" + bundledTorPath + "'")
t, err := tor.Start(context.TODO(), &tor.StartConf{ControlPort: tp.controlPort, DisableCookieAuth: true, UseEmbeddedControlConn: false, DisableEagerAuth: true, EnableNetwork: true, DataDir: dataDir, TorrcFile: path.Join(torDir, "torrc"), ExePath: bundledTorPath, DebugWriter: nil, ProcessCreator: newHideCmd(bundledTorPath)})
if err != nil {
@ -493,6 +501,13 @@ func startTor(appDirectory string, bundledTorPath string, dataDir string, contro
return nil, err
}
tp.t = t
} else if checkCmdlineTorVersion("tor") {
t, err := tor.Start(context.TODO(), &tor.StartConf{ControlPort: tp.controlPort, DisableCookieAuth: true, UseEmbeddedControlConn: false, DisableEagerAuth: true, EnableNetwork: true, DataDir: dataDir, TorrcFile: path.Join(torDir, "torrc"), DebugWriter: nil, ProcessCreator: newHideCmd("tor")})
if err != nil {
log.Debugf("Error connecting to self-run system tor: %v\n", err)
return nil, err
}
tp.t = t
} else {
log.Debugln("Could not find a viable tor running or to run")
return nil, fmt.Errorf("could not connect to or start Tor that met requirements (min Tor version 0.3.5.x)")
@ -502,6 +517,7 @@ func startTor(appDirectory string, bundledTorPath string, dataDir string, contro
if err == nil {
tp.t.DeleteDataDirOnClose = false // caller is responsible for dealing with cached information...
tp.dialer, err = tp.t.Dialer(context.TODO(), &tor.DialConf{Authenticator: tp.authenticator})
tp.callVersionCallback(tp.GetVersion())
return tp, err
}
return nil, fmt.Errorf("could not connect to running tor: %v", err)