-
Notifications
You must be signed in to change notification settings - Fork 37
/
tool.go
197 lines (175 loc) · 3.92 KB
/
tool.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
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
package gosshtool
import (
crand "crypto/rand"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
mrand "math/rand"
"os"
"regexp"
"strings"
"sync"
"time"
)
var (
sshClients map[string]*SSHClient
sshClientsMutex sync.RWMutex
)
var seeded bool = false
var syncbufpool *sync.Pool
var uuidRegex *regexp.Regexp = regexp.MustCompile(`^\{?([a-fA-F0-9]{8})-?([a-fA-F0-9]{4})-?([a-fA-F0-9]{4})-?([a-fA-F0-9]{4})-?([a-fA-F0-9]{12})\}?$`)
type UUID [16]byte
// Hex returns a hex string representation of the UUID in xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format.
func (this UUID) Hex() string {
x := [16]byte(this)
return fmt.Sprintf("%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
x[0], x[1], x[2], x[3], x[4],
x[5], x[6],
x[7], x[8],
x[9], x[10], x[11], x[12], x[13], x[14], x[15])
}
// Rand generates a new version 4 UUID.
func Rand() UUID {
var x [16]byte
randBytes(x[:])
x[6] = (x[6] & 0x0F) | 0x40
x[8] = (x[8] & 0x3F) | 0x80
return x
}
func FromStr(s string) (id UUID, err error) {
if s == "" {
err = errors.New("Empty string")
return
}
parts := uuidRegex.FindStringSubmatch(s)
if parts == nil {
err = errors.New("Invalid string format")
return
}
var array [16]byte
slice, _ := hex.DecodeString(strings.Join(parts[1:], ""))
copy(array[:], slice)
id = array
return
}
func MustFromStr(s string) UUID {
id, err := FromStr(s)
if err != nil {
panic(err)
}
return id
}
func randBytes(x []byte) {
length := len(x)
n, err := crand.Read(x)
if n != length || err != nil {
if !seeded {
mrand.Seed(time.Now().UnixNano())
}
for length > 0 {
length--
x[length] = byte(mrand.Int31n(256))
}
}
}
func init() {
sshClients = make(map[string]*SSHClient)
syncbufpool = &sync.Pool{}
syncbufpool.New = func() interface{} {
return make([]byte, 32*1024)
}
}
func CopyIOAndUpdateSessionDeadline(dst io.Writer, src io.Reader, session *SshSession) (written int64, err error) {
if wt, ok := src.(io.WriterTo); ok {
return wt.WriteTo(dst)
}
if rt, ok := dst.(io.ReaderFrom); ok {
return rt.ReadFrom(src)
}
buf := syncbufpool.Get().([]byte)
defer syncbufpool.Put(buf)
for {
nr, er := src.Read(buf)
if nr > 0 {
if session.idleTimeout > 0 {
deadlinenew := time.Now().Add(time.Second * time.Duration(session.idleTimeout))
session.SetDeadline(&deadlinenew)
}
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er == io.EOF {
break
}
if er != nil {
err = er
break
}
}
return written, err
}
func NewSSHClient(config *SSHClientConfig) (client *SSHClient) {
sshClientsMutex.RLock()
client = sshClients[config.Host]
if client != nil {
return
}
sshClientsMutex.RUnlock()
client = new(SSHClient)
client.Host = config.Host
client.User = config.User
client.Password = config.Password
client.Privatekey = config.Privatekey
client.DialTimeoutSecond = config.DialTimeoutSecond
sshClientsMutex.Lock()
sshClients[config.Host] = client
sshClientsMutex.Unlock()
return client
}
func getClient(hostname string) (client *SSHClient, err error) {
if hostname == "" {
return nil, errors.New("host name is empty")
}
sshClientsMutex.RLock()
client = sshClients[hostname]
if client != nil {
return client, nil
}
sshClientsMutex.RUnlock()
return nil, errors.New("client not create")
}
func ExecuteCmd(cmd, hostname string) (output, errput string, currentSession *SshSession, err error) {
client, err := getClient(hostname)
if err != nil {
return
}
return client.Cmd(cmd, nil, nil, 0)
}
func UploadFile(hostname, sourceFile, targetFile string) (stdout, stderr string, err error) {
client, err := getClient(hostname)
if err != nil {
return
}
f, err := os.Open(sourceFile)
if err != nil {
return
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
return
}
return client.TransferData(targetFile, data)
}