-
Notifications
You must be signed in to change notification settings - Fork 80
/
account_details.go
396 lines (335 loc) · 10 KB
/
account_details.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package gui
import (
"crypto/tls"
"encoding/hex"
"fmt"
"log"
"strconv"
"strings"
"github.com/coyim/coyim/config"
"github.com/coyim/coyim/digests"
"github.com/coyim/coyim/i18n"
"github.com/coyim/coyim/net"
"github.com/coyim/coyim/session/access"
"github.com/coyim/coyim/xmpp"
"github.com/coyim/gotk3adapter/gtki"
)
type accountDetailsData struct {
builder *builder
dialog gtki.Dialog
notebook gtki.Notebook
otherSettings gtki.CheckButton
acc gtki.Entry
pass gtki.Entry
displayName gtki.Entry
server gtki.Entry
port gtki.Entry
proxies gtki.ListStore
pins gtki.ListStore
notificationArea gtki.Box
proxiesView gtki.TreeView
fingerprintsMessage gtki.Label
pinningPolicy gtki.ComboBoxText
pinsView gtki.TreeView
}
func getBuilderAndAccountDialogDetails() *accountDetailsData {
data := &accountDetailsData{}
dialogID := "AccountDetails"
data.builder = newBuilder(dialogID)
//data.proxies needs to be kept beyond the lifespan of the builder.
//Because this also holds a reference to the builder, we should be fine.
data.builder.getItems(
dialogID, &data.dialog,
"notebook1", &data.notebook,
"otherSettings", &data.otherSettings,
"account", &data.acc,
"password", &data.pass,
"displayName", &data.displayName,
"server", &data.server,
"port", &data.port,
"proxies-model", &data.proxies,
"notification-area", &data.notificationArea,
"proxies-view", &data.proxiesView,
"fingerprintsMessage", &data.fingerprintsMessage,
"pins-model", &data.pins,
"pinningPolicyValue", &data.pinningPolicy,
"pins-view", &data.pinsView,
)
return data
}
func formattedFingerprintsFor(s access.Session) string {
result := ""
if s != nil {
for _, sk := range s.PrivateKeys() {
pk := sk.PublicKey()
if pk != nil {
result = fmt.Sprintf("%s%s%s\n", result, " ", config.FormatFingerprint(pk.Fingerprint()))
}
}
}
return result
}
func findPinningPolicyFor(t string) int {
switch t {
case "none":
return 0
case "deny":
return 1
case "add":
return 2
case "", "add-first-ask-rest":
return 3
case "add-first-deny-rest":
return 4
case "ask":
return 5
}
return -1
}
func filterCertificates(oldCerts []*config.CertificatePin, newList gtki.ListStore) []*config.CertificatePin {
allPins := make(map[string]bool)
iter, ok := newList.GetIterFirst()
for ok {
vv, _ := newList.GetValue(iter, 2)
pp, _ := vv.GetString()
allPins[pp] = true
ok = newList.IterNext(iter)
}
newCerts := []*config.CertificatePin{}
for _, cc := range oldCerts {
if allPins[hex.EncodeToString(cc.Fingerprint)] {
newCerts = append(newCerts, cc)
}
}
return newCerts
}
func (u *gtkUI) connectionInfoDialog(account *account) {
assertInUIThread()
dialogID := "ConnectionInformation"
builder := newBuilder(dialogID)
var dialog gtki.Dialog
var server, tlsAlgo, tlsVersion, tlsFingerprint gtki.Label
var pinCertButton gtki.Button
builder.getItems(
dialogID, &dialog,
"serverValue", &server,
"tlsAlgoValue", &tlsAlgo,
"tlsVersionValue", &tlsVersion,
"tlsFingerprintValue", &tlsFingerprint,
"pin-cert", &pinCertButton,
)
tlsConn := account.session.Conn().RawOut().(*tls.Conn)
serverAddress := account.session.Conn().ServerAddress()
if serverAddress == "" {
parts := strings.SplitN(account.session.GetConfig().Account, "@", 2)
serverAddress = parts[1]
}
server.SetLabel(serverAddress)
tlsAlgo.SetLabel(xmpp.GetCipherSuiteName(tlsConn.ConnectionState()))
tlsVersion.SetLabel(xmpp.GetTLSVersion(tlsConn.ConnectionState()))
certs := tlsConn.ConnectionState().PeerCertificates
chunks := splitStringEvery(fmt.Sprintf("%X", digests.Sha3_256(certs[0].Raw)), chunkingDefaultGrouping)
tlsFingerprint.SetLabel(fmt.Sprintf("%s %s %s %s\n%s %s %s %s", chunks[0], chunks[1], chunks[2], chunks[3], chunks[4], chunks[5], chunks[6], chunks[7]))
if checkPinned(account.session.GetConfig(), certs) {
pinCertButton.SetSensitive(false)
}
builder.ConnectSignals(map[string]interface{}{
"on_close_signal": dialog.Destroy,
"on_pin_signal": func() {
account.session.GetConfig().SaveCert(certs[0].Subject.CommonName, certs[0].Issuer.CommonName, digests.Sha3_256(certs[0].Raw))
u.SaveConfig()
pinCertButton.SetSensitive(false)
},
})
dialog.SetTransientFor(u.window)
dialog.ShowAll()
}
type accountDetails struct {
accTxt string
passTxt string
dispTxt string
servTxt string
portTxt string
}
func getAccountDetails(data *accountDetailsData) *accountDetails {
accTxt, _ := data.acc.GetText()
passTxt, _ := data.pass.GetText()
dispTxt, _ := data.displayName.GetText()
servTxt, _ := data.server.GetText()
portTxt, _ := data.port.GetText()
details := &accountDetails{
accTxt,
passTxt,
dispTxt,
servTxt,
portTxt,
}
return details
}
func renderSessionDetails(s access.Session, data *accountDetailsData, account string) {
data.displayName.SetProperty("placeholder-text", s.DisplayName())
nick := s.GetConfig().Nickname
if nick != "" {
data.displayName.SetText(nick)
}
if s.PrivateKeys() != nil && len(s.PrivateKeys()) > 0 {
data.fingerprintsMessage.SetSelectable(true)
m := i18n.Local("The fingerprints for %s:\n%s")
message := fmt.Sprintf(m, account, formattedFingerprintsFor(s))
data.fingerprintsMessage.SetText(message)
}
}
func renderAccountDetails(account *config.Account, data *accountDetailsData) {
if account.Password != "" {
data.pass.SetProperty("placeholder-text", "(saved in configuration file)")
}
data.server.SetText(account.Server)
if account.Port == 0 {
account.Port = 5222
}
data.port.SetText(strconv.Itoa(account.Port))
for _, px := range account.Proxies {
iter := data.proxies.Append()
data.proxies.SetValue(iter, 0, net.ParseProxy(px).ForPresentation())
data.proxies.SetValue(iter, 1, px)
}
for _, px := range account.Certificates {
iter := data.pins.Append()
data.pins.SetValue(iter, 0, px.Subject)
data.pins.SetValue(iter, 1, px.Issuer)
data.pins.SetValue(iter, 2, hex.EncodeToString(px.Fingerprint))
}
data.pinningPolicy.SetActive(findPinningPolicyFor(account.PinningPolicy))
}
func addAccount(account *config.Account, accDtails *accountDetails, data *accountDetailsData) {
account.Account = accDtails.accTxt
account.Server = accDtails.servTxt
if accDtails.passTxt != "" {
account.Password = accDtails.passTxt
}
account.Nickname = accDtails.dispTxt
convertedPort, e := strconv.Atoi(accDtails.portTxt)
if len(strings.TrimSpace(accDtails.portTxt)) == 0 || e != nil {
convertedPort = 5222
}
account.Port = convertedPort
newProxies := []string{}
iter, ok := data.proxies.GetIterFirst()
for ok {
vv, _ := data.proxies.GetValue(iter, 1)
newProxy, _ := vv.GetString()
newProxies = append(newProxies, newProxy)
ok = data.proxies.IterNext(iter)
}
account.Proxies = newProxies
account.Certificates = filterCertificates(account.Certificates, data.pins)
account.PinningPolicy = data.pinningPolicy.GetActiveID()
}
func (u *gtkUI) accountDialog(s access.Session, account *config.Account, saveFunction func()) {
assertInUIThread()
data := getBuilderAndAccountDialogDetails()
data.otherSettings.SetActive(u.config.AdvancedOptions)
data.acc.SetText(account.Account)
if s != nil {
renderSessionDetails(s, data, account.Account)
}
renderAccountDetails(account, data)
p2, _ := data.notebook.GetNthPage(1)
p3, _ := data.notebook.GetNthPage(2)
p4, _ := data.notebook.GetNthPage(3)
editProxy := func(iter gtki.TreeIter, onCancel func()) {
val, _ := data.proxies.GetValue(iter, 1)
realProxyData, _ := val.GetString()
u.editProxy(realProxyData, data.dialog,
func(p net.Proxy) {
data.proxies.SetValue(iter, 0, p.ForPresentation())
data.proxies.SetValue(iter, 1, p.ForProcessing())
}, onCancel)
}
errorNotif := newErrorNotification(data.notificationArea)
data.builder.ConnectSignals(map[string]interface{}{
"on_toggle_other_settings": func() {
otherSettings := data.otherSettings.GetActive()
u.setShowAdvancedSettings(otherSettings)
data.notebook.SetShowTabs(otherSettings)
if !otherSettings {
p2.Hide()
p3.Hide()
p4.Hide()
}
p2.Show()
p3.Show()
p4.Show()
},
"on_save_signal": func() {
accDtails := getAccountDetails(data)
if isJid, err := verifyXMPPAddress(accDtails.accTxt); !isJid {
errorNotif.ShowMessage(err)
log.Printf(err)
return
}
addAccount(account, accDtails, data)
go saveFunction()
data.dialog.Destroy()
},
"on_edit_proxy_signal": func() {
ts, _ := data.proxiesView.GetSelection()
if _, iter, ok := ts.GetSelected(); ok {
editProxy(iter, func() {})
}
},
"on_remove_proxy_signal": func() {
ts, _ := data.proxiesView.GetSelection()
if _, iter, ok := ts.GetSelected(); ok {
data.proxies.Remove(iter)
}
},
"on_remove_pin_signal": func() {
ts, _ := data.pinsView.GetSelection()
if _, iter, ok := ts.GetSelected(); ok {
data.pins.Remove(iter)
}
},
"on_add_proxy_signal": func() {
iter := data.proxies.Append()
data.proxies.SetValue(iter, 0, "tor-auto://")
data.proxies.SetValue(iter, 1, "tor-auto://")
ts, _ := data.proxiesView.GetSelection()
ts.SelectIter(iter)
editProxy(iter, func() {
data.proxies.Remove(iter)
})
},
"on_edit_activate_proxy_signal": func(_ gtki.TreeView, path gtki.TreePath) {
iter, err := data.proxies.GetIter(path)
if err == nil {
editProxy(iter, func() {})
}
},
"on_cancel_signal": func() {
u.buildAccountsMenu()
data.dialog.Destroy()
},
"on_import_key_signal": func() {
u.importKeysForDialog(account, data.dialog)
},
"on_import_fpr_signal": func() {
u.importFingerprintsForDialog(account, data.dialog)
},
"on_export_key_signal": func() {
u.exportKeysForDialog(account, data.dialog)
},
"on_export_fpr_signal": func() {
u.exportFingerprintsForDialog(account, data.dialog)
},
})
data.notebook.SetCurrentPage(0)
data.notebook.SetShowTabs(u.config.AdvancedOptions)
if !u.config.AdvancedOptions {
p2.Hide()
p3.Hide()
p4.Hide()
}
data.dialog.SetTransientFor(u.window)
data.dialog.ShowAll()
}