Require error to construct an ErrorACN
continuous-integration/drone/pr Build is passing Details
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Sarah Jamie Lewis 2023-05-29 10:22:12 -07:00
parent dedcbdd3cb
commit 9beff8a10a
2 changed files with 25 additions and 19 deletions

View File

@ -1,20 +1,25 @@
package connectivity
import (
"errors"
"fmt"
"net"
)
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 {
acnError error
statusCallbackCache func(int, string)
versionCallbackCache func(string)
}
func NewErrorACN(err error) ErrorACN {
return ErrorACN{
acnError: err,
statusCallbackCache: func(int, string) {},
versionCallbackCache: func(string) {},
}
}
func (e *ErrorACN) GetStatusCallback() func(int, string) {
return e.statusCallbackCache
}
@ -24,15 +29,15 @@ func (e *ErrorACN) GetVersionCallback() func(string) {
}
func (e *ErrorACN) GetInfo(addr string) (map[string]string, error) {
return nil, errors.New(acnError)
return nil, e.acnError
}
func (e *ErrorACN) GetBootstrapStatus() (int, string) {
return -1, acnError
return -1, e.acnError.Error()
}
func (e *ErrorACN) WaitTillBootstrapped() error {
return errors.New(acnError)
return e.acnError
}
func (e *ErrorACN) SetStatusCallback(callback func(int, string)) {
@ -47,20 +52,21 @@ func (e *ErrorACN) Restart() {
}
func (e *ErrorACN) Open(hostname string) (net.Conn, string, error) {
return nil, "", fmt.Errorf(acnError)
return nil, "", e.acnError
}
func (e *ErrorACN) Listen(identity PrivateKey, port int) (ListenService, error) {
return nil, fmt.Errorf(acnError)
return nil, e.acnError
}
func (e *ErrorACN) GetPID() (int, error) {
return -1, fmt.Errorf(acnError)
return -1, e.acnError
}
func (e *ErrorACN) GetVersion() string {
return acnError
return e.acnError.Error()
}
func (e *ErrorACN) Close() {
// nothing to do...
}

View File

@ -170,8 +170,9 @@ var progRe = regexp.MustCompile("PROGRESS=([0-9]*)")
var sumRe = regexp.MustCompile("SUMMARY=\"(.*)\"$")
// GetBootstrapStatus returns an int 0-100 on the percent the bootstrapping of the underlying network is at and an optional string message
// returns -1 on network disconnected
// returns -2 on error
//
// returns -1 on network disconnected
// returns -2 on error
func (tp *torProvider) GetBootstrapStatus() (int, string) {
tp.lock.Lock()
defer tp.lock.Unlock()
@ -442,19 +443,18 @@ func newHideCmd(exePath string) process.Creator {
loggerDebug := &logWriter{log.LevelDebug}
loggerError := &logWriter{log.LevelError}
cmd := exec.CommandContext(ctx, exePath, args...)
cmd.Stdout = loggerDebug
cmd.Stderr = loggerError
cmd.SysProcAttr = sysProcAttr
// override tor ld_library_path if requested
torLdLibPath,exists := os.LookupEnv("TOR_LD_LIBRARY_PATH")
torLdLibPath, exists := os.LookupEnv("TOR_LD_LIBRARY_PATH")
if exists {
ldLibPath := fmt.Sprintf("LD_LIBRARY_PATH=%v", torLdLibPath)
cmd.Env = append([]string{ldLibPath},os.Environ()...)
cmd.Env = append([]string{ldLibPath}, os.Environ()...)
}
return cmd, nil
})
}
@ -665,4 +665,4 @@ func dialControlPort(port int) (*control.Conn, error) {
return nil, err
}
return control.NewConn(textConn), nil
}
}