-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
270 lines (226 loc) · 4.5 KB
/
session.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package sshclient
import (
"context"
"errors"
"fmt"
"golang.org/x/crypto/ssh"
"regexp"
"strings"
"time"
)
type Session struct {
conn *ssh.Client
session *ssh.Session
in chan string
out chan string
keepaliveDone chan struct{}
}
func NewSeesion(conn *ssh.Client) (*Session, error) {
sshSession := new(Session)
sshSession.conn = conn
if err := sshSession.shell(); err != nil {
return nil, err
}
if err := sshSession.start(); err != nil {
return nil, err
}
return sshSession, nil
}
func (this *Session) shell() error {
defer func() {
if err := recover(); err != nil {
//LogError("Session shell err:%s", err)
}
}()
session, err := this.conn.NewSession()
if err != nil {
return err
}
this.session = session
w, err := this.session.StdinPipe()
if err != nil {
return err
}
r, err := this.session.StdoutPipe()
if err != nil {
return err
}
in := make(chan string, 1024)
out := make(chan string, 1024)
go func() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Goroutine shell write err:%s", err)
}
}()
for cmd := range in {
_, err := w.Write([]byte(cmd + "\n"))
if err != nil {
//LogDebug("Writer write err:%s", err.Error())
return
}
}
}()
go func() {
defer func() {
if err := recover(); err != nil {
fmt.Printf("Goroutine shell read err:%s", err)
}
}()
var (
buf [65 * 1024]byte
t int
)
for {
n, err := r.Read(buf[t:])
if err != nil {
//LogDebug("Reader read err:%s", err.Error())
return
}
t += n
out <- string(buf[:t])
t = 0
}
}()
this.in = in
this.out = out
return nil
}
func (this *Session) start() error {
if err := this.session.Shell(); err != nil {
return err
}
this.ReadChannelTiming(time.Second * 3)
return nil
}
func (this *Session) Close() error {
defer func() error {
if err := recover(); err != nil {
return errors.New(fmt.Sprintf("Session Close err:%s", err))
}
return nil
}()
if err := this.session.Close(); err != nil {
return err
//LogError("Close session err:%s", err.Error())
}
close(this.in)
close(this.out)
return nil
}
func (this *Session) WriteChannel(cmds ...string) {
for _, cmd := range cmds {
this.in <- cmd
}
}
func (this *Session) ReadChannelExpect(timeout time.Duration, expects ...string) string {
readFn := func(data string) bool {
for _, expect := range expects {
if strings.Contains(data, expect) {
return true
}
}
return false
}
return this.ReadChannel(timeout, readFn)
}
func (this *Session) ReadChannelRegExp(timeout time.Duration, re string) string {
readFn := func(data string) bool {
var re = regexp.MustCompile(re)
ok := re.MatchString(data)
return ok
}
return this.ReadChannel(timeout, readFn)
}
func (this *Session) ReadChannelTiming(timeout time.Duration) string {
output := ""
isDelayed := false
for i := 0; i < 300; i++ {
time.Sleep(time.Millisecond * 100)
newData := this.readChannelData()
if newData != "" {
output += newData
isDelayed = false
continue
}
if !isDelayed {
time.Sleep(timeout)
isDelayed = true
} else {
return output
}
}
return output
}
func (this *Session) ReadChannel(timeout time.Duration, fn func(string) bool) string {
output := ""
isDelayed := false
for i := 0; i < 300; i++ {
time.Sleep(time.Millisecond * 100)
newData := this.readChannelData()
if newData != "" {
output += newData
isDelayed = false
continue
}
if fn != nil {
ok := fn(output)
if ok {
return output
}
}
if !isDelayed {
time.Sleep(timeout)
isDelayed = true
} else {
return output
}
}
return output
}
func (this *Session) ClearChannel() {
this.readChannelData()
}
func (this *Session) RawReadChannel(ctx context.Context, fn ChannelDataReader, ticker *time.Ticker) error {
output := ""
if ticker == nil {
ticker = time.NewTicker(time.Millisecond * 100)
}
defer ticker.Stop()
doneChan := make(chan bool, 1)
defer close(doneChan)
var err error
for {
select {
case <-doneChan:
return err
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
newData := this.readChannelData()
if newData != "" {
output += newData
continue
}
if len(output) > 0 {
err = fn(output, doneChan)
output = ""
}
}
}
}
func (this *Session) readChannelData() string {
output := ""
for {
time.Sleep(time.Millisecond * 100)
select {
case channelData, ok := <-this.out:
if !ok {
return output
}
output += channelData
default:
return output
}
}
}