From 799fc6e621ae510c7abbcf7e37e3ca638c5e8a07 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 18 Jan 2022 12:49:52 -0800 Subject: [PATCH] Allow Caching of Tor Data Directories - Also deletes unused data directories on startup. --- features/servers/servers_functionality.go | 2 +- go.mod | 2 +- go.sum | 3 ++- lib.go | 33 +++++++++++++++++++++-- utils/settings.go | 4 +++ 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/features/servers/servers_functionality.go b/features/servers/servers_functionality.go index b80a247..6b25c1a 100644 --- a/features/servers/servers_functionality.go +++ b/features/servers/servers_functionality.go @@ -110,7 +110,7 @@ func (sf *ServersFunctionality) Enable() { func (sf *ServersFunctionality) LoadServers(password string) ([]string, error) { servers, err := appServers.LoadServers(password) - // server:1.3/libcwtch-go:1.4 accidentely enabled monitor logging by default. make sure it's turned off + // server:1.3/libcwtch-go:1.4 accidentally enabled monitor logging by default. make sure it's turned off for _, onion := range servers { server := appServers.GetServer(onion) server.SetMonitorLogging(false) diff --git a/go.mod b/go.mod index bcf291b..0c882ad 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.15 require ( cwtch.im/cwtch v0.14.12 git.openprivacy.ca/cwtch.im/server v1.4.2 - git.openprivacy.ca/openprivacy/connectivity v1.7.0 + git.openprivacy.ca/openprivacy/connectivity v1.8.0 git.openprivacy.ca/openprivacy/log v1.0.3 github.com/mutecomm/go-sqlcipher/v4 v4.4.2 golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect diff --git a/go.sum b/go.sum index 03f736e..93a99a2 100644 --- a/go.sum +++ b/go.sum @@ -25,8 +25,9 @@ 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/connectivity v1.5.0/go.mod h1:UjQiGBnWbotmBzIw59B8H6efwDadjkKzm3RPT1UaIRw= git.openprivacy.ca/openprivacy/connectivity v1.6.0/go.mod h1:UjQiGBnWbotmBzIw59B8H6efwDadjkKzm3RPT1UaIRw= -git.openprivacy.ca/openprivacy/connectivity v1.7.0 h1:h3NZsxY0/viqyrLInu/S/3t23ehAYmhJm7rsHPvm8NU= git.openprivacy.ca/openprivacy/connectivity v1.7.0/go.mod h1:UjQiGBnWbotmBzIw59B8H6efwDadjkKzm3RPT1UaIRw= +git.openprivacy.ca/openprivacy/connectivity v1.8.0 h1:/jnc/2LuUpli2NgeLw6zOAVESHrz5mr10kDCQHyZ7R4= +git.openprivacy.ca/openprivacy/connectivity v1.8.0/go.mod h1:UjQiGBnWbotmBzIw59B8H6efwDadjkKzm3RPT1UaIRw= 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= diff --git a/lib.go b/lib.go index 5ecfdf6..535f8f1 100644 --- a/lib.go +++ b/lib.go @@ -10,6 +10,8 @@ import ( "crypto/rand" "encoding/json" "fmt" + "io/ioutil" + "path" "strconv" constants2 "cwtch.im/cwtch/model/constants" @@ -195,6 +197,8 @@ func _startCwtch(appDir string, torPath string) { event.FileDownloaded, } + // Settings may have changed... + settings = utils.ReadGlobalSettings() settingsJson, _ := json.Marshal(settings) application.LoadProfiles(constants.DefactoPasswordForUnencryptedProfiles) @@ -260,7 +264,32 @@ func buildACN(settings utils.GlobalSettings, torPath string, appDir string) conn return &connectivity.ErrorACN{} } - acn, err := tor.NewTorACNWithAuth(appDir, torPath, controlPort, tor.HashedPasswordAuthenticator{Password: base64.StdEncoding.EncodeToString(key)}) + dataDir := settings.TorCacheDir + if !settings.UseTorCache { + + // purge data dir directories if we are not using them for a cache + torDir := path.Join(appDir, "tor") + files, err := filepath.Glob(path.Join(torDir, "data-dir-*")) + if err != nil { + log.Errorf("could not construct filesystem glob: %v", err) + } + for _, f := range files { + if err := os.RemoveAll(f); err != nil { + log.Errorf("could not remove data-dir: %v", err) + } + } + + if dataDir, err = ioutil.TempDir(torDir, "data-dir-"); err != nil { + eventHandler.Push(event.NewEventList(utils.CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err))) + return &connectivity.ErrorACN{} + } + } + + // Persist Current Data Dir as Tor Cache... + settings.TorCacheDir = dataDir + utils.WriteGlobalSettings(settings) + + acn, err := tor.NewTorACNWithAuth(appDir, torPath, dataDir, controlPort, tor.HashedPasswordAuthenticator{Password: base64.StdEncoding.EncodeToString(key)}) if err != nil { log.Errorf("Error connecting to Tor replacing with ErrorACN: %v\n", err) eventHandler.Push(event.NewEventList(utils.CwtchStartError, event.Error, fmt.Sprintf("Error connecting to Tor: %v", err))) @@ -296,7 +325,7 @@ func ReconnectCwtchForeground() { profile := application.GetPeer(profileOnion) conversations, _ := profile.FetchConversations() for _, conversation := range conversations { - if (conversation.IsGroup() && groupHandler != nil) || conversation.IsServer() == false { + if (conversation.IsGroup() && groupHandler != nil) || !conversation.IsServer() { totalMessages, _ := profile.GetChannelMessageCount(conversation.ID, 0) eventHandler.Push(event.NewEvent(event.MessageCounterResync, map[event.Field]string{ event.Identity: profileOnion, diff --git a/utils/settings.go b/utils/settings.go index 4f15a07..3a9cc6a 100644 --- a/utils/settings.go +++ b/utils/settings.go @@ -46,6 +46,8 @@ type GlobalSettings struct { UseExternalTor bool CustomSocksPort int CustomControlPort int + UseTorCache bool + TorCacheDir string } var DefaultGlobalSettings = GlobalSettings{ @@ -66,6 +68,8 @@ var DefaultGlobalSettings = GlobalSettings{ UseCustomTorrc: false, CustomSocksPort: -1, CustomControlPort: -1, + UseTorCache: false, + TorCacheDir: "", } func InitGlobalSettingsFile(directory string, password string) error {