-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.go
233 lines (213 loc) · 5.98 KB
/
app.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
package main
import (
"context"
"errors"
"fmt"
"log"
"path/filepath"
"sync"
"github.com/getlantern/systray"
"github.com/masahide/OmniSSHAgent/pkg/cygwinsocket"
"github.com/masahide/OmniSSHAgent/pkg/namedpipe"
"github.com/masahide/OmniSSHAgent/pkg/pageant"
"github.com/masahide/OmniSSHAgent/pkg/sshkey"
"github.com/masahide/OmniSSHAgent/pkg/sshutil"
"github.com/masahide/OmniSSHAgent/pkg/store"
"github.com/masahide/OmniSSHAgent/pkg/unix"
"github.com/masahide/OmniSSHAgent/pkg/winopen"
//"github.com/masahide/OmniSSHAgent/pkg/wintray"
"github.com/masahide/OmniSSHAgent/icon"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App application struct
type App struct {
ctx context.Context
//ti *wintray.TrayIcon
keyRing *sshutil.KeyRing
settings *store.Settings
wg sync.WaitGroup
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
func (a *App) systrayOnRedy() {
systray.SetIcon(icon.Data)
systray.SetTitle(AppName)
systray.SetTooltip(AppName)
mShowWindow := systray.AddMenuItem("ShowWindow", "Show main window")
mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
mLogCheckBox := systray.AddMenuItemCheckbox("Debug log", "Enable debug log file output", false)
mLogDirOpen := systray.AddMenuItem("Open log directory", "open log directory")
mLogDirOpen.Disable()
go func() {
for {
select {
case <-mShowWindow.ClickedCh:
a.showWindow()
case <-mLogCheckBox.ClickedCh:
if mLogCheckBox.Checked() {
mLogCheckBox.Uncheck()
log.Print("Disable debug log")
Logger.SetEnable(false)
mLogDirOpen.Disable()
} else {
mLogCheckBox.Check()
Logger.SetEnable(true)
log.Print("Enable debug log")
mLogDirOpen.Enable()
}
case <-mQuit.ClickedCh:
log.Print("Quit was clicked on the menu")
a.Quit()
return
case <-mLogDirOpen.ClickedCh:
dir := filepath.Dir(Logger.FilePath)
winopen.Open(dir)
}
}
}()
}
func (a *App) systrayOnExit() {
//log.Print("systrayOnExit")
Logger.Close()
a.wg.Done()
}
// startup is called at application startup
func (a *App) startup(ctx context.Context) {
// Perform your setup here
a.ctx = ctx
/*
a.ti = wintray.NewTrayIcon()
a.ti.BalloonClickFunc = a.showWindow
a.ti.TrayClickFunc = a.showWindow
go a.ti.RunTray()
*/
a.wg.Add(1)
go func() {
defer func() {
if r := recover(); r != nil {
log.Printf("recover:%v", r)
}
a.Quit()
}()
systray.Run(a.systrayOnRedy, a.systrayOnExit)
}()
debug := false
a.keyRing = sshutil.NewKeyRing(a.settings)
if err := a.keyRing.AddKeys(); err != nil {
log.Printf("KeyRing.AddKeys err: %s", err)
}
a.keyRing.NotifyCallback = a.notice
pa := &pageant.Pageant{
ExtendedAgent: a.keyRing,
AppName: AppName,
Debug: debug,
CheckFunc: a.showWindow,
}
if a.settings.PageantAgent {
go pa.RunAgent()
}
log.Println("Starting pageant...")
if a.settings.NamedPipeAgent {
pipeName := ""
na := &namedpipe.NamedPipe{ExtendedAgent: a.keyRing, Debug: debug, Name: pipeName}
log.Println("Starting NamedPipe agent..")
go na.RunAgent()
}
if a.settings.UnixSocketAgent {
ua := &unix.DomainSock{Agent: a.keyRing, Debug: debug, Path: a.settings.UnixSocketPath}
go ua.RunAgent()
log.Println("Start Unix domain socket agent..")
}
if a.settings.CygWinAgent {
ca := &cygwinsocket.CygwinSock{Agent: a.keyRing, Debug: debug, Path: a.settings.CygWinSocketPath}
go ca.RunAgent()
log.Println("Starting Cygwin unix domain socket agent..")
}
}
func (a *App) notice(action string, data interface{}) {
switch action {
case "Add", "Remove", "RemoveAll":
//a.ti.ShowBalloonNotification(action, sshutil.JSONDump(data))
runtime.EventsEmit(a.ctx, "LoadKeysEvent")
}
}
func (a *App) OpenFile() (string, error) {
return runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Select a private key file",
})
}
// domReady is called after the front-end dom has been loaded
func (a *App) domReady(ctx context.Context) {
// Add your action here
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s!", name)
}
func (a *App) showWindow() {
//runtime.LogDebug(a.ctx, "showWindow")
runtime.WindowShow(a.ctx)
}
func (a *App) Quit() {
log.Print("call a.Quit")
runtime.Quit(a.ctx)
}
// shutdown はruntime.Quitから呼ばれる
func (a *App) shutdown(ctx context.Context) {
log.Print("shutdown")
systray.Quit()
a.wg.Wait()
}
func (a *App) AddLocalFile(pk sshkey.PrivateKeyFile) error {
pk.Name = filepath.Base(pk.FilePath)
pk.StoreType = sshutil.LocalStore
//log.Printf("AddLocalFile:%s", sshutil.JSONDump(pk))
id, err := a.keyRing.AddKeySettings(pk)
if err != nil {
return err
}
if err := a.keyRing.AddKey(id); err != nil {
return err
}
return nil
}
func (a *App) DeleteKey(sha256 string) error {
c, err := runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.QuestionDialog,
Title: "Delete?",
Message: "Do you really want to delete this key?",
})
if err != nil {
return err
}
//runtime.LogDebug(a.ctx, c)
if c != "Yes" {
return errors.New("cancel")
}
if err := a.keyRing.RemoveKey(sha256); err != nil {
return err
}
return a.keyRing.DeleteKeySettings(sha256)
}
func (a *App) KeyList() ([]sshkey.PrivateKeyFile, error) {
return a.keyRing.KeyList()
}
func (a *App) CheckKeyType(filePath, passphrase string) (*sshkey.PrivateKeyFile, error) {
return sshutil.CheckKeyType(filePath, passphrase)
}
func (a *App) GetSettings() store.SaveData {
return a.settings.SaveData
}
func (a *App) Save(s store.SaveData) error {
a.settings.SaveData.StartHidden = s.StartHidden
a.settings.SaveData.PageantAgent = s.PageantAgent
a.settings.SaveData.NamedPipeAgent = s.NamedPipeAgent
a.settings.SaveData.UnixSocketAgent = s.UnixSocketAgent
a.settings.SaveData.UnixSocketPath = s.UnixSocketPath
a.settings.SaveData.CygWinAgent = s.CygWinAgent
a.settings.SaveData.CygWinSocketPath = s.CygWinSocketPath
a.settings.SaveData.ProxyModeOfNamedPipe = s.ProxyModeOfNamedPipe
return a.settings.Save()
}