Update connectivity. Delay tor connection until the end of setup #29

Merged
sarah merged 7 commits from tor-setup into main 2023-06-13 18:43:49 +00:00
3 changed files with 63 additions and 36 deletions

7
go.mod
View File

@ -3,9 +3,9 @@ module git.openprivacy.ca/cwtch.im/cwtch-autobindings
go 1.19
require (
cwtch.im/cwtch v0.20.6
cwtch.im/cwtch v0.20.8
git.openprivacy.ca/cwtch.im/server v1.4.5
git.openprivacy.ca/openprivacy/connectivity v1.8.7
git.openprivacy.ca/openprivacy/connectivity v1.10.0
git.openprivacy.ca/openprivacy/log v1.0.3
github.com/mutecomm/go-sqlcipher/v4 v4.4.2
)
@ -19,9 +19,10 @@ require (
github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d // indirect
golang.org/x/mobile v0.0.0-20230427221453-e8d11dd0ba41 // indirect
golang.org/x/mobile v0.0.0-20230531173138-3c911d8e3eda // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
golang.org/x/tools v0.1.12 // indirect
)

12
go.sum
View File

@ -1,6 +1,6 @@
cwtch.im/cwtch v0.18.0/go.mod h1:StheazFFY7PKqBbEyDVLhzWW6WOat41zV0ckC240c5Y=
cwtch.im/cwtch v0.20.6 h1:Vl+z3dRwRClHNNH1/8/y67qN25ApWBMF2YvoODgqohE=
cwtch.im/cwtch v0.20.6/go.mod h1:h8S7EgEM+8pE1k+XLB5jAFdIPlOzwoXEY0GH5mQye5A=
cwtch.im/cwtch v0.20.8 h1:5N95p6uYquu9Vj2E9jK41FJpa679h1oYfiv/2jm5gbA=
cwtch.im/cwtch v0.20.8/go.mod h1:h8S7EgEM+8pE1k+XLB5jAFdIPlOzwoXEY0GH5mQye5A=
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
@ -13,8 +13,8 @@ git.openprivacy.ca/openprivacy/bine v0.0.4/go.mod h1:13ZqhKyqakDsN/ZkQkIGNULsmLy
git.openprivacy.ca/openprivacy/bine v0.0.5 h1:DJs5gqw3SkvLSgRDvroqJxZ7F+YsbxbBRg5t0rU5gYE=
git.openprivacy.ca/openprivacy/bine v0.0.5/go.mod h1:fwdeq6RO08WDkV0k7HfArsjRvurVULoUQmT//iaABZM=
git.openprivacy.ca/openprivacy/connectivity v1.8.6/go.mod h1:Hn1gpOx/bRZp5wvCtPQVJPXrfeUH0EGiG/Aoa0vjGLg=
git.openprivacy.ca/openprivacy/connectivity v1.8.7 h1:/hJLAqLaTj41mWI9ACVEEoCGHWUp+Dxw5HXQeqKDoWI=
git.openprivacy.ca/openprivacy/connectivity v1.8.7/go.mod h1:OQO1+7OIz/jLxDrorEMzvZA6SEbpbDyLGpjoFqT3z1Y=
git.openprivacy.ca/openprivacy/connectivity v1.10.0 h1:7P5xdFL4mGg9bwlIY+sdaNGUMazlMB82/v/6kXlvqxY=
git.openprivacy.ca/openprivacy/connectivity v1.10.0/go.mod h1:OQO1+7OIz/jLxDrorEMzvZA6SEbpbDyLGpjoFqT3z1Y=
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/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
@ -92,8 +92,8 @@ golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo=
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/mobile v0.0.0-20230427221453-e8d11dd0ba41 h1:539vykMVJsmdiucRtMmdeLLZaTVhWhaAHFcPabj2lws=
golang.org/x/mobile v0.0.0-20230427221453-e8d11dd0ba41/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
golang.org/x/mobile v0.0.0-20230531173138-3c911d8e3eda h1:O+EUvnBNPwI4eLthn8W5K+cS8zQZfgTABPLNm6Bna34=
golang.org/x/mobile v0.0.0-20230531173138-3c911d8e3eda/go.mod h1:aAjjkJNdrh3PMckS4B10TGS2nag27cbKR1y2BpUxsiY=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=

View File

