-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
111 lines (104 loc) · 2.73 KB
/
client.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
package ssh
import (
"fmt"
"golang.org/x/crypto/ssh"
"io/ioutil"
"strings"
"time"
)
const (
//tcp establish timeout
connTimeout = time.Duration(15) * time.Second
)
type SSHTool struct {
host string
port string
username string
password string
privatePath string
}
//NewSSHTool returned *SSHTool
// choose (password) or (privateKey) as remote ssh connect authority
// examples:
// hostname: 10.129.130.122:11229
// username: Alice
// password: yourpassword
// privatePath:
func NewSSHTool(hostname, username, password string, privatePath string) (*SSHTool, error) {
//get host port
var host, port string
hosts := strings.Split(hostname, ":")
if len(hosts) == 1 {
host = hosts[0]
port = "22"
} else if len(hosts) == 2 {
host = hosts[0]
port = hosts[1]
} else {
return nil, fmt.Errorf("ssh host(%s) invalid", hostname)
}
//object
tool := SSHTool{
host: host,
port: port,
username: username,
password: password,
privatePath: privatePath,
}
return &tool, nil
}
//SSHTool Connect returned *ssh.Client which can be used for proxy
func (tool *SSHTool) Connect() (conn *ssh.Client, err error) {
//tool config
var conf *ssh.ClientConfig
if tool.password != "" {
conf, err = makePasswordConfig(tool.username, tool.password)
if err != nil {
return nil, fmt.Errorf("ssh makePasswordConfig err:%s", err.Error())
}
} else if tool.privatePath != "" {
//get private key
content, err := ioutil.ReadFile(tool.privatePath)
if err != nil {
return nil, fmt.Errorf("ssh read privateKey error. path(%s),err:%s", tool.privatePath, err.Error())
}
conf, err = makePrivateKeyConfig(tool.username, string(content))
if err != nil {
return nil, fmt.Errorf("ssh makePrivateKeyConfig err:%s", err.Error())
}
} else {
return nil, fmt.Errorf("ssh password and privateKey both empty")
}
//dial
conn, err = ssh.Dial("tcp", fmt.Sprintf("%s:%s", tool.host, tool.port), conf)
if err != nil {
return nil, fmt.Errorf("ssh dial err:%s", err.Error())
}
return conn, nil
}
func makePasswordConfig(username string, password string) (*ssh.ClientConfig, error) {
conf := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: connTimeout,
}
return conf, nil
}
func makePrivateKeyConfig(username string, privateKey string) (*ssh.ClientConfig, error) {
signer, err := ssh.ParsePrivateKey([]byte(privateKey))
if err != nil {
return nil, fmt.Errorf("ssh ParsePrivateKey err:%s", err.Error())
}
conf := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.PublicKeys(signer),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: connTimeout,
}
return conf, nil
}