Allow Caching of Tor Data Directories
continuous-integration/drone/pr Build is pending Details

- Also deletes unused data directories on startup.
This commit is contained in:
Sarah Jamie Lewis 2022-01-18 12:49:52 -08:00
parent 4cf95d6507
commit 799fc6e621
5 changed files with 39 additions and 5 deletions

View File

@ -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)

2
go.mod
View File

@ -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

3
go.sum
View File

@ -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=

33
lib.go
View File

@ -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,

View File

@ -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 {