-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.go
158 lines (141 loc) · 3.8 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"errors"
"net"
"os"
"gioui.org/app"
"github.com/katzenpost/katzenpost/catshadow"
"github.com/katzenpost/katzenpost/client"
"github.com/katzenpost/katzenpost/client/config"
"github.com/katzenpost/katzenpost/core/log"
"path/filepath"
)
// checks to see if the local system has a listener on port 9050
func hasTor() bool {
c, err := net.Dial("tcp", "127.0.0.1:9050")
if err != nil {
return false
}
c.Close()
return true
}
func setupCatShadow(passphrase []byte, result chan interface{}) {
// XXX: if the catshadowClient already exists, shut it down
// FIXME: figure out a better way to toggle connected/disconnected
// states and allow to retry attempts on a timeout or other failure.
var catshadowClient *catshadow.Client
var stateWorker *catshadow.StateWriter
var state *catshadow.State
var err error
// obtain the default data location
dir, err := app.DataDir()
if err != nil {
result <- err
return
}
// dir does not appear to point to ~/.config/katzen but rather ~/.config on linux?
// create directory for application data
datadir := filepath.Join(dir, dataDirName)
_, err = os.Stat(datadir)
if os.IsNotExist(err) {
// create the application data directory
err := os.Mkdir(datadir, os.ModeDir|os.FileMode(0700))
if err != nil {
result <- err
return
}
}
// if the statefile doesn't exist, try the default datadir
var statefile string
if _, err := os.Stat(*stateFile); os.IsNotExist(err) {
statefile = filepath.Join(datadir, *stateFile)
} else {
statefile = *stateFile
}
var cfg *config.Config
if len(*clientConfigFile) != 0 {
cfg, err = config.LoadFile(*clientConfigFile)
if err != nil {
result <- err
return
}
} else {
cfg, err = config.Load(cfgWithoutTor)
if err != nil {
result <- err
return
}
}
// initialize logging
backendLog, err := log.New(cfg.Logging.File, cfg.Logging.Level, cfg.Logging.Disable)
if err != nil {
result <- err
return
}
// automatically create a statefile if one does not already exist
stateLogger := backendLog.GetLogger("catshadow_state")
if _, err = os.Stat(statefile); os.IsNotExist(err) {
stateWorker, err = catshadow.NewStateWriter(stateLogger, statefile, passphrase)
} else {
stateWorker, state, err = catshadow.LoadStateWriter(stateLogger, statefile, passphrase)
}
// catches any err above
if err != nil {
result <- err
return
}
// create a default statefile with default options on first run
if state == nil {
// create a default statefile
state = &catshadow.State{
Contacts: make([]*catshadow.Contact, 0),
Conversations: make(map[string]map[catshadow.MessageID]*catshadow.Message),
}
}
// initialize default options
if state.Blob == nil {
state.Blob = make(map[string][]byte)
if hasTor() && len(*clientConfigFile) == 0 {
state.Blob["UseTor"] = []byte{1}
state.Blob["AutoConnect"] = []byte{1}
}
}
// apply any persistent settings that are needed before bootstrapping client
if _, ok := state.Blob["UseTor"]; ok {
if len(*clientConfigFile) != 0 {
// a user-supplied configuration file was specified
if cfg.UpstreamProxy.Type != "socks5" {
state.Blob["UseTor"] = []byte{0}
result <- errors.New("User supplied configuration and client settings mismatch! UseTor option selected without valid UpstreamProxy!")
return
}
}
if !hasTor() {
warnNoTor()
// disable autoconnect
delete(state.Blob, "AutoConnect")
} else {
cfg, err = config.Load(cfgWithTor)
if err != nil {
result <- err
return
}
}
}
// create a client
c, err := client.New(cfg)
if err != nil {
result <- err
return
}
// Start the stateworker
stateWorker.Start()
catshadowClient, err = catshadow.New(backendLog, c, stateWorker, state)
if err != nil {
result <- err
c.Shutdown()
stateWorker.Halt()
return
}
result <- catshadowClient
}