Much nicer Tor Handling
continuous-integration/drone/pr Build is pending Details

This commit is contained in:
Sarah Jamie Lewis 2023-05-24 14:05:47 -07:00
parent 1b07a195be
commit 6f427d282b
1 changed files with 40 additions and 33 deletions

View File

@ -30,6 +30,7 @@ import (
_ "github.com/mutecomm/go-sqlcipher/v4" _ "github.com/mutecomm/go-sqlcipher/v4"
"cwtch.im/cwtch/app" "cwtch.im/cwtch/app"
"git.openprivacy.ca/openprivacy/connectivity" "git.openprivacy.ca/openprivacy/connectivity"
"sync"
{{IMPORTS}} {{IMPORTS}}
) )
@ -193,20 +194,9 @@ func _startCwtch(appDir string, torPath string) {
{{EXPERIMENT_REGISTER}} {{EXPERIMENT_REGISTER}}
// Finally attempt to set up a proper TOR // Finally attempt to set up a proper Tor
// NOTE: This can block until finishing, so we wrap it in a // Note: ResetTor launches an internal goroutine so this is non-blocking...
// go routine... ResetTor()
go func() {
newACN, globalSettings := buildACN(application.ReadSettings(), globalTorPath, globalAppDir)
settingsFile.WriteGlobalSettings(globalSettings)
globalACN.ReplaceACN(newACN)
application.QueryACNVersion()
// Settings may have changed...
globalSettings = settingsFile.ReadGlobalSettings()
settingsJson, _ := json.Marshal(globalSettings)
application.UpdateSettings(globalSettings)
application.GetPrimaryBus().Publish(event.NewEvent(settings.UpdateGlobalSettings, map[event.Field]string{event.Data: string(settingsJson)}))
}()
} }
// the pointer returned from this function **must** be freed using c_Free // the pointer returned from this function **must** be freed using c_Free
@ -405,21 +395,38 @@ func c_ResetTor() {
ResetTor() ResetTor()
} }
var torLock sync.Mutex
func ResetTor() { func ResetTor() {
log.Infof("Replacing ACN with new Tor...") go func() {
settings := application.ReadSettings() // prevent concurrent calls to this method...
torLock.Lock()
globalACN.Close() // we need to close first if dateDir is the same, otherwise buildACN can't launch tor. defer torLock.Unlock()
newAcn, settings := buildACN(settings, globalTorPath, globalAppDir) log.Infof("Replacing ACN with new Tor...")
application.UpdateSettings(settings) settings := application.ReadSettings()
globalACN.ReplaceACN(newAcn)
application.QueryACNVersion() globalACN.Close() // we need to close first if dateDir is the same, otherwise buildACN can't launch tor.
newAcn, settings, err := buildACN(settings, globalTorPath, globalAppDir)
// We need to update settings on reset as buildACN can alter settings, otherwise the next reset will be broken... // only update settings if successful.
settings = application.ReadSettings() if err == nil {
settingsJson, _ := json.Marshal(settings) // Only update Tor specific settings...
application.GetPrimaryBus().Publish(event.NewEvent(UpdateGlobalSettings, map[event.Field]string{event.Data: string(settingsJson)})) currentSettings := application.ReadSettings()
log.Infof("Restarted") currentSettings.TorCacheDir = settings.TorCacheDir
currentSettings.CustomControlPort = settings.CustomControlPort
currentSettings.CustomSocksPort = settings.CustomSocksPort
currentSettings.CustomTorrc = settings.CustomTorrc
application.UpdateSettings(currentSettings)
globalACN.ReplaceACN(newAcn)
application.QueryACNVersion()
// We need to update settings on reset as buildACN can alter settings, otherwise the next reset will be broken...
settings = application.ReadSettings()
settingsJson, _ := json.Marshal(settings)
application.GetPrimaryBus().Publish(event.NewEvent(UpdateGlobalSettings, map[event.Field]string{event.Data: string(settingsJson)}))
}
log.Infof("Restarted")
}()
} }
const ( const (
@ -428,7 +435,7 @@ const (
UpdateGlobalSettings = event.Type("UpdateGlobalSettings") UpdateGlobalSettings = event.Type("UpdateGlobalSettings")
) )
func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir string) (connectivity.ACN, settings.GlobalSettings) { func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir string) (connectivity.ACN, settings.GlobalSettings, error) {
mrand.Seed(int64(time.Now().Nanosecond())) mrand.Seed(int64(time.Now().Nanosecond()))
socksPort := mrand.Intn(1000) + 9600 socksPort := mrand.Intn(1000) + 9600
@ -447,7 +454,7 @@ func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir str
if err != nil { if err != nil {
log.Errorf("error creating tor data directory: %v. Aborting app start up", err) log.Errorf("error creating tor data directory: %v. Aborting app start up", err)
eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err))) eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err)))
return &connectivity.ErrorACN{}, globalSettings return &connectivity.ErrorACN{}, globalSettings, err
} }
if globalSettings.AllowAdvancedTorConfig { if globalSettings.AllowAdvancedTorConfig {
@ -482,7 +489,7 @@ func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir str
if err != nil { if err != nil {
log.Errorf("error constructing torrc: %v", err) log.Errorf("error constructing torrc: %v", err)
eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err))) eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err)))
return &connectivity.ErrorACN{}, globalSettings return &connectivity.ErrorACN{}, globalSettings, err
} }
dataDir := globalSettings.TorCacheDir dataDir := globalSettings.TorCacheDir
@ -502,7 +509,7 @@ func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir str
if dataDir, err = os.MkdirTemp(torDir, "data-dir-"); err != nil { if dataDir, err = os.MkdirTemp(torDir, "data-dir-"); err != nil {
eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err))) eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err)))
return &connectivity.ErrorACN{}, globalSettings return &connectivity.ErrorACN{}, globalSettings, err
} }
} }
@ -515,7 +522,7 @@ func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir str
eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err))) eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err)))
acn = &connectivity.ErrorACN{} acn = &connectivity.ErrorACN{}
} }
return acn, globalSettings return acn, globalSettings, err
} }