-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
543 lines (490 loc) · 15.2 KB
/
main.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"regexp"
"sort"
"strings"
"syscall/js"
"time"
"github.com/vkuznet/ecm/crypt"
vt "github.com/vkuznet/ecm/vault"
//dom "honnef.co/go/js/dom/v2"
)
// Record represent map of key-valut pairs
// type Record map[string]string
// VaultRecord represents vault record subset suitable for web UI
// type VaultRecord struct {
// ID string // record ID
// Map Record // record map (key-vault pairs)
// }
// LoginRecord represent login credentials
type LoginRecord struct {
ID string
Login string
Password string
Name string
Note string
Tags string
URL string
}
// RecordMap type defines our ECM record map
type RecordMap map[string]LoginRecord
// RecordsManager holds ECM records
type RecordsManager struct {
Map RecordMap
RenewInterval int64
Expire int64
}
// global records manager which holds all vault records
var recordsManager *RecordsManager
// helper function to get ECM records
func (mgr *RecordsManager) update(url, cipher, password string) error {
if recordsManager.Map == nil || recordsManager.Expire < time.Now().Unix() {
rmap, err := getRecords(url, cipher, password)
if err != nil {
rmap := make(RecordMap)
rid := "12345"
lrec := LoginRecord{
ID: rid,
Login: "Error",
Name: fmt.Sprintf("url: %s, error: %v", url, err),
}
rmap[rid] = lrec
}
mgr.Map = rmap
mgr.Expire = time.Now().Unix() + mgr.RenewInterval
return err
}
return nil
}
// helper function to get ECM records from given URL
func getRecords(url, cipher, password string) (RecordMap, error) {
rmap := make(RecordMap)
// Make the HTTP request
client, err := httpClient()
if err != nil {
return rmap, err
}
// get results from our url
res, err := client.Get(url)
if err != nil {
return rmap, err
}
defer res.Body.Close()
// Read the response body
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return rmap, err
}
// records represent list of file names
var records [][]byte
err = json.Unmarshal(data, &records)
if err != nil {
return rmap, err
}
for _, rec := range records {
data, err := crypt.Decrypt(rec, password, cipher)
if err != nil {
return rmap, err
}
var vrec vt.VaultRecord
err = json.Unmarshal(data, &vrec)
lrec := LoginRecord{
ID: vrec.ID,
Login: vrec.Map["Login"],
Password: vrec.Map["Password"],
Note: vrec.Map["Note"],
Name: vrec.Map["Name"],
Tags: vrec.Map["Tags"],
URL: vrec.Map["URL"],
}
rmap[vrec.ID] = lrec
}
return rmap, nil
}
// main function sets JS functions
func main() {
// log time, filename, and line number
log.SetFlags(log.LstdFlags | log.Lshortfile)
// initialize records manager with renew interval of 60 seconds
recordsManager = &RecordsManager{RenewInterval: 60}
// define out decode JS function to be bound to decoreWrapper Go counterpart
//js.Global().Set("Lock", lockWrapper())
js.Global().Set("getLogin", loginWrapper())
js.Global().Set("getPassword", passwordWrapper())
js.Global().Set("records", recordsWrapper())
js.Global().Set("uploadFile", uploadFileWrapper())
js.Global().Set("addRecord", actionWrapper("login_record"))
js.Global().Set("addJsonRecord", actionWrapper("json_record"))
js.Global().Set("addNote", actionWrapper("note_record"))
js.Global().Set("addCard", actionWrapper("card_record"))
js.Global().Set("addVault", actionWrapper("new_vault"))
js.Global().Set("syncHosts", actionWrapper("sync_hosts"))
js.Global().Set("showRecords", actionWrapper("show_records"))
js.Global().Set("generatePassword", actionWrapper("gen_password"))
// js.Global().Call("showRecords")
<-make(chan bool)
}
// VaultPassword keeps copy of vault password
var VaultPassword string
// recordsWraper function performs business logic, i.e.
// it recordss given input obtained from JS upstream code
func recordsWrapper() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
server := args[0].String()
vault := args[1].String()
cipher := args[2].String()
password := args[3].String()
pattern := args[4].String()
pageUrl := args[5].String()
// construct URL, e.g.
// http://127.0.0.1:8888/vault/Primary/records
// for exact IP:PORT values please consult
// extension/manifest.json and extension/index.html
// and/or set it up in settings menu of wasm extension
url := fmt.Sprintf("%s/vault/%s/records", server, vault)
if VaultPassword == "" {
VaultPassword = password
}
// Create and return the Promise object
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
go RecordsHandler(url, cipher, VaultPassword, pattern, pageUrl, args)
return nil
})
// define where we should put our data
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler)
})
}
// uploadFileWraper function performs file uploadFile
func uploadFileWrapper() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
fname := args[0].String()
size := args[1].Int()
ftype := args[2].String()
content := args[3].String() // adjust accordingly with utils.js:readFile function
// Create and return the Promise object
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
go uploadFile(fname, size, ftype, content)
return nil
})
// define where we should put our data
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler)
})
}
// ErrorHandler handles JS errors
func ErrorHandler(reject js.Value, err error) {
// Handle errors here too
errorConstructor := js.Global().Get("Error")
errorObject := errorConstructor.New(err.Error())
reject.Invoke(errorObject)
}
// wrapper function to add new vault
func actionWrapper(action string) js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
// Create and return the Promise object
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
go ActionHandler(action, args)
return nil
})
// define where we should put our data
promiseConstructor := js.Global().Get("Promise")
return promiseConstructor.New(handler)
})
}
// ActionHandler handles vault action
func ActionHandler(action string, args []js.Value) {
resolve := args[0]
reject := args[1]
// perform action with our action
var data []byte
var err error
if action == "show_records" {
data, err = showRecords()
} else if action == "login_record" {
data, err = loginRecord()
} else if action == "json_record" {
data, err = jsonRecord()
} else if action == "card_record" {
data, err = cardRecord()
} else if action == "note_record" {
data, err = noteRecord()
} else if action == "sync_host" {
data, err = syncHosts()
} else if action == "new_vault" {
data, err = createVault()
} else if action == "gen_password" {
data, err = newPassword()
} else {
data, err = defaultAction()
}
if err != nil {
ErrorHandler(reject, err)
return
}
// "data" is a byte slice, so we need to convert it to a JS Uint8Array object
arrayConstructor := js.Global().Get("Uint8Array")
dataJS := arrayConstructor.New(len(data))
js.CopyBytesToJS(dataJS, data)
// Create a Response object and pass the data
responseConstructor := js.Global().Get("Response")
response := responseConstructor.New(dataJS)
// Resolve the Promise
resolve.Invoke(response)
}
// wrapper function to generate new password
func genPasswordWrapper() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
const voc string = "abcdfghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numbers string = "0123456789"
// const symbols string = "!@#$%&*+_-="
size := args[0].Int()
chars := voc + numbers
password := generatePassword(size, chars)
document := js.Global().Get("document")
docRecords := document.Call("getElementById", "new-password")
docRecords.Set("innerHTML", password)
return ""
})
}
// wrapper function to return password for given record id
func passwordWrapper() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
rid := args[0].String()
if lrec, ok := recordsManager.Map[rid]; ok {
return lrec.Password
}
return ""
})
}
// wrapper function to return password for given record id
func loginWrapper() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
rid := args[0].String()
if lrec, ok := recordsManager.Map[rid]; ok {
return lrec.Login
}
return ""
})
}
// helper function to match url with pattern
func urlMatch(url, pattern string) bool {
purl := strings.Replace(url, "http://", "", -1)
purl = strings.Replace(purl, "https://", "", -1)
arr := strings.Split(purl, "/")
for _, v := range strings.Split(arr[0], ".") {
if matched, err := regexp.MatchString(v, pattern); err == nil && matched {
return true
}
}
return false
}
// helper function to match url parts with LoginRecord
func urlMatchRecord(url string, rec LoginRecord) bool {
if urlMatch(url, rec.Name) ||
urlMatch(url, rec.URL) ||
urlMatch(url, rec.Tags) ||
urlMatch(url, rec.Note) {
return true
}
return false
}
// update records within DOM document
func updateRecords(url, cipher, passphrase, pattern, pageUrl string, extention bool) ([]string, error) {
var rids []string
err := recordsManager.update(url, cipher, passphrase)
if err != nil {
return rids, err
}
// get pageUrl
document := js.Global().Get("document")
// TODO: I should find a way to obtain page URL
// in wasm there is no way to access page url
// pageUrl := ""
docRecords := document.Call("getElementById", "records")
docRecords.Set("innerHTML", "")
ul := document.Call("createElement", "ul")
ul.Call("setAttribute", "class", "records")
docRecords.Call("appendChild", ul)
count := 0
nrec := 5 // total number of records to show
var mkeys []string
for k, _ := range recordsManager.Map {
mkeys = append(mkeys, k)
}
sort.Strings(mkeys)
// for key, lrec := range recordsManager.Map {
for _, key := range mkeys {
lrec, ok := recordsManager.Map[key]
if !ok {
continue
}
count += 1
// skip records which does not match page url
if pageUrl != "" && pattern == "" {
if !urlMatchRecord(pageUrl, lrec) {
continue
}
}
name := lrec.Name
login := lrec.Login
password := lrec.Password
rurl := lrec.URL
tags := lrec.Tags
if pattern != "" {
// TODO: we may need to fetch only appropriate records from server
// instead of filtering here
if !(strings.Contains(name, pattern) ||
strings.Contains(login, pattern) ||
strings.Contains(password, pattern) ||
strings.Contains(rurl, pattern) ||
strings.Contains(tags, pattern)) {
continue
}
}
if count > nrec {
continue
}
rids = append(rids, key)
// construct frontend UI
li := document.Call("createElement", "li")
li.Call("setAttribute", "class", "item")
ul.Call("appendChild", li)
nameDiv := document.Call("createElement", "div")
nameDiv.Set("innerHTML", "Name: "+name)
loginDiv := document.Call("createElement", "div")
loginDiv.Set("innerHTML", "Login: "+login)
passDiv := document.Call("createElement", "div")
pid := "pid-" + key
passDiv.Set("id", pid)
passDiv.Call("setAttribute", "class", "hide")
// add buttons
buttons := document.Call("createElement", "div")
buttons.Call("setAttribute", "class", "button-right")
// add show button
button := document.Call("createElement", "button")
bid := "bid-" + key
button.Set("id", bid)
if extention {
button.Call("setAttribute", "class", "label is-focus is-pointer")
} else {
button.Call("setAttribute", "class", "button is-secondary is-small")
}
button.Set("innerHTML", "Show password")
var callback js.Func
callback = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
doc := document.Call("getElementById", pid)
button := document.Call("getElementById", bid)
if button.Get("innerHTML").String() == "Show password" {
doc.Call("setAttribute", "class", "show-inline")
doc.Set("innerHTML", "Password: "+password)
button.Set("innerHTML", "Hide password")
} else {
doc.Call("setAttribute", "class", "show-inline")
doc.Set("innerHTML", "")
button.Set("innerHTML", "Show password")
}
return nil
})
button.Call("addEventListener", "click", callback)
if extention && password != "" {
buttons.Call("appendChild", button)
} else {
// create grid with two columns and add button to second one
grid := document.Call("createElement", "div")
grid.Call("setAttribute", "class", "is-row")
div := document.Call("createElement", "div")
div.Call("setAttribute", "class", "is-col is-80")
img := document.Call("createElement", "img")
if strings.Contains(tags, "file") {
img.Set("src", "static/images/file-32.png")
} else {
img.Set("src", "static/images/notes-32.png")
}
div.Call("appendChild", img)
txt := document.Call("createElement", "span")
txt.Set("innerHTML", fmt.Sprintf(" %s", key))
div.Call("appendChild", txt)
grid.Call("appendChild", div)
div = document.Call("createElement", "div")
div.Call("setAttribute", "class", "is-col is-20")
div.Call("appendChild", button)
grid.Call("appendChild", div)
// add grid to buttons
buttons.Call("appendChild", grid)
}
// add autofill button
if extention {
button = document.Call("createElement", "button")
aid := "autofill-" + key
button.Set("id", aid)
button.Call("setAttribute", "class", "label autofill is-bold")
button.Set("innerHTML", "Autofill")
button.Set("RecordID", key)
buttons.Call("appendChild", button)
}
siteDiv := document.Call("createElement", "div")
siteDiv.Set("innerHTML", "URL: "+rurl)
if !extention {
li.Call("append", buttons)
}
li.Call("append", nameDiv)
li.Call("append", siteDiv)
li.Call("append", loginDiv)
li.Call("append", passDiv)
if extention {
li.Call("append", buttons)
}
}
if count > nrec {
moreDiv := document.Call("createElement", "div")
if extention {
moreDiv.Set("innerHTML", fmt.Sprintf("Total vault records: %d<br>URL %s<br>Pattern: %s", count, pageUrl, pattern))
} else {
moreDiv.Set("innerHTML", fmt.Sprintf("<b>Total vault records: %d</b><br>", count))
}
docRecords.Call("append", moreDiv)
}
// if we do not have any records matched above we'll use first nrec ones
if len(rids) == 0 {
count := 0
for key, _ := range recordsManager.Map {
count += 1
if count > nrec {
break
}
rids = append(rids, key)
}
}
return rids, nil
}
// RecordsHandler handles asynchronously HTTP requests
func RecordsHandler(url, cipher, passphrase, pattern, pageUrl string, args []js.Value) {
resolve := args[0]
reject := args[1]
// place records within DOM page
extention := true
rids, err := updateRecords(url, cipher, passphrase, pattern, pageUrl, extention)
if err != nil {
ErrorHandler(reject, err)
return
}
adata, err := json.Marshal(rids)
if err != nil {
ErrorHandler(reject, err)
return
}
// "data" is a byte slice, so we need to convert it to a JS Uint8Array object
arrayConstructor := js.Global().Get("Uint8Array")
dataJS := arrayConstructor.New(len(adata))
js.CopyBytesToJS(dataJS, adata)
// Create a Response object and pass the data
responseConstructor := js.Global().Get("Response")
response := responseConstructor.New(dataJS)
// Resolve the Promise
resolve.Invoke(response)
}