-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathclient.go
More file actions
370 lines (329 loc) Β· 7.73 KB
/
client.go
File metadata and controls
370 lines (329 loc) Β· 7.73 KB
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
package sshw
import (
"bufio"
"fmt"
"io"
"net"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/atrox/homedir"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/terminal"
)
var (
DefaultCiphers = []string{
"aes128-ctr",
"aes192-ctr",
"aes256-ctr",
"aes128-gcm@openssh.com",
"chacha20-poly1305@openssh.com",
"arcfour256",
"arcfour128",
"arcfour",
"aes128-cbc",
"3des-cbc",
"blowfish-cbc",
"cast128-cbc",
"aes192-cbc",
"aes256-cbc",
}
)
type Client interface {
Login()
}
type cleanupFunc func()
type defaultClient struct {
clientConfig *ssh.ClientConfig
node *Node
cleanups []cleanupFunc
}
func getAgentConn(agentPath string) (net.Conn, error) {
if len(agentPath) == 0 {
return nil, nil
}
expandedPath, err := homedir.Expand(agentPath)
if err != nil {
return nil, err
}
// IdentityAgent
// Specifies the UNIX-domain socket used to communicate with the
// authentication agent.
return net.DialTimeout("unix", expandedPath, time.Second*10)
}
func setupAgentAuth(node *Node) (ssh.AuthMethod, cleanupFunc, error) {
if node.AgentPath == "" {
return nil, nil, nil
}
conn, err := getAgentConn(node.AgentPath)
if err != nil {
return nil, nil, err
}
client := agent.NewClient(conn)
return ssh.PublicKeysCallback(client.Signers), func() { conn.Close() }, nil
}
func setupKeyFileAuth(node *Node) (ssh.AuthMethod, cleanupFunc, error) {
keyPath := node.KeyPath
if keyPath == "" {
return nil, nil, nil
}
keyPath, err := homedir.Expand(keyPath)
if err != nil {
return nil, nil, err
}
bytes, err := os.ReadFile(keyPath)
if err != nil {
return nil, nil, err
}
var signer ssh.Signer
if node.Passphrase != "" {
signer, err = ssh.ParsePrivateKeyWithPassphrase(bytes, []byte(node.Passphrase))
} else {
signer, err = ssh.ParsePrivateKey(bytes)
}
if err != nil {
return nil, nil, err
}
return ssh.PublicKeys(signer), nil, nil
}
func setupDefaultKeyAuth(node *Node) (ssh.AuthMethod, cleanupFunc, error) {
u, err := user.Current()
if err != nil {
return nil, nil, err
}
keyPath := filepath.Join(u.HomeDir, ".ssh/id_rsa")
bytes, err := os.ReadFile(keyPath)
if err == nil && len(bytes) > 0 {
var signer ssh.Signer
if node.Passphrase != "" {
signer, err = ssh.ParsePrivateKeyWithPassphrase(bytes, []byte(node.Passphrase))
} else {
signer, err = ssh.ParsePrivateKey(bytes)
}
if err == nil {
return ssh.PublicKeys(signer), nil, nil
}
}
return nil, nil, nil
}
func setupPasswordAuth(node *Node) (ssh.AuthMethod, cleanupFunc, error) {
if node.Password == "" {
return nil, nil, nil
}
return ssh.Password(node.Password), nil, nil
}
func setupKeyboardAuth(node *Node) (ssh.AuthMethod, cleanupFunc, error) {
return ssh.KeyboardInteractive(func(user, instruction string, questions []string, echos []bool) ([]string, error) {
answers := make([]string, 0, len(questions))
for i, q := range questions {
fmt.Print(q)
if echos[i] {
scan := bufio.NewScanner(os.Stdin)
if scan.Scan() {
answers = append(answers, scan.Text())
}
err := scan.Err()
if err != nil {
return nil, err
}
} else {
b, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
fmt.Println()
answers = append(answers, string(b))
}
}
return answers, nil
}), nil, nil
}
func genSSHConfig(node *Node) *defaultClient {
// support multiple auth methods
// order: agent > key file > default key file > password > keyboard interactive
fetchers := []func(node *Node) (ssh.AuthMethod, cleanupFunc, error){
setupAgentAuth,
setupKeyFileAuth,
setupDefaultKeyAuth,
setupPasswordAuth,
setupKeyboardAuth,
}
var authMethods []ssh.AuthMethod
var cleanups []cleanupFunc
for _, fetcher := range fetchers {
authMethod, cleanup, err := fetcher(node)
if err == nil && authMethod != nil {
authMethods = append(authMethods, authMethod)
}
if cleanup != nil {
cleanups = append(cleanups, cleanup)
}
if err != nil {
l.Error(err)
}
}
config := &ssh.ClientConfig{
User: node.user(),
Auth: authMethods,
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: time.Second * 10,
}
config.SetDefaults()
config.Ciphers = append(config.Ciphers, DefaultCiphers...)
return &defaultClient{
clientConfig: config,
node: node,
cleanups: cleanups,
}
}
func NewClient(node *Node) Client {
return genSSHConfig(node)
}
func (c *defaultClient) close() {
for _, cleanup := range c.cleanups {
cleanup()
}
}
func (c *defaultClient) Login() {
defer c.close()
host := c.node.Host
port := strconv.Itoa(c.node.port())
jNodes := c.node.Jump
var client *ssh.Client
if len(jNodes) > 0 {
jNode := jNodes[0]
jc := genSSHConfig(jNode)
proxyClient, err := ssh.Dial("tcp", net.JoinHostPort(jNode.Host, strconv.Itoa(jNode.port())), jc.clientConfig)
if err != nil {
l.Error(err)
return
}
conn, err := proxyClient.Dial("tcp", net.JoinHostPort(host, port))
if err != nil {
l.Error(err)
return
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, net.JoinHostPort(host, port), c.clientConfig)
if err != nil {
l.Error(err)
return
}
client = ssh.NewClient(ncc, chans, reqs)
} else {
client1, err := ssh.Dial("tcp", net.JoinHostPort(host, port), c.clientConfig)
client = client1
if err != nil {
msg := err.Error()
// use terminal password retry
if strings.Contains(msg, "no supported methods remain") && !strings.Contains(msg, "password") {
fmt.Printf("%s@%s's password:", c.clientConfig.User, host)
var b []byte
b, err = terminal.ReadPassword(int(syscall.Stdin))
if err == nil {
p := string(b)
if p != "" {
c.clientConfig.Auth = append(c.clientConfig.Auth, ssh.Password(p))
}
fmt.Println()
client, err = ssh.Dial("tcp", net.JoinHostPort(host, port), c.clientConfig)
}
}
}
if err != nil {
l.Error(err)
return
}
}
defer client.Close()
l.Infof("connect server ssh -p %d %s@%s version: %s\n", c.node.port(), c.node.user(), host, string(client.ServerVersion()))
session, err := client.NewSession()
if err != nil {
l.Error(err)
return
}
defer session.Close()
fd := int(os.Stdin.Fd())
state, err := terminal.MakeRaw(fd)
if err != nil {
l.Error(err)
return
}
defer terminal.Restore(fd, state)
//changed fd to int(os.Stdout.Fd()) because terminal.GetSize(fd) doesn't work in Windows
//reference: https://github.com/golang/go/issues/20388
w, h, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
l.Error(err)
return
}
modes := ssh.TerminalModes{
ssh.ECHO: 1,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
err = session.RequestPty("xterm", h, w, modes)
if err != nil {
l.Error(err)
return
}
session.Stdout = os.Stdout
session.Stderr = os.Stderr
stdinPipe, err := session.StdinPipe()
if err != nil {
l.Error(err)
return
}
err = session.Shell()
if err != nil {
l.Error(err)
return
}
// then callback
for i := range c.node.CallbackShells {
shell := c.node.CallbackShells[i]
time.Sleep(shell.Delay * time.Millisecond)
stdinPipe.Write([]byte(shell.Cmd + "\r"))
}
// change stdin to user
go func() {
_, err = io.Copy(stdinPipe, os.Stdin)
l.Error(err)
session.Close()
}()
// interval get terminal size
// fix resize issue
go func() {
var (
ow = w
oh = h
)
for {
cw, ch, err := terminal.GetSize(fd)
if err != nil {
break
}
if cw != ow || ch != oh {
err = session.WindowChange(ch, cw)
if err != nil {
break
}
ow = cw
oh = ch
}
time.Sleep(time.Second)
}
}()
// send keepalive
go func() {
for {
time.Sleep(time.Second * 10)
client.SendRequest("keepalive@openssh.com", false, nil)
}
}()
session.Wait()
}