This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcatchat.go
306 lines (263 loc) · 8.38 KB
/
catchat.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"flag"
"fmt"
"os"
"syscall"
"time"
"github.com/katzenpost/catshadow"
catconfig "github.com/katzenpost/catshadow/config"
"github.com/katzenpost/client"
clientConfig "github.com/katzenpost/client/config"
gap "github.com/muesli/go-app-paths"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/qml"
"github.com/therecipe/qt/quickcontrols2"
)
const (
initialPKIConsensusTimeout = 45 * time.Second
)
var (
config Config
clientConfigFile = flag.String("f", "", "Path to the client config file.")
stateFile = flag.String("s", "catshadow_statefile", "The catshadow state file path.")
catshadowClient *catshadow.Client
catshadowCfg *catconfig.Config
contactListModel *ContactListModel
conversationModel *ConversationModel
)
// runApp loads and executes the QML UI
func runApp(config Config) {
var theme string
switch config.Theme {
case "System":
theme = ""
case "Light":
theme = "Default"
default:
theme = config.Theme
}
if theme != "" {
quickcontrols2.QQuickStyle_SetStyle(theme)
}
app := qml.NewQQmlApplicationEngine(nil)
app.RootContext().SetContextProperty("accountBridge", accountBridge)
app.RootContext().SetContextProperty("settings", configBridge)
app.Load(core.NewQUrl3("qrc:/qml/catchat.qml", 0))
gui.QGuiApplication_Exec()
}
func setupCatShadow(catshadowCfg *catconfig.Config, passphrase []byte) {
// 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.
if catshadowClient != nil {
catshadowClient.Shutdown()
contactListModel.clear()
conversationModel.clear()
}
accountBridge.SetStatus("Connecting...")
var stateWorker *catshadow.StateWriter
var state *catshadow.State
cfg, err := catshadowCfg.ClientConfig()
if err != nil {
accountBridge.SetError(err.Error())
accountBridge.SetStatus("Disconnected")
return
}
// automatically create a statefile if one does not already exist
// TODO: pick a sensible location for a default statefile other than cwd
if _, err := os.Stat(*stateFile); os.IsNotExist(err) {
cfg, linkKey := client.AutoRegisterRandomClient(cfg)
c, err := client.New(cfg)
if err != nil {
accountBridge.SetError(err.Error())
// UX only receives connection events from the catshadow client
accountBridge.SetStatus("Disconnected")
return
}
// Create statefile.
stateWorker, err = catshadow.NewStateWriter(c.GetLogger("catshadow_state"), *stateFile, passphrase)
if err != nil {
accountBridge.SetError(err.Error())
accountBridge.SetStatus("Disconnected")
c.Shutdown()
return
}
// Start the stateworker
stateWorker.Start()
fmt.Println("creating remote message receiver spool")
backendLog, err := catshadowCfg.InitLogBackend()
if err != nil {
accountBridge.SetError(err.Error())
accountBridge.SetStatus("Disconnected")
stateWorker.Halt()
c.Shutdown()
return
}
user := fmt.Sprintf("%x", linkKey.PublicKey().Bytes())
catshadowClient, err = catshadow.NewClientAndRemoteSpool(backendLog, c, stateWorker, user, linkKey)
if err != nil {
accountBridge.SetError(err.Error())
accountBridge.SetStatus("Disconnected")
stateWorker.Halt()
c.Shutdown()
return
}
fmt.Println("catshadow client successfully created")
} else {
cfg, _ := client.AutoRegisterRandomClient(cfg)
// Load previous state to setup our current client state.
backendLog, err := catshadowCfg.InitLogBackend()
if err != nil {
accountBridge.SetError(err.Error())
accountBridge.SetStatus("Disconnected")
return
}
stateWorker, state, err = catshadow.LoadStateWriter(backendLog.GetLogger("state_worker"), *stateFile, passphrase)
if err != nil {
accountBridge.SetError(err.Error())
accountBridge.SetStatus("Disconnected")
return
}
// Start the stateworker
stateWorker.Start()
cfg.Account = &clientConfig.Account{
User: state.User,
Provider: state.Provider,
}
// Run a Client.
c, err := client.New(cfg)
if err != nil {
accountBridge.SetError(err.Error())
accountBridge.SetStatus("Disconnected")
stateWorker.Halt()
return
}
// Make a catshadow Client.
catshadowClient, err = catshadow.New(backendLog, c, stateWorker, state)
if err != nil {
accountBridge.SetError(err.Error())
accountBridge.SetStatus("Disconnected")
c.Shutdown()
stateWorker.Halt()
return
}
}
// Start catshadow client.
catshadowClient.Start()
go eventLoop(catshadowClient.EventSink, conversationModel, contactListModel)
contacts := catshadowClient.GetContacts()
loadContactList(contactListModel, contacts)
}
func main() {
flag.Parse()
// Set the umask to something "paranoid".
syscall.Umask(0077)
fmt.Println("Katzenpost is still pre-alpha. DO NOT DEPEND ON IT FOR STRONG SECURITY OR ANONYMITY.")
core.QCoreApplication_SetApplicationName("catchat")
core.QCoreApplication_SetOrganizationName("katzenpost")
core.QCoreApplication_SetAttribute(core.Qt__AA_EnableHighDpiScaling, true)
ga := gui.NewQGuiApplication(len(os.Args), os.Args)
ga.SetWindowIcon(gui.NewQIcon5(":/qml/images/katzenpost_logo.png"))
// load config
scope := gap.NewScope(gap.User, "catchat")
configDir, err := scope.ConfigPath("")
if err != nil {
panic(err)
}
os.MkdirAll(configDir, 0700)
dataDir, err := scope.DataDirs()
if err != nil {
panic(err)
}
os.MkdirAll(dataDir[0], 0700)
configFile, err := scope.ConfigPath("catchat.conf")
if err != nil {
panic(err)
}
config = LoadConfig(configFile)
// Prepare catshadow client instance.
contactListModel = NewContactListModel(nil)
conversationModel = NewConversationModel(nil)
// Load catshadow config file if specified or use baked-in defaults
if len(*clientConfigFile) != 0 {
catshadowCfg, err = catconfig.LoadFile(*clientConfigFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load config file '%v': %v\n", *clientConfigFile, err)
os.Exit(-1)
}
} else {
// use the baked in configuration defaults if a configuration is not specified
catshadowCfg, err = getDefaultConfig()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to load config defaults: %v\n", err)
os.Exit(-1)
}
}
// Start graphical user interface.
setupQmlBridges()
configBridge.SetTheme(config.Theme)
configBridge.SetStyle(config.Style)
configBridge.SetNotification(config.Notification)
configBridge.SetFirstRun(config.FirstRun)
configBridge.SetPositionX(config.PositionX)
configBridge.SetPositionY(config.PositionY)
configBridge.SetWidth(config.Width)
configBridge.SetHeight(config.Height)
accountBridge.SetContactListModel(contactListModel)
accountBridge.SetConversationModel(conversationModel)
runApp(config)
// Shutdown client after graphical user interface is halted.
catshadowClient.Shutdown()
// Save Qt user interface config on clean shutdown.
config.Theme = configBridge.Theme()
config.Style = configBridge.Style()
config.Notification = configBridge.Notification()
config.PositionX = configBridge.PositionX()
config.PositionY = configBridge.PositionY()
config.Width = configBridge.Width()
config.Height = configBridge.Height()
config.FirstRun = false
SaveConfig(configFile, config)
}
func getDefaultConfig() (*catconfig.Config, error) {
cfgString := `
[UpstreamProxy]
Type = "socks5"
Network = "tcp"
Address = "127.0.0.1:9050"
[Logging]
Disable = false
Level = "DEBUG"
File = ""
[ClientLogging]
Disable = false
Level = "NOTICE"
File = ""
[VotingAuthority]
[[VotingAuthority.Peers]]
Addresses = ["n5axysudjvjjkpy4r7hur7qfgybfaiwrfz2mqwkvnyylqxinldtao2ad.onion:30000"]
IdentityPublicKey = "EmUWxb6ocBBXhxlrAKgxVd/6tyIDVK/8pIY/nZrqSDQ="
LinkPublicKey = "Mcfs706pyzBIvEj+k5t2L9t9x+LplOR4wz3RiVrgoVU="
[[VotingAuthority.Peers]]
Addresses = ["mj5ouhyjvokgvbcp56lh56plxvzh4wcrq3fadpqf6ewdqmuy7pr3n6qd.onion:30000"]
IdentityPublicKey = "vdOAeoRtWKFDw+W4k3sNN1EMT9ZsaHHmuCHOEKSg1aA="
LinkPublicKey = "VNmU4g1hXBS7BQ1RJYMGNjNg4fIZbCimppeJ1XwrqX4="
[[VotingAuthority.Peers]]
Addresses = ["pz6obnsyh7vmpmtmrsam443jh4gkei77q3y66ty3fd6h6wjdvcmu6pid.onion:30000"]
IdentityPublicKey = "bFgvws69dJrc3ACKXN5aCJKLHjkN7D8DA2HDKkhSNIk="
LinkPublicKey = "p1JekMh8uCPDsRSP5Uc59DJvEGMmA/B0mcMCXx1WEkk="
[Debug]
CaseSensitiveUserIdentifiers = false
PollingInterval = 500
PreferedTransports = ["onion"]
[Panda]
Receiver = "+panda"
Provider = "provider1"
BlobSize = 1000
[Reunion]
Enable = false
`
return catconfig.Load([]byte(cfgString))
}