Repository navigation
Expand file tree
/
Copy pathconnect.go
More file actions
701 lines (581 loc) · 16.9 KB
/
Copy pathconnect.go
File metadata and controls
701 lines (581 loc) · 16.9 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// Copyright (c) 2026 Blacknon. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package sshlib
import (
"context"
"errors"
"io"
"log"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/net/proxy"
terminal "golang.org/x/term"
)
// Connect structure to store contents about ssh connection.
type Connect struct {
// Client *ssh.Client
Client *ssh.Client
reconnectMu sync.Mutex
reconnectAuths []ssh.AuthMethod
controlClient *controlClient
controlMaster *controlMaster
controlHost string
controlPort string
controlUser string
controlSpawned bool
proxyConnects []*Connect
// Session
Session *ssh.Session
// Session Stdin, Stdout, Stderr...
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
// ProxyDialer
ProxyDialer proxy.ContextDialer
// ProxyRoute takes precedence over ProxyDialer when set.
ProxyRoute []ProxyRoute
// Connect timeout second.
ConnectTimeout int
// AutoReconnect reconnects the underlying SSH transport before starting a new operation
// when the direct connection has been lost.
AutoReconnect bool
// AutoReconnectInterval is the delay in seconds between reconnect attempts.
AutoReconnectInterval int
// AutoReconnectMax is the maximum number of reconnect attempts.
AutoReconnectMax int
// SendKeepAliveMax and SendKeepAliveInterval
SendKeepAliveMax int
SendKeepAliveInterval int
// Session use tty flag.
// Set it before CraeteClient.
TTY bool
// Forward ssh agent flag.
// Set it before CraeteClient.
ForwardAgent bool
// Set the TTY to be used as the input and output for the Session/Cmd.
PtyRelayTty *os.File
// StdoutMutex is a mutex for use Stdout.
StdoutMutex *sync.Mutex
// CheckKnownHosts if true, check knownhosts.
// Ignored if HostKeyCallback is set.
// Set it before CraeteClient.
CheckKnownHosts bool
// HostKeyCallback is ssh.HostKeyCallback.
// This item takes precedence over `CheckKnownHosts`.
// Set it before CraeteClient.
HostKeyCallback ssh.HostKeyCallback
// OverwriteKnownHosts if true, if the knownhost is different, check whether to overwrite.
OverwriteKnownHosts bool
// KnownHostsFiles is list of knownhosts files path.
KnownHostsFiles []string
// TextAskWriteKnownHosts defines a confirmation message when writing a knownhost.
// We are using Go's template engine and have the following variables available.
// - Address ... ssh server hostname
// - RemoteAddr ... ssh server address
// - Fingerprint ... ssh PublicKey fingerprint
TextAskWriteKnownHosts string
// TextAskOverwriteKnownHosts defines a confirmation message when over-writing a knownhost.
// We are using Go's template engine and have the following variables available.
// - Address ... ssh server hostname
// - RemoteAddr ... ssh server address
// - OldKeyText ... old ssh PublicKey text.
// ex: /home/user/.ssh/known_hosts:17: ecdsa-sha2-nistp256 AAAAE2VjZHN...bJklasnFtkFSDyOjTFSv2g=
// - NewFingerprint ... new ssh PublicKey fingerprint
TextAskOverwriteKnownHosts string
// ssh-agent interface.
// agent.Agent or agent.ExtendedAgent
// Set it before CraeteClient.
Agent AgentInterface
// Forward x11 flag.
// Set it before CraeteClient.
ForwardX11 bool
// Forward X11 trusted flag.
// This flag is ssh -Y option like flag.
// Set it before CraeteClient.
ForwardX11Trusted bool
x11HandlerOnce sync.Once
// Dynamic forward related logger
DynamicForwardLogger *log.Logger
// ControlMaster enables OpenSSH-like connection sharing via a local control socket.
// Supported values are "", "no", "yes", and "auto".
ControlMaster string
// ControlPath is the Unix domain socket path used for connection sharing.
ControlPath string
// ControlPersist keeps the local control socket alive while the owning process remains alive.
// When greater than zero, sshlib will try to start a detached helper process.
ControlPersist time.Duration
// ControlPersistAuth contains auth methods that can be replayed by the detached helper.
// Set ControlPersistAuth.AuthMethods with auth methods created by
// sshlib.CreateAuthMethodPassword or sshlib.CreateAuthMethodPublicKey.
// Required when ControlPersist > 0 and no master is already running.
ControlPersistAuth *ControlPersistAuth
// shell terminal log flag
logging bool
// terminal log add timestamp flag
logTimestamp bool
// terminal log path
logFile string
// remove ansi code on terminal log.
logRemoveAnsiCode bool
}
// CreateClient set c.Client.
func (c *Connect) CreateClient(host, port, user string, authMethods []ssh.AuthMethod) (err error) {
debugf("sshlib: CreateClient host=%s port=%s user=%s control_master=%s persist=%s proxy_route=%d proxy_dialer=%t\n",
host, port, user, c.ControlMaster, c.ControlPersist, len(c.ProxyRoute), c.ProxyDialer != nil)
c.controlClient = nil
c.controlSpawned = false
c.controlHost = host
c.controlPort = port
c.controlUser = user
authMethods, err = c.resolveAuthMethods(authMethods, nil)
if err != nil {
return err
}
c.rememberReconnectConfig(host, port, user, authMethods)
mode := c.controlMode()
if mode == "" || mode == "no" {
debugln("sshlib: CreateClient using direct mode")
return c.createDirectClient(host, port, user, authMethods)
}
if c.ControlPath == "" {
return errors.New("sshlib: ControlPath is required when ControlMaster is enabled")
}
if mode == "auto" || mode == "yes" {
debugf("sshlib: attempting existing control socket path=%s\n", c.ControlPath)
client, cerr := dialControlClient(c.ControlPath)
if cerr == nil {
debugln("sshlib: connected to existing control master")
c.controlClient = client
return nil
}
debugf("sshlib: no existing control master path=%s err=%v\n", c.ControlPath, cerr)
if mode == "yes" {
return cerr
}
}
if c.ControlPersist > 0 {
debugln("sshlib: spawning detached control master")
if err := c.startDetachedControlMaster(host, port, user); err != nil {
return err
}
c.controlSpawned = true
client, err := waitForControlClient(c.ControlPath, 5*time.Second)
if err != nil {
debugf("sshlib: waiting for control client failed path=%s err=%v\n", c.ControlPath, err)
return err
}
debugln("sshlib: detached control master ready")
c.controlClient = client
return nil
}
if err := c.createDirectClient(host, port, user, authMethods); err != nil {
return err
}
master, err := newControlMaster(c, c.ControlPath)
if err != nil {
c.Client.Close()
c.Client = nil
return err
}
c.controlMaster = master
return nil
}
func (c *Connect) resolveAuthMethods(authMethods []ssh.AuthMethod, prompt PromptFunc) ([]ssh.AuthMethod, error) {
if len(authMethods) > 0 {
return authMethods, nil
}
if c.ControlPersistAuth == nil {
return authMethods, nil
}
resolved, err := c.ControlPersistAuth.resolved()
if err != nil {
return nil, err
}
return createControlPersistAuthMethodsWithPrompt(resolved, prompt)
}
func (c *Connect) IsControlClient() bool {
return c.isControlClient()
}
func (c *Connect) SpawnedControlMaster() bool {
return c.controlSpawned
}
func (c *Connect) createDirectClient(host, port, user string, authMethods []ssh.AuthMethod) (err error) {
uri := net.JoinHostPort(host, port)
debugf("sshlib: createDirectClient begin uri=%s user=%s timeout=%ds\n", uri, user, c.ConnectTimeout)
timeout := 20
if c.ConnectTimeout == 0 {
c.ConnectTimeout = timeout
}
// Create new ssh.ClientConfig{}
config := &ssh.ClientConfig{
User: user,
Auth: authMethods,
Timeout: time.Duration(c.ConnectTimeout) * time.Second,
}
if c.HostKeyCallback != nil {
config.HostKeyCallback = c.HostKeyCallback
} else {
if c.CheckKnownHosts {
if len(c.KnownHostsFiles) == 0 {
// append default files
c.KnownHostsFiles = append(c.KnownHostsFiles, "~/.ssh/known_hosts")
}
config.HostKeyCallback = c.VerifyAndAppendNew
} else {
config.HostKeyCallback = ssh.InsecureIgnoreHostKey()
}
}
// check Dialer
dialer := c.ProxyDialer
proxyConnects := c.proxyConnects
if len(c.ProxyRoute) > 0 {
if err := c.closeProxyConnects(); err != nil {
return err
}
dialer, proxyConnects, err = buildProxyRouteDialer(c.ProxyRoute, nil)
if err != nil {
return err
}
} else if dialer == nil {
dialer = proxy.Direct
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.ConnectTimeout)*time.Second)
defer cancel()
// Dial to host:port
debugf("sshlib: dialing network=tcp addr=%s\n", uri)
netConn, cerr := dialer.DialContext(ctx, "tcp", uri)
if cerr != nil {
debugf("sshlib: dial failed addr=%s err=%v\n", uri, cerr)
_ = closeProxyConnectList(proxyConnects)
return cerr
}
debugf("sshlib: dial succeeded addr=%s\n", uri)
// Set deadline
_ = netConn.SetDeadline(time.Now().Add(time.Duration(c.ConnectTimeout) * time.Second))
// Create new ssh connect
debugf("sshlib: starting ssh handshake addr=%s\n", uri)
sshCon, channel, req, cerr := ssh.NewClientConn(netConn, uri, config)
if cerr != nil {
debugf("sshlib: ssh handshake failed addr=%s err=%v\n", uri, cerr)
_ = netConn.Close()
_ = closeProxyConnectList(proxyConnects)
return cerr
}
debugf("sshlib: ssh handshake succeeded addr=%s\n", uri)
// Reet deadline
_ = netConn.SetDeadline(time.Time{})
// Create *ssh.Client
c.Client = ssh.NewClient(sshCon, channel, req)
c.proxyConnects = proxyConnects
debugf("sshlib: createDirectClient success uri=%s\n", uri)
return
}
func (c *Connect) rememberReconnectConfig(host, port, user string, authMethods []ssh.AuthMethod) {
c.controlHost = host
c.controlPort = port
c.controlUser = user
c.reconnectAuths = append(c.reconnectAuths[:0], authMethods...)
}
func (c *Connect) controlMode() string {
switch c.ControlMaster {
case "", "no":
return c.ControlMaster
case "yes", "auto":
return c.ControlMaster
default:
return ""
}
}
func (c *Connect) isControlClient() bool {
return c.controlClient != nil
}
// Close releases control resources and the underlying SSH client.
func (c *Connect) Close() error {
c.reconnectMu.Lock()
defer c.reconnectMu.Unlock()
var err error
if c.controlClient != nil {
err = c.controlClient.Close()
c.controlClient = nil
}
if c.controlMaster != nil {
closeErr := c.controlMaster.Close()
c.controlMaster = nil
if err == nil {
err = closeErr
}
}
if c.Client != nil {
closeErr := c.Client.Close()
c.Client = nil
if err == nil {
err = closeErr
}
}
closeErr := c.closeProxyConnects()
if err == nil {
err = closeErr
}
return err
}
func (c *Connect) closeProxyConnects() error {
err := closeProxyConnectList(c.proxyConnects)
c.proxyConnects = nil
return err
}
// CreateSession retrun ssh.Session
func (c *Connect) CreateSession() (session *ssh.Session, err error) {
if c.isControlClient() {
return nil, errors.New("sshlib: CreateSession is not available over ControlMaster; use Command, Shell(nil), or CmdShell(nil, command)")
}
if err := c.ensureActiveConnection(); err != nil {
return nil, err
}
// Create session
session, err = c.Client.NewSession()
return
}
// Dial opens a connection using the active SSH transport.
// When ControlMaster is enabled, the dial is tunneled via the control socket.
func (c *Connect) Dial(network, addr string) (net.Conn, error) {
if c.isControlClient() {
if err := c.ensureActiveConnection(); err != nil {
return nil, err
}
return c.controlClient.Dial(network, addr)
}
if err := c.ensureActiveConnection(); err != nil {
return nil, err
}
return c.Client.Dial(network, addr)
}
// Listen starts a remote listener using the active SSH transport.
// When ControlMaster is enabled, the listener is managed by the control master.
func (c *Connect) Listen(network, addr string) (net.Listener, error) {
if c.isControlClient() {
if err := c.ensureActiveConnection(); err != nil {
return nil, err
}
return c.controlClient.Listen(network, addr)
}
if err := c.ensureActiveConnection(); err != nil {
return nil, err
}
return c.Client.Listen(network, addr)
}
func (c *Connect) autoReconnectConfig() (time.Duration, int) {
interval := 1
if c.AutoReconnectInterval > 0 {
interval = c.AutoReconnectInterval
}
max := 1
if c.AutoReconnectMax > 0 {
max = c.AutoReconnectMax
}
return time.Duration(interval) * time.Second, max
}
func (c *Connect) canAutoReconnect() bool {
return c.AutoReconnect && !c.isControlClient() && c.controlHost != "" && c.controlPort != "" && c.controlUser != "" && len(c.reconnectAuths) > 0
}
func (c *Connect) ensureActiveConnection() error {
if c.isControlClient() {
return c.ensureControlClient()
}
if c.Client == nil {
if !c.canAutoReconnect() {
return errors.New("ssh client is nil")
}
return c.reconnect()
}
if err := c.CheckClientAlive(); err != nil {
if !c.canAutoReconnect() {
return err
}
return c.reconnect()
}
return nil
}
func (c *Connect) reconnect() error {
c.reconnectMu.Lock()
defer c.reconnectMu.Unlock()
if c.Client != nil {
if err := c.CheckClientAlive(); err == nil {
return nil
}
}
if !c.canAutoReconnect() {
if c.Client == nil {
return errors.New("ssh client is nil")
}
return errors.New("sshlib: auto reconnect is not configured")
}
interval, max := c.autoReconnectConfig()
var lastErr error
for attempt := 0; attempt < max; attempt++ {
if attempt > 0 {
time.Sleep(interval)
}
if c.Client != nil {
_ = c.Client.Close()
c.Client = nil
}
_ = c.closeProxyConnects()
lastErr = c.createDirectClient(c.controlHost, c.controlPort, c.controlUser, c.reconnectAuths)
if lastErr == nil {
return nil
}
}
return lastErr
}
func (c *Connect) keepAliveConfig() (time.Duration, int) {
interval := 30
if c.SendKeepAliveInterval > 0 {
interval = c.SendKeepAliveInterval
}
max := 3
if c.SendKeepAliveMax > 0 {
max = c.SendKeepAliveMax
}
return time.Duration(interval) * time.Second, max
}
func (c *Connect) startSessionKeepAlive(session *ssh.Session) func() {
done := make(chan struct{})
go func() {
interval, max := c.keepAliveConfig()
t := time.NewTicker(interval)
defer t.Stop()
failures := 0
for {
select {
case <-done:
return
case <-t.C:
if _, err := session.SendRequest("keepalive@openssh.com", true, nil); err != nil {
log.Println("Failed to send keepalive packet:", err)
failures++
if failures > max {
_ = session.Close()
return
}
continue
}
failures = 0
}
}
}()
return func() {
close(done)
}
}
// SendKeepAlive send packet to session.
// TODO(blacknon): Interval及びMaxを設定できるようにする(v0.1.1)
func (c *Connect) SendKeepAlive(session *ssh.Session) {
interval, max := c.keepAliveConfig()
t := time.NewTicker(interval)
defer t.Stop()
failures := 0
for range t.C {
if _, err := session.SendRequest("keepalive@openssh.com", true, nil); err != nil {
log.Println("Failed to send keepalive packet:", err)
failures++
if failures > max {
_ = session.Close()
return
}
continue
}
failures = 0
}
}
// CheckClientAlive check alive ssh.Client.
func (c *Connect) CheckClientAlive() error {
if c.isControlClient() {
return c.controlClient.Ping()
}
if c.Client == nil {
return errors.New("ssh client is nil")
}
_, _, err := c.Client.SendRequest("keepalive@openssh.com", true, nil)
if err == nil {
return nil
}
if strings.Contains(strings.ToLower(err.Error()), "request failed") {
return nil
}
return err
}
// RequestTty requests the association of a pty with the session on the remote
// host. Terminal size is obtained from the currently connected terminal
func RequestTty(session *ssh.Session) (err error) {
// Get terminal window size
fd := int(os.Stdout.Fd())
width, height, err := terminal.GetSize(fd)
if err != nil {
return
}
// Get env `TERM`
term := os.Getenv("TERM")
if err = RequestTtyWithSize(session, term, width, height, nil); err != nil {
return err
}
// Terminal resize goroutine.
winch := syscall.Signal(0x1c)
signalchan := make(chan os.Signal, 1)
signal.Notify(signalchan, winch)
go func() {
for {
s := <-signalchan
switch s {
case winch:
fd := int(os.Stdout.Fd())
width, height, _ = terminal.GetSize(fd)
session.WindowChange(height, width)
}
}
}()
return
}
func defaultTerminalModes() ssh.TerminalModes {
return ssh.TerminalModes{
ssh.ECHO: 1,
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}
}
func normalizeTerminalTerm(term string) string {
if term == "" {
term = os.Getenv("TERM")
}
if term == "" {
return "xterm-256color"
}
return term
}
func normalizeTerminalModes(modes ssh.TerminalModes) ssh.TerminalModes {
if len(modes) == 0 {
return defaultTerminalModes()
}
return modes
}
// RequestTtyWithSize requests the association of a pty with the session on the
// remote host using the caller-provided terminal size.
func RequestTtyWithSize(session *ssh.Session, term string, cols, rows int, modes ssh.TerminalModes) error {
if cols <= 0 {
cols = 80
}
if rows <= 0 {
rows = 24
}
if err := session.RequestPty(normalizeTerminalTerm(term), rows, cols, normalizeTerminalModes(modes)); err != nil {
session.Close()
return err
}
return nil
}