-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialer_ssh.go
114 lines (103 loc) · 2.46 KB
/
dialer_ssh.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
package main
import (
"context"
"net"
"os"
"sync"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
var _ Dialer = (*SSHDialer)(nil)
type SSHDialer struct {
Username string
Password string
PrivateKey string
Host string
Port string
StrictHostKeyChecking bool
UserKnownHostsFile string
MaxClients int
Timeout time.Duration
Dialer Dialer
mutexes [64]sync.Mutex
clients [64]*ssh.Client
}
func (d *SSHDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
connect := func() (*ssh.Client, error) {
config := &ssh.ClientConfig{
User: d.Username,
Auth: []ssh.AuthMethod{
ssh.Password(d.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: d.Timeout,
}
if d.PrivateKey != "" {
signer, err := ssh.ParsePrivateKey([]byte(d.PrivateKey))
if err != nil {
return nil, err
}
config.Auth = append([]ssh.AuthMethod{ssh.PublicKeys(signer)}, config.Auth...)
}
if d.StrictHostKeyChecking {
file := d.UserKnownHostsFile
if file == "" {
file = os.ExpandEnv("$HOME/.ssh/known_hosts")
}
cb, err := knownhosts.New(file)
if err != nil {
return nil, err
}
config.HostKeyCallback = cb
}
dialer := d.Dialer
if dialer == nil {
dialer = &net.Dialer{Timeout: d.Timeout}
}
ctx, cancel := context.WithTimeout(ctx, config.Timeout)
defer cancel()
conn, err := dialer.DialContext(ctx, "tcp", net.JoinHostPort(d.Host, d.Port))
if err != nil {
return nil, err
}
c, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
if err != nil {
return nil, err
}
return ssh.NewClient(c, chans, reqs), nil
}
maxClient := d.MaxClients
if maxClient == 0 {
maxClient = 1
}
n := 1
if 0 < maxClient && maxClient < len(d.clients) {
n = maxClient
}
n = int(fastrandn(uint32(n)))
if d.clients[n] == nil {
d.mutexes[n].Lock()
c, err := connect()
if err != nil {
d.mutexes[n].Unlock()
return nil, err
}
d.clients[n] = c
d.mutexes[n].Unlock()
}
conn, err := d.clients[n].Dial(network, addr)
if err != nil {
time.Sleep(time.Duration(100+fastrandn(200)) * time.Millisecond)
old := d.clients[n]
d.mutexes[n].Lock()
if d.clients[n] == old {
d.clients[n], err = connect()
}
d.mutexes[n].Unlock()
if c := d.clients[n]; c != nil && c != old {
conn, err = c.Dial(network, addr)
}
}
return conn, err
}