-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
227 lines (191 loc) · 5.89 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
package main
import (
_ "embed"
"flag"
"fmt"
"os"
"os/signal"
"regexp"
"runtime"
"strings"
"syscall"
"github.com/amurzeau/ssh-agent-bridge/agent"
"github.com/amurzeau/ssh-agent-bridge/agent/cygwinUnixSocket"
"github.com/amurzeau/ssh-agent-bridge/agent/namedPipe"
"github.com/amurzeau/ssh-agent-bridge/agent/pageant"
"github.com/amurzeau/ssh-agent-bridge/agent/pageantPipe"
"github.com/amurzeau/ssh-agent-bridge/agent/wslUnixSocket"
"github.com/amurzeau/ssh-agent-bridge/log"
"github.com/getlantern/systray"
)
//go:embed assets/oxygen-status-wallet-open.ico
var assetsOxygenStatusWalletOpen []byte
var (
argFrom *string
argTo *string
argPipePath *string
argCygwinUnixSocketPath *string
argWslUnixSocketPath *string
agentContext = agent.CreateAgent()
)
var sshAgentFromMap = map[string]func(*agent.AgentContext){
"pipe": func(ctx *agent.AgentContext) {
namedPipe.ServePipe(*argPipePath, ctx)
},
"cygwin": func(ctx *agent.AgentContext) {
cygwinUnixSocket.ServeUnixSocket(*argCygwinUnixSocketPath, ctx)
},
"wsl": func(ctx *agent.AgentContext) {
wslUnixSocket.ServeWslUnixSocket(*argWslUnixSocketPath, ctx)
},
"pageant": func(ctx *agent.AgentContext) {
pageant.ServePageant(ctx)
},
"pageant-pipe": func(ctx *agent.AgentContext) {
pageantPipe.ServePageantPipe(ctx)
},
}
var sshAgentToMap = map[string]func(*agent.AgentContext) error{
"pipe": func(ctx *agent.AgentContext) error {
return namedPipe.ClientPipe(*argPipePath, ctx)
},
"cygwin": func(ctx *agent.AgentContext) error {
return cygwinUnixSocket.ClientUnixSocket(*argCygwinUnixSocketPath, ctx)
},
"wsl": func(ctx *agent.AgentContext) error {
return wslUnixSocket.ClientWslUnixSocket(*argWslUnixSocketPath, ctx)
},
"pageant": func(ctx *agent.AgentContext) error {
return pageant.ClientPageant(ctx)
},
"pageant-pipe": func(ctx *agent.AgentContext) error {
return pageantPipe.ClientPageantPipe(ctx)
},
}
func keys[T any, Key comparable](m map[Key]T) []Key {
j := 0
keys := make([]Key, len(m))
for k := range m {
keys[j] = k
j++
}
return keys
}
func remove[T comparable](l []T, item T) []T {
for i, other := range l {
if other == item {
return append(l[:i], l[i+1:]...)
}
}
return l
}
var reCygwinTmpDir = regexp.MustCompile(`^/tmp`)
var reCygwinDriveDir = regexp.MustCompile(`^(/cygdrive)?/([a-z])/`)
func convertCygwinPathToWindows(path string) string {
tmpPath := os.TempDir()
nativePath := path
nativePath = reCygwinTmpDir.ReplaceAllLiteralString(nativePath, tmpPath)
nativePath = reCygwinDriveDir.ReplaceAllString(nativePath, "$2:/")
if path != nativePath {
log.Debugf("converting cygwin path from %s to %s",
path,
nativePath)
}
return nativePath
}
func main() {
argFrom = flag.String("from", "",
fmt.Sprintf("comma-separated list of endpoint to listen on, available: all, %s (cygwin also work for Git for Windows)",
strings.Join(keys(sshAgentFromMap), ", ")))
argTo = flag.String("to", "pageant",
fmt.Sprintf("endpoint to use as upstream agent, available: %s (cygwin also work for Git for Windows)",
strings.Join(keys(sshAgentToMap), ", ")))
argPipePath = flag.String("pipe", `\\.\pipe\openssh-ssh-agent`, "path to the pipe to use for pipe mode")
argCygwinUnixSocketPath = flag.String("cygwin-socket", os.Getenv("SSH_AUTH_SOCK"), "path to the ssh-agent unix socket for cygwin-ssh-agent mode")
argWslUnixSocketPath = flag.String("wsl-socket", os.Getenv("SSH_AUTH_SOCK"), "path to the WSL ssh-agent unix socket for wsl-ssh-agent mode")
argDebug := flag.Bool("debug", false, "enable debug logs")
argNoGuiError := flag.Bool("no-gui-error", false, "don't show a message box for fatal error")
flag.Parse()
if *argDebug {
log.Level = log.Debug
}
if *argNoGuiError {
log.UseMessageBoxForFatal = false
}
if *argFrom == "" {
log.Fatalf("--from is required, see help with --help")
}
// By default, listen on every possible supported endpoint except the one used as upstream agent
if *argFrom == "all" {
fromKeys := keys(sshAgentFromMap)
fromKeys = remove(fromKeys, *argTo)
*argFrom = strings.Join(fromKeys, ",")
}
// Convert cygwin/msys paths to native Windows path
if runtime.GOOS == "windows" {
*argCygwinUnixSocketPath = convertCygwinPathToWindows(*argCygwinUnixSocketPath)
*argWslUnixSocketPath = convertCygwinPathToWindows(*argWslUnixSocketPath)
}
systray.Run(onReady, onExit)
}
func onReady() {
systray.SetIcon(assetsOxygenStatusWalletOpen)
systray.SetTitle("SSH Agent Bridge")
systray.SetTooltip("SSH Agent Bridge")
mExit := systray.AddMenuItem("Exit", "Exit SSH Agent Bridge")
go func() {
<-mExit.ClickedCh
agentContext.Stop()
<-mExit.ClickedCh
log.Debugf("agentContext: hard exit")
os.Exit(0)
}()
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
agentContext.Stop()
<-sigs
log.Debugf("agentContext: hard exit")
os.Exit(0)
}()
go func() {
<-agentContext.Done()
agentContext.Wait()
systray.Quit()
}()
// Listen on all requested endpoints
fromValues := strings.Split(strings.ReplaceAll(*argFrom, " ", ""), ",")
for _, from := range fromValues {
if serverHandler, ok := sshAgentFromMap[from]; ok {
log.Infof("Handling ssh agent queries from %s", from)
agentContext.Go(func() {
serverHandler(&agentContext)
})
} else {
log.Fatalf("Bad --from value %s, available: %s",
from,
strings.Join(keys(sshAgentFromMap), ", "))
agentContext.Stop()
}
}
if clientHandler, ok := sshAgentToMap[*argTo]; ok {
log.Infof("Forwarding ssh agent queries to %s", *argTo)
// Run upstream agent handler
agentContext.Go(func() {
err := clientHandler(&agentContext)
if err != nil {
log.Fatalf("error with upstream agent: %v", err)
agentContext.Stop()
}
})
} else {
log.Fatalf("Bad --to value %s, available: %s",
*argTo,
strings.Join(keys(sshAgentToMap), ", "))
agentContext.Stop()
}
}
func onExit() {
os.Exit(0)
}