-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtransport.go
360 lines (284 loc) · 8.73 KB
/
transport.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package dev
import (
"context"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"time"
"golang.org/x/crypto/ssh"
)
type transp interface {
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
SetDeadline(t time.Time) error
SetWriteDeadline(t time.Time) error
Close() error
}
type transpTCP struct {
net.Conn
}
type transpTelnet struct {
net.Conn
logger hasPrintf
}
type telnetOptions struct {
supressGoAhead bool
linemode bool
}
func (s *transpTelnet) Read(b []byte) (int, error) {
n1, err1 := s.Conn.Read(b)
if err1 != nil {
return n1, err1
}
n2, err2 := telnetNegotiation(b, n1, s, s.logger, false)
return n2, err2
}
type transpPipe struct {
proc *exec.Cmd
stdout io.ReadCloser
stderr io.ReadCloser
reader io.Reader
writer io.WriteCloser
logger hasPrintf
devLabel string
debug bool
ctx context.Context
cancel context.CancelFunc
}
func (s *transpPipe) result(n int, err error) (int, error) {
var waitErr error
if err != nil {
waitErr = s.proc.Wait()
}
ctxErr := s.ctx.Err()
if ctxErr != nil || waitErr != nil {
return n, fmt.Errorf("transPipe result error: error=[%v] context=[%v] wait=[%v]", err, ctxErr, waitErr)
}
return n, err
}
func (s *transpPipe) Read(b []byte) (int, error) {
n, err := s.reader.Read(b)
return s.result(n, err)
}
func (s *transpPipe) Write(b []byte) (int, error) {
n, err := s.writer.Write(b)
return s.result(n, err)
}
func (s *transpPipe) SetDeadline(t time.Time) error {
s.logger.Printf("transpPipe.SetDeadline: FIXME WRITEME")
return nil
}
func (s *transpPipe) SetWriteDeadline(t time.Time) error {
s.logger.Printf("transpPipe.SetWriteDeadline: FIXME WRITEME")
return nil
}
func (s *transpPipe) Close() error {
if s.debug {
s.logger.Printf("transpPipe.Close: %s contextErr=[%v]", s.devLabel, s.ctx.Err())
}
s.cancel()
err1 := s.stdout.Close()
err2 := s.stderr.Close()
err3 := s.writer.Close()
if err1 != nil || err2 != nil || err3 != nil {
return fmt.Errorf("transpPipe: close error: out=[%v] err=[%v] writer=[%v]", err1, err2, err3)
}
return nil
}
type transpSSH struct {
devLabel string
conn net.Conn
client *ssh.Client
session *ssh.Session
writer io.Writer
reader io.Reader
}
func (s *transpSSH) Read(b []byte) (int, error) {
return s.reader.Read(b)
}
func (s *transpSSH) Write(b []byte) (int, error) {
n, err := s.writer.Write(b)
if err != nil {
return -1, fmt.Errorf("ssh write(%s): %v", b, err)
}
return n, nil
}
func (s *transpSSH) SetDeadline(t time.Time) error {
return s.conn.SetDeadline(t)
}
func (s *transpSSH) SetWriteDeadline(t time.Time) error {
return s.conn.SetWriteDeadline(t)
}
func (s *transpSSH) Close() error {
err1 := s.session.Close()
err2 := s.conn.Close()
if err1 != nil || err2 != nil {
return fmt.Errorf("close error: session=[%v] conn=[%v]", err1, err2)
}
return nil
}
func openTransportPipe(logger hasPrintf, modelName, devID, hostPort, transports, user, pass string, args []string, debug bool, timeout time.Duration) (transp, string, bool, error) {
s, err := openPipe(logger, modelName, devID, hostPort, transports, user, pass, args, debug, timeout)
return s, "pipe", true, err
}
func openPipe(logger hasPrintf, modelName, devID, hostPort, transports, user, pass string, args []string, debug bool, timeout time.Duration) (transp, error) {
devLabel := fmt.Sprintf("%s %s %s", modelName, devID, hostPort)
logger.Printf("openPipe: %s - opening", devLabel)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
c := exec.CommandContext(ctx, args[0], args[1:]...)
pipeOut, outErr := c.StdoutPipe()
if outErr != nil {
cancel()
return nil, fmt.Errorf("openPipe: StdoutPipe: %s - %v", devLabel, outErr)
}
pipeErr, errErr := c.StderrPipe()
if errErr != nil {
cancel()
return nil, fmt.Errorf("openPipe: StderrPipe: %s - %v", devLabel, errErr)
}
writer, wrErr := c.StdinPipe()
if wrErr != nil {
cancel()
return nil, fmt.Errorf("openPipe: StdinPipe: %s - %v", devLabel, wrErr)
}
s := &transpPipe{proc: c, logger: logger, devLabel: devLabel, debug: debug}
s.reader = io.MultiReader(pipeOut, pipeErr)
s.stdout = pipeOut
s.stderr = pipeErr
s.writer = writer
s.ctx = ctx
s.cancel = cancel
logger.Printf("openPipe: %s - starting", devLabel)
os.Setenv("JAZIGO_DEV_ID", devID)
os.Setenv("JAZIGO_DEV_HOSTPORT", hostPort)
os.Setenv("JAZIGO_DEV_USER", user)
os.Setenv("JAZIGO_DEV_PASS", pass)
if startErr := s.proc.Start(); startErr != nil {
s.Close()
return nil, fmt.Errorf("openPipe: error: %v", startErr)
}
logger.Printf("openPipe: %s - started", devLabel)
return s, nil
}
func openTransport(logger hasPrintf, modelName, devID, hostPort, transports, user, pass string,
sshClearCiphers bool, sshAddCiphers []string) (transp, string, bool, error) {
tList := strings.Split(transports, ",")
if len(tList) < 1 {
return nil, transports, false, fmt.Errorf("openTransport: missing transports: [%s]", transports)
}
var lastErr error
timeout := 10 * time.Second
for _, t := range tList {
switch t {
case "ssh":
hp := forceHostPort(hostPort, "22")
s, err := openSSH(logger, modelName, devID, hp, timeout, user, pass,
sshClearCiphers, sshAddCiphers)
if err == nil {
return s, t, true, nil
}
logger.Printf("openTransport: %v", err)
lastErr = err
case "telnet":
hp := forceHostPort(hostPort, "23")
s, err := openTelnet(logger, modelName, devID, hp, timeout)
if err == nil {
return s, t, false, nil
}
logger.Printf("openTransport: %v", err)
lastErr = err
default:
s, err := openTCP(logger, modelName, devID, hostPort, timeout)
if err == nil {
return s, t, false, nil
}
logger.Printf("openTransport: %v", err)
lastErr = err
}
}
return nil, transports, false, fmt.Errorf("openTransport: %s %s %s %s - unable to open transport: last error: %v", modelName, devID, hostPort, transports, lastErr)
}
func forceHostPort(hostPort, defaultPort string) string {
i := strings.Index(hostPort, ":")
if i < 0 {
return fmt.Sprintf("%s:%s", hostPort, defaultPort)
}
return hostPort
}
func hostKeyCheck(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil // FIXME hostKeyCheck accept anything
}
func openSSH(logger hasPrintf, modelName, devID, hostPort string, timeout time.Duration, user, pass string,
sshClearCiphers bool, sshAddCiphers []string) (transp, error) {
conn, dialErr := net.DialTimeout("tcp", hostPort, timeout)
if dialErr != nil {
return nil, fmt.Errorf("openSSH: Dial: %s %s %s - %v", modelName, devID, hostPort, dialErr)
}
conf := &ssh.Config{}
conf.SetDefaults()
if sshClearCiphers {
conf.Ciphers = nil
}
conf.Ciphers = append(conf.Ciphers, sshAddCiphers...)
config := &ssh.ClientConfig{
Config: *conf,
User: user,
Auth: []ssh.AuthMethod{
ssh.Password(pass),
},
Timeout: timeout,
HostKeyCallback: hostKeyCheck,
}
c, chans, reqs, connErr := ssh.NewClientConn(conn, hostPort, config)
if connErr != nil {
return nil, fmt.Errorf("openSSH: NewClientConn: %s %s %s - %v", modelName, devID, hostPort, connErr)
}
cli := ssh.NewClient(c, chans, reqs)
s := &transpSSH{conn: conn, client: cli, devLabel: fmt.Sprintf("%s %s %s", modelName, devID, hostPort) /*, logger: logger*/}
ses, sessionErr := s.client.NewSession()
if sessionErr != nil {
return nil, fmt.Errorf("openSSH: NewSession: %s - %v", s.devLabel, sessionErr)
}
s.session = ses
modes := ssh.TerminalModes{
ssh.ECHO: 0, // disable echoing
}
if ptyErr := ses.RequestPty("xterm", 80, 40, modes); ptyErr != nil {
return nil, fmt.Errorf("openSSH: Pty: %s - %v", s.devLabel, ptyErr)
}
pipeOut, outErr := ses.StdoutPipe()
if outErr != nil {
return nil, fmt.Errorf("openSSH: StdoutPipe: %s - %v", s.devLabel, outErr)
}
pipeErr, errErr := ses.StderrPipe()
if errErr != nil {
return nil, fmt.Errorf("openSSH: StderrPipe: %s - %v", s.devLabel, errErr)
}
s.reader = io.MultiReader(pipeOut, pipeErr)
writer, wrErr := ses.StdinPipe()
if wrErr != nil {
return nil, fmt.Errorf("openSSH: StdinPipe: %s - %v", s.devLabel, wrErr)
}
s.writer = writer
if shellErr := ses.Shell(); shellErr != nil {
return nil, fmt.Errorf("openSSH: Remote shell error: %s - %v", s.devLabel, shellErr)
}
return s, nil
}
func openTelnet(logger hasPrintf, modelName, devID, hostPort string, timeout time.Duration) (transp, error) {
conn, err := net.DialTimeout("tcp", hostPort, timeout)
if err != nil {
return nil, fmt.Errorf("openTelnet: %s %s %s - %v", modelName, devID, hostPort, err)
}
return &transpTelnet{conn, logger}, nil
}
func openTCP(logger hasPrintf, modelName, devID, hostPort string, timeout time.Duration) (transp, error) {
conn, err := net.DialTimeout("tcp", hostPort, timeout)
if err != nil {
return nil, fmt.Errorf("openTCP: %s %s %s - %v", modelName, devID, hostPort, err)
}
return &transpTCP{conn}, nil
}