@ -30,6 +30,7 @@ import (
_ "github.com/mutecomm/go-sqlcipher/v4"
"cwtch.im/cwtch/app"
"git.openprivacy.ca/openprivacy/connectivity"
"sync"
{{IMPORTS}}
)
@ -166,18 +167,14 @@ func _startCwtch(appDir string, torPath string) {
globalAppDir = appDir
globalTorPath = torPath
settingsFile := app.LoadAppSettings(appDir)
newACN, globalSettings := buildACN(settingsFile.ReadGlobalSettings(), globalTorPath, globalAppDir)
globalACN = connectivity.NewProxyACN(newACN)
settingsFile.WriteGlobalSettings(globalSettings)
// start with an Error ACN
erracn := connectivity.NewErrorACN(fmt.Errorf("initializing tor"))
globalACN = connectivity.NewProxyACN(&erracn)
application = app.NewApp(&globalACN, appDir, settingsFile)
// Subscribe to all App Events...
eventHandler.HandleApp(application)
// Settings may have changed...
globalSettings = settingsFile.ReadGlobalSettings()
settingsJson, _ := json.Marshal(globalSettings)
// FIXME: This code exists to allow the Splash Screen test in the new UI integration tests to pass
// it doesn't actually fix the problem in theory, and we should get around to ensuring that application
// is safe to access even if shutdown is called concurrently...
@ -186,7 +183,8 @@ func _startCwtch(appDir string, torPath string) {
return
}
// Send global settings to the UI...
application.UpdateSettings(globalSettings)
globalSettings := application.ReadSettings()
settingsJson, _ := json.Marshal(globalSettings)
application.GetPrimaryBus().Publish(event.NewEvent(settings.UpdateGlobalSettings, map[event.Field]string{event.Data: string(settingsJson)}))
log.Infof("libcwtch-go application launched")
application.GetPrimaryBus().Publish(event.NewEvent(settings.CwtchStarted, map[event.Field]string{}))
@ -195,6 +193,10 @@ func _startCwtch(appDir string, torPath string) {
{{EXPERIMENT_REGISTER}}
// Finally attempt to set up a proper Tor
// Note: ResetTor launches an internal goroutine so this is non-blocking...
ResetTor()
}
// the pointer returned from this function **must** be freed using c_Free
@ -393,21 +395,41 @@ func c_ResetTor() {
ResetTor()
}
var torLock sync.Mutex
func ResetTor() {
log.Infof("Replacing ACN with new Tor...")
settings := application.ReadSettings()
go func() {
// prevent concurrent calls to this method...
torLock.Lock()
defer torLock.Unlock()
log.Infof("Replacing ACN with new Tor...")
settings := application.ReadSettings()
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)
// only update settings if successful.
if err == nil {
// Only update Tor specific settings...
currentSettings := application.ReadSettings()
currentSettings.TorCacheDir = settings.TorCacheDir
currentSettings.CustomControlPort = settings.CustomControlPort
currentSettings.CustomSocksPort = settings.CustomSocksPort
currentSettings.CustomTorrc = settings.CustomTorrc
application.UpdateSettings(currentSettings)
dan marked this conversation as resolved
Review
		`application.QueryACNStatus()` as well
`application.QueryACNStatus()` as well
Review

pretty sure we still need application.QueryACNStatus() as well I think?

pretty sure we still need `application.QueryACNStatus()` as well I think?
// 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)}))
}
globalACN.Close() // we need to close first if dateDir is the same, otherwise buildACN can't launch tor.
newAcn, settings := buildACN(settings, globalTorPath, globalAppDir)
application.UpdateSettings(settings)
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")
// replace ACN regardlesss
globalACN.ReplaceACN(newAcn)
application.QueryACNStatus()
application.QueryACNVersion()
log.Infof("Restarted")
}()
}
const (
@ -416,7 +438,7 @@ const (
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()))
socksPort := mrand.Intn(1000) + 9600
@ -435,7 +457,8 @@ func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir str
if err != nil {
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)))
return &connectivity.ErrorACN{}, globalSettings
erracn := connectivity.NewErrorACN(err)
return &erracn, globalSettings, err
}
if globalSettings.AllowAdvancedTorConfig {
@ -470,7 +493,8 @@ func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir str
if err != nil {
log.Errorf("error constructing torrc: %v", err)
eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err)))
return &connectivity.ErrorACN{}, globalSettings
erracn := connectivity.NewErrorACN(err)
return &erracn, globalSettings, err
}
dataDir := globalSettings.TorCacheDir
@ -490,7 +514,8 @@ func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir str
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)))
return &connectivity.ErrorACN{}, globalSettings
erracn := connectivity.NewErrorACN(err)
return &erracn, globalSettings, err
}
}
@ -501,9 +526,10 @@ func buildACN(globalSettings settings.GlobalSettings, torPath string, appDir str
if err != nil {
log.Errorf("Error connecting to Tor replacing with ErrorACN: %v\n", err)
eventHandler.Push(event.NewEventList(CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err)))
acn = &connectivity.ErrorACN{}
erracn := connectivity.NewErrorACN(err)
acn = &erracn
}
return acn, globalSettings
return acn, globalSettings, err
}