-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
56 lines (49 loc) · 1.14 KB
/
helper.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
package ssh
import (
"fmt"
"io"
"net"
"time"
"golang.org/x/crypto/ssh"
)
type Info struct {
Host string
Username string
Port int
Password string
Key string
Timeout int
}
func (info *Info) prepare() (*ssh.ClientConfig, error) {
var auth []ssh.AuthMethod
if info.Key != "" {
signer, err := ssh.ParsePrivateKey([]byte(info.Key))
if err != nil {
return nil, err
}
auth = append(auth, ssh.PublicKeys(signer))
}
if info.Password != "" {
auth = append(auth, ssh.Password(info.Password))
}
config := ssh.ClientConfig{
User: info.Username,
Auth: auth,
HostKeyCallback: ssh.HostKeyCallback(func(hostname string, remote net.Addr, key ssh.PublicKey) error { return nil }),
Timeout: time.Duration(time.Second.Nanoseconds() * int64(info.Timeout)),
}
return &config, nil
}
func (info *Info) serverAddr() string {
return fmt.Sprintf("%s:%d", info.Host, info.Port)
}
type closeWriter interface {
CloseWrite() error
}
func exchange(dst io.Writer, src io.Reader, errCh chan error) {
_, err := io.Copy(dst, src)
if tcpConn, ok := dst.(closeWriter); ok {
tcpConn.CloseWrite()
}
errCh <- err
}