/
conwork.go
69 lines (62 loc) · 1.43 KB
/
conwork.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
package pssh
import (
"context"
"fmt"
"log"
"golang.org/x/crypto/ssh"
)
type conWork struct {
*Pssh
id int
host string
command chan input
startSession func(ctx context.Context, conn sshClientIface, cmd input)
}
// TemporaryError is network error
type TemporaryError interface {
Temporary() bool
}
func (c *conWork) conWorker(ctx context.Context, config ssh.ClientConfig, instanceCh chan<- conInstance) {
if c.Pssh == nil {
return
}
if c.Concurrency > 0 {
defer func() { <-c.concurrentGoroutines }()
}
authMethods := c.mergeAuthMethods(c.getIdentFileAuthMethods(c.identFileData))
config.Auth = authMethods
res := conInstance{conWork: c, err: nil}
if c.Debug {
log.Printf("start ssh.Dial : %s", c.host)
}
conn, err := c.sshDialer.Dial("tcp", c.host, &config)
if err != nil {
res.err = fmt.Errorf("cannot connect [%s] err:%s", c.host, err)
select {
case <-ctx.Done():
case instanceCh <- res:
}
return
}
// nolint: errcheck
defer conn.Close()
c.commandLoop(ctx, conn, false)
}
func (c *conWork) commandLoop(ctx context.Context, conn sshClientIface, loop bool) {
for {
select {
case <-ctx.Done():
return
case cmd := <-c.command:
c.startSession(ctx, conn, cmd)
}
if !loop {
return
}
}
}
func (c *conWork) startSessionWorker(ctx context.Context, conn sshClientIface, cmd input) {
s := &sessionWork{id: cmd.id, input: &cmd, con: c}
s.runner = s.run
s.worker(ctx, conn)
}