store bootsrap version, make available; fix tor version parsing for double digit versions
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Dan Ballard 2022-08-28 18:13:26 -07:00
parent 1741e63424
commit 789de52589
1 changed files with 22 additions and 11 deletions

View File

@ -76,6 +76,7 @@ type torProvider struct {
isClosed bool
dataDir string
version string
bootProgress int
}
func (ols *onionListenService) AddressFull() string {
@ -205,6 +206,7 @@ func (tp *torProvider) GetBootstrapStatus() (int, string) {
status = statusMatches[1]
}
}
tp.bootProgress = progress
return progress, status
}
@ -298,12 +300,12 @@ func (tp *torProvider) Restart() {
log.Debugf("launching restart...")
tp.lock.Lock()
defer tp.lock.Unlock()
log.Debugf("checking last restart time")
if time.Since(tp.lastRestartTime) < restartCooldown {
tp.lock.Unlock()
return
}
tp.lock.Unlock()
go tp.restart()
}
@ -346,6 +348,10 @@ func (tp *torProvider) Open(hostname string) (net.Conn, string, error) {
tp.lock.Unlock()
return nil, hostname, errors.New("tor is offline")
}
if tp.bootProgress != 100 {
tp.lock.Unlock()
return nil, hostname, fmt.Errorf("tor not online, bootstrap progress only %v", tp.bootProgress)
}
tp.lock.Unlock()
resolvedHostname := hostname
@ -353,14 +359,24 @@ func (tp *torProvider) Open(hostname string) (net.Conn, string, error) {
addrParts := strings.Split(hostname, ":")
resolvedHostname = addrParts[1]
}
conn, err := tp.dialer.Dial("tcp", resolvedHostname+".onion:9878")
return conn, resolvedHostname, err
}
func (tp *torProvider) Close() {
closing := false
tp.lock.Lock()
defer tp.lock.Unlock()
if !tp.isClosed {
// Break out of any background checks and close
// the underlying tor connection
tp.isClosed = true
tp.breakChan <- true
// wiggle lock to make sure if monitorRestart is waiting for the lock, it gets it, and finished that branch and gets the channel request to exit
tp.lock.Unlock()
tp.lock.Lock()
closing = true
}
// Unregister Child Listeners
for addr, child := range tp.childListeners {
@ -370,11 +386,7 @@ func (tp *torProvider) Close() {
log.Debugf("shutting down acn threads..(is already closed: %v)", tp.isClosed)
if !tp.isClosed {
// Break out of any background checks and close
// the underlying tor connection
tp.isClosed = true
tp.breakChan <- true
if closing {
if tp.t != nil {
tp.t.Close()
tp.t = nil
@ -465,7 +477,7 @@ func startTor(appDirectory string, bundledTorPath string, dataDir string, contro
os.MkdirAll(torDir, 0700)
tp := &torProvider{authenticator: authenticator, controlPort: controlPort, appDirectory: appDirectory, bundeledTorPath: bundledTorPath, childListeners: make(map[string]*onionListenService), breakChan: make(chan bool, 1), statusCallback: nil, versionCallback: 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, 1), statusCallback: nil, versionCallback: nil, lastRestartTime: time.Now().Add(-restartCooldown), bootProgress: -1}
log.Debugf("checking if there is a running system tor")
if version, err := tp.checkVersion(); err == nil {
@ -542,7 +554,6 @@ func (tp *torProvider) monitorRestart() {
prog, status := tp.GetBootstrapStatus()
if prog == torDown && tp.t != nil {
log.Warnf("monitorRestart calling tp.restart() with prog:%v\n", prog)
tp.restart()
return
}
@ -618,7 +629,7 @@ func checkCmdlineTorVersion(torCmd string) (string, bool) {
return "", false
}
re := regexp.MustCompile(`[0-1]\.[0-9]\.[0-9]\.[0-9]`)
re := regexp.MustCompile(`[0-1]+\.[0-9]+\.[0-9]+\.[0-9]+`)
sysTorVersion := re.Find(outb.Bytes())
log.Infof("tor version: %v", string(sysTorVersion))
return string(sysTorVersion), minTorVersionReqs(string(sysTorVersion))