/
main.go
171 lines (144 loc) · 3.79 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
package main
import (
"context"
"crypto/sha256"
"flag"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
"github.com/atotto/clipboard"
"github.com/justwatchcom/gopass/backend/gpg/cli"
"github.com/justwatchcom/gopass/store/sub"
"github.com/marcusolsson/tui-go"
)
const logo = `
________
/ _____/ ____ ___________ ______ ______
/ \ ___ / _ \\____ \__ \ / ___// ___/
\ \_\ ( <_> ) |_> > __ \_\___ \ \___ \
\______ /\____/| __(____ /____ >____ >
\/ |__| \/ \/ \/ `
func main() {
unclipFlag := flag.Bool("u", false, "")
clipboardTimeout := flag.Int("t", 15, "Clipboard timeout in seconds")
flag.Parse()
// Process spawned to clear clipboard
if *unclipFlag {
unclip(*clipboardTimeout)
os.Exit(0)
}
gpg, err := cli.New(context.Background(), cli.Config{})
if err != nil {
panic(err)
}
store := sub.New("root", os.Getenv("HOME")+"/.password-store", gpg)
secrets, err := store.List("")
if err != nil {
panic(err)
}
status := tui.NewStatusBar("Enter - Copy to clipboard, Right Arrow - Show, ESC - Quit.")
storeList := tui.NewList()
storeList.AddItems(secrets...)
storeList.SetFocused(true)
storeList.SetSelected(0)
search := tui.NewEntry()
search.SetFocused(true)
search.OnChanged(func(entry *tui.Entry) {
foundItems := make([]string, 0)
allItems, err := store.List("")
if err != nil {
status.SetText(err.Error())
}
for _, item := range allItems {
if strings.Contains(item, entry.Text()) {
foundItems = append(foundItems, item)
}
}
storeList.RemoveItems()
storeList.AddItems(foundItems...)
if len(foundItems) > 0 {
storeList.SetSelected(0)
}
})
form := tui.NewGrid(0, 0)
form.AppendRow(tui.NewLabel("Search:"))
form.AppendRow(search)
window := tui.NewVBox(
tui.NewPadder(10, 1, tui.NewLabel(logo)),
tui.NewPadder(1, 1, storeList),
tui.NewPadder(1, 1, form),
)
window.SetBorder(true)
wrapper := tui.NewVBox(
tui.NewSpacer(),
window,
tui.NewSpacer(),
)
content := tui.NewHBox(tui.NewSpacer(), wrapper, tui.NewSpacer())
root := tui.NewVBox(
content,
status,
)
ui := tui.New(root)
ui.SetKeybinding("Esc", func() { ui.Quit() })
// Show password
ui.SetKeybinding("Right", func() {
secret, err := store.Get(context.Background(), storeList.SelectedItem())
if err != nil {
status.SetText(err.Error())
return
}
ui.Quit()
fmt.Printf("Password for %v: %v\n", storeList.SelectedItem(), secret.String())
})
// Copy to clipboard
storeList.OnItemActivated(func(list *tui.List) {
secret, err := store.Get(context.Background(), list.SelectedItem())
if err != nil {
status.SetText(err.Error())
return
}
if err := clipboard.WriteAll(secret.String()); err != nil {
panic(err)
}
err = delayedClearClipboard(secret.String(), *clipboardTimeout)
if err != nil {
panic(err)
}
ui.Quit()
fmt.Printf("Password for %v copied to clipboard. Will clear in %v seconds.\n",
storeList.SelectedItem(),
*clipboardTimeout)
})
if err := ui.Run(); err != nil {
panic(err)
}
}
func delayedClearClipboard(content string, timeout int) error {
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(content)))
cmd := exec.Command(os.Args[0], "-u", strconv.Itoa(timeout))
// https://groups.google.com/d/msg/golang-nuts/shST-SDqIp4/za4oxEiVtI0J
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
cmd.Env = append(os.Environ(), "GOPASS_TUI_UNCLIP_CHECKSUM="+hash)
return cmd.Start()
}
func unclip(timeout int) {
checksum := os.Getenv("GOPASS_TUI_UNCLIP_CHECKSUM")
time.Sleep(time.Second * time.Duration(timeout))
curr, err := clipboard.ReadAll()
if err != nil {
panic(err)
}
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(curr)))
if hash == checksum {
if err := clipboard.WriteAll(""); err != nil {
panic(err)
}
}
}