-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
175 lines (137 loc) · 3.65 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
package client
import (
"context"
"io"
"time"
"golang.org/x/crypto/ssh"
)
// The Secure Transport layer provides a communication path between
// the client and server. NETCONF can be layered over any
// transport protocol that provides a set of basic requirements.
// Transport interface defines what characteristics make up a NETCONF transport
// layer object.
type Transport interface {
io.ReadWriteCloser
}
type tImpl struct {
reader io.Reader
writeCloser io.WriteCloser
sshSession *ssh.Session
sshClient *ssh.Client
trace *ClientTrace
target string
dialer SSHClientFactory
}
// SSHClientFactory defines a factory that provides an SSH client.
type SSHClientFactory interface {
Dial(ctx context.Context) (*ssh.Client, error)
// Close will close the client (assumed to have been returned by an earlier call to the Dial method), if
// appropriate.
Close(*ssh.Client) error
}
// NewSSHTransport creates a new SSH transport, connecting to the target with the supplied client configuration
// and requesting the specified subsystem.
func NewSSHTransport(ctx context.Context, dialer SSHClientFactory, target string) (rt Transport, err error) {
impl := tImpl{target: target, dialer: dialer}
impl.trace = ContextClientTrace(ctx)
impl.trace.ConnectStart(target)
defer func(begin time.Time) {
impl.trace.ConnectDone(target, err, time.Since(begin))
}(time.Now())
defer func() {
if err != nil {
dialer.Close(impl.sshClient)
if impl.sshSession != nil {
_ = impl.sshSession.Close()
}
}
}()
impl.sshClient, err = dialer.Dial(ctx)
if err != nil {
return
}
if impl.sshSession, err = impl.sshClient.NewSession(); err != nil {
return
}
if err = impl.sshSession.RequestSubsystem("netconf"); err != nil {
return
}
if impl.reader, err = impl.sshSession.StdoutPipe(); err != nil {
return
}
if impl.writeCloser, err = impl.sshSession.StdinPipe(); err != nil {
return
}
impl.injectTraceReader()
impl.injectTraceWriter()
rt = &impl
return rt, err
}
func (t *tImpl) Read(p []byte) (n int, err error) {
return t.reader.Read(p)
}
func (t *tImpl) Write(p []byte) (n int, err error) {
return t.writeCloser.Write(p)
}
// Close closes all session resources in the following order:
//
// 1. stdin pipe
// 2. SSH session
// 3. SSH client
//
// Errors are returned with priority matching the same order.
func (t *tImpl) Close() (err error) {
defer t.trace.ConnectionClosed(t.target, err)
var (
writeCloseErr error
sshSessionCloseErr error
)
if t.writeCloser != nil {
writeCloseErr = t.writeCloser.Close()
}
if t.sshSession != nil {
sshSessionCloseErr = t.sshSession.Close()
}
// Use dialer to close the client, so we don't close a pre-existing client.
err = t.dialer.Close(t.sshClient)
if err == nil {
err = writeCloseErr
}
if err == nil {
err = sshSessionCloseErr
}
return err
}
type traceReader struct {
r io.Reader
trace *ClientTrace
}
func (t *tImpl) injectTraceReader() {
t.reader = &traceReader{r: t.reader, trace: t.trace}
}
func (tr *traceReader) Read(p []byte) (c int, err error) {
tr.trace.ReadStart(p)
defer func(begin time.Time) {
tr.trace.ReadDone(p, c, err, time.Since(begin))
}(time.Now())
c, err = tr.r.Read(p)
return
}
type traceWriter struct {
w io.WriteCloser
trace *ClientTrace
}
func (t *tImpl) injectTraceWriter() {
t.writeCloser = &traceWriter{w: t.writeCloser, trace: t.trace}
}
func (tw *traceWriter) Write(p []byte) (c int, err error) {
tw.trace.WriteStart(p)
defer func(begin time.Time) {
tw.trace.WriteDone(p, c, err, time.Since(begin))
}(time.Now())
c, err = tw.w.Write(p)
return
}
func (tw *traceWriter) Close() (err error) {
return tw.w.Close()
}