-
Notifications
You must be signed in to change notification settings - Fork 66
/
main.go
220 lines (200 loc) · 4.92 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
package main
//go:generate goversioninfo -icon=assets/icon.ico
import (
"context"
"flag"
"github.com/buptczq/WinCryptSSHAgent/capi"
"os"
"os/signal"
"path/filepath"
"sync"
"time"
"github.com/Microsoft/go-winio"
"github.com/buptczq/WinCryptSSHAgent/app"
"github.com/buptczq/WinCryptSSHAgent/sshagent"
"github.com/buptczq/WinCryptSSHAgent/utils"
notify "github.com/hattya/go.notify"
notification "github.com/hattya/go.notify/windows"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/registry"
)
const agentTitle = "WinCrypt SSH Agent v1.1.9"
var applications = []app.Application{
new(app.PubKeyView),
new(app.WSL),
new(app.VSock),
new(app.Cygwin),
new(app.NamedPipe),
new(app.Pageant),
new(app.XShell),
}
var installHVService = flag.Bool("i", false, "Install Hyper-V Guest Communication Services")
var disableCapi = flag.Bool("disable-capi", false, "Disable Windows Crypto API")
var disablePINCache = flag.Bool("disable-pin-cache", false, "Clear the Smart Card PIN Cache after each operation")
func installService() {
if !utils.IsAdmin() {
err := utils.RunMeElevated()
if err != nil {
utils.MessageBox("Install Service Error:", err.Error(), utils.MB_ICONERROR)
}
return
}
err := winio.RunWithPrivilege(winio.SeRestorePrivilege, func() error {
gcs, err := registry.OpenKey(registry.LOCAL_MACHINE, utils.HyperVServiceRegPath, registry.ALL_ACCESS)
if err != nil {
return err
}
defer gcs.Close()
agentSrv, _, err := registry.CreateKey(gcs, utils.HyperVServiceGUID.String(), registry.ALL_ACCESS)
if err != nil {
return err
}
err = agentSrv.SetStringValue("ElementName", "WinCryptSSHAgent")
if err != nil {
return err
}
return nil
})
if err != nil {
utils.MessageBox("Install Service Error:", err.Error(), utils.MB_ICONERROR)
} else {
utils.MessageBox("Install Service Success:", "Please reboot your computer to take effect!", utils.MB_ICONINFORMATION)
}
return
}
func initDebugLog() {
if os.Getenv("WCSA_DEBUG") == "1" {
home, err := os.UserHomeDir()
if err != nil {
return
}
f, err := os.OpenFile(filepath.Join(home, "WCSA_DEBUG.log"), os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0664)
if err != nil {
return
}
err = windows.SetStdHandle(windows.STD_OUTPUT_HANDLE, windows.Handle(f.Fd()))
if err != nil {
return
}
err = windows.SetStdHandle(windows.STD_ERROR_HANDLE, windows.Handle(f.Fd()))
if err != nil {
return
}
os.Stdout = f
os.Stderr = f
}
}
func main() {
flag.Parse()
utils.SetProcessSystemDpiAware()
initDebugLog()
if *installHVService {
installService()
return
}
// hyper-v
hvClient := false
hvConn, err := utils.ConnectHyperV()
if err == nil {
hvConn.Close()
hvClient = true
}
// systray
notifier, err := initSystray(hvClient)
if err != nil {
utils.MessageBox("Error:", err.Error(), utils.MB_ICONERROR)
return
}
sysTray := notifier.Sys().(*notification.NotifyIcon)
menu := NewMenu(sysTray)
// context
ctx, cancel := context.WithCancel(context.Background())
capi.SetDisablePINCache(*disablePINCache)
// agent
var ag agent.Agent
if hvClient {
ag = sshagent.NewHVAgent()
} else if *disableCapi {
ag = sshagent.NewKeyRingAgent()
} else {
cag := new(sshagent.CAPIAgent)
defer cag.Close()
defaultAgent := sshagent.NewKeyRingAgent()
ag = sshagent.NewWrappedAgent(defaultAgent, []agent.Agent{agent.Agent(cag)})
}
ctx = context.WithValue(ctx, "agent", ag)
ctx = context.WithValue(ctx, "hv", hvClient)
server := &sshagent.Server{
Agent: ag,
}
// application
wg := new(sync.WaitGroup)
for _, v := range applications {
v.Menu(menu.Register)
wg.Add(1)
go func(application app.Application) {
err := application.Run(ctx, server.SSHAgentHandler)
if err != nil {
utils.MessageBox(application.AppId().String()+" Error:", err.Error(), utils.MB_ICONWARNING)
}
wg.Done()
}(v)
}
// interrupt signal
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
// show systray
menu.menu.Sep()
menu.menu.Item("Quit", app.MENU_QUIT)
err = sysTray.Add()
if err != nil {
utils.MessageBox("Error:", err.Error(), utils.MB_ICONERROR)
goto cleanup
}
// event
for {
select {
case clicked := <-sysTray.Menu:
if clicked.ID == app.MENU_QUIT {
goto cleanup
}
menu.Handle(app.AppId(clicked.ID))
case <-sysTray.Balloon:
continue
case <-quit:
goto cleanup
}
}
cleanup:
sysTray.Close()
cancel()
done := make(chan struct{})
go func() {
wg.Wait()
done <- struct{}{}
}()
select {
case <-time.After(time.Second * 5):
case <-done:
}
}
func initSystray(hv bool) (notify.Notifier, error) {
icon, err := notification.LoadIcon(1)
if err != nil {
return nil, err
}
title := agentTitle
if hv {
title += " (Hyper-V)"
}
n, err := notification.NewNotifier(title, icon)
if err != nil {
return nil, err
}
n.Register("info", notification.IconInfo, map[string]interface{}{
"windows:sound": false,
})
utils.RegisterNotifier(n)
return n, nil
}