-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel.go
310 lines (256 loc) · 8.36 KB
/
channel.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
package gimlet
import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"github.com/Cyberax/gimlet/internal"
"github.com/Cyberax/gimlet/internal/utils"
"github.com/Cyberax/gimlet/log"
"github.com/Cyberax/gimlet/mgsproto"
"github.com/xtaci/smux"
"golang.org/x/net/websocket"
"io"
"sync"
)
type Channel struct {
options ChannelOptions
mtx sync.Mutex
conn *websocket.Conn
done chan bool
// This channel is signalled when we either get the muxer running
// or if we end up with an error set.
handshakeComplete chan bool
err error
sender *internal.Sender
receiver *internal.Receiver
muxer *smux.Session
stats log.Stats
}
// NewChannel creates a new Gimlet connection to the specified endpoint, the websocket connection
// uses the standard defaults. `ctx` parameter can be used for cancellation or deadline support.
func NewChannel(ctx context.Context, endpoint string, token string, options ChannelOptions) (*Channel, error) {
config, err := websocket.NewConfig(endpoint, endpoint)
if err != nil {
return nil, err
}
conn, err := internal.DialWebSocket(ctx, config)
if err != nil {
return nil, err
}
channel := NewChannelWithConnection(conn, token, options)
return channel, nil
}
// NewChannelWithConnection creates a new Gimlet channel over a caller-provided Websocket connection. The function
// still does the full MGS handshake.
func NewChannelWithConnection(conn *websocket.Conn, token string, options ChannelOptions) *Channel {
wc := &Channel{
options: options,
done: make(chan bool),
handshakeComplete: make(chan bool),
conn: conn,
}
wsAdapter := internal.NewWSAdapter(conn)
wc.sender = internal.NewSender(wsAdapter, wc.close,
options.MaxPacketsPerSecond, options.ResendTimeout, options.Log, &wc.stats)
wc.receiver = internal.NewReceiver(wsAdapter, wc.close, wc.sender.EnqueueAck, options.Log, &wc.stats)
wc.options.Log.LogInfo("Starting connection to token: " + token)
wc.sender.EnqueueChannelOpen(token)
go wc.runControlQueueReader()
return wc
}
// GetStats returns the object that tracks live channel statistics
func (c *Channel) GetStats() *log.Stats {
return &c.stats
}
// WaitUntilReady waits for the MGS handshake to complete
func (c *Channel) WaitUntilReady() error {
<-c.handshakeComplete
return c.GetError()
}
// GetHandshakeChannel returns the channel that becomes readable when the MGS handshake is complete.
// You can use it to monitor for the completion in a `select` clause. Check the `GetError` method to
// get the result of the handshake.
func (c *Channel) GetHandshakeChannel() <-chan bool {
return c.handshakeComplete
}
// GetError returns the error that has caused the connection to be closed. Normal closure (via `Shutdown` method)
// is signalled by the `io.EOF` error.
// It returns `nil` while the channel is open.
func (c *Channel) GetError() error {
c.mtx.Lock()
defer c.mtx.Unlock()
return c.err
}
// GetDieChan returns the channel that becomes readable once the channel is completely terminated
func (c *Channel) GetDieChan() <-chan bool {
return c.done
}
// Terminate closes the connection and frees all resources taken by it. It does not block waiting
// for an orderly shutdown.
func (c *Channel) Terminate() {
c.close(io.ErrClosedPipe)
}
// OpenStream creates a new channel to the target port. If the connection is not yet hands
//
// The returned `smux.Stream` implements the `net.Conn` interface and can be used in any methods that accept it.
// If the SSM agent can't establish the connection, the returned `smux.Stream` will immediately fail with an
// error on the first `Read` operation.
func (c *Channel) OpenStream() (*smux.Stream, error) {
err := c.WaitUntilReady() // Wait for the muxer to be set
if err != nil {
return nil, err
}
// If there's no error then the muxer is ready
utils.PanicIf(c.muxer == nil, "muxer is not set")
return c.muxer.OpenStream()
}
func (c *Channel) close(err error) {
c.mtx.Lock()
defer c.mtx.Unlock()
if !utils.IsChannelOpen(c.done) {
return
}
if err == io.EOF {
c.options.Log.LogInfo("channel was closed gracefully")
} else {
c.options.Log.LogError("unexpected channel closure: " + err.Error())
}
c.err = err
if c.muxer != nil {
_ = c.muxer.Close()
}
c.sender.Shutdown()
c.receiver.Shutdown()
_ = c.conn.Close()
if utils.IsChannelOpen(c.handshakeComplete) {
close(c.handshakeComplete)
}
close(c.done)
}
// Shutdown attempts to close the connection gracefully. It sends a `TerminateSession` message and
// then waits for the connection to be closed. `ctx context.Context` can be used for deadline/cancellation.
// If the deadline specified in the context expires before the connection is closed, the connection is forcibly
// terminated. All resources are freed by the time this method returns.
func (c *Channel) Shutdown(ctx context.Context) {
_ = c.sender.EnqueueControlFlag(mgsproto.TerminateSession)
select {
case <-c.done:
case <-ctx.Done():
}
c.Terminate()
}
func (c *Channel) runControlQueueReader() {
loop:
for {
ch := c.receiver.ControlReaderQueue().ReadChan()
select {
case msg := <-ch:
if msg != nil {
c.dispatch(msg)
}
case <-c.done:
break loop
}
}
}
func (c *Channel) dispatch(msg *mgsproto.ClientMessage) {
if msg.MessageType == mgsproto.PausePublicationMessage {
c.options.Log.LogInfo("treating pause_publication as channel_closed")
c.close(io.EOF)
return
}
if msg.MessageType == mgsproto.StartPublicationMessage {
// Nothing to do here
return
}
if msg.MessageType == mgsproto.AcknowledgeMessage {
c.handleAck(msg.Payload)
return
}
if msg.MessageType == mgsproto.ChannelClosedMessage {
c.options.Log.LogInfo("channel_closed received from the server")
c.close(io.EOF)
return
}
if msg.MessageType == mgsproto.OutputStreamMessage {
switch msg.PayloadType {
case mgsproto.HandshakeRequestPayloadType:
c.handleHandshake(msg)
case mgsproto.HandshakeCompletePayloadType:
c.runMuxer()
case mgsproto.Flag:
c.handleFlag(msg)
default:
c.stats.NumUnknownPackets.Add(1)
c.options.Log.LogInfo("Unknown packet type ignored", msg.PayloadType.String())
}
}
}
func (c *Channel) handleAck(payload []byte) {
ack := mgsproto.AcknowledgeContent{}
err := json.Unmarshal(payload, &ack)
if err != nil {
err = fmt.Errorf("failed to parse an ack: %w", err)
c.close(err)
return
}
c.sender.AckReceived(ack.SequenceNumber)
}
func (c *Channel) handleFlag(msg *mgsproto.ClientMessage) {
if len(msg.Payload) != 4 {
c.stats.NumUnknownPackets.Add(1)
c.options.Log.LogInfo("Malformed flag ignored")
return
}
c.stats.NumFlags.Add(1)
flag := mgsproto.FlagMessage(binary.BigEndian.Uint32(msg.Payload))
if flag == mgsproto.TerminateSession {
c.options.Log.LogInfo("Terminate session received")
}
if flag == mgsproto.ConnectToPortError {
c.options.Log.LogInfo("Port error")
c.options.OnConnectionFailedFlagReceived()
}
}
func (c *Channel) runMuxer() {
c.mtx.Lock()
defer c.mtx.Unlock()
// `runMuxer` can be called more than once if the server retries the message
if c.muxer != nil || c.err != nil {
return
}
rw := internal.NewReadWriter(c.receiver.DataReaderQueue(), c.sender.EnqueueData)
var err error
c.muxer, err = smux.Client(rw, c.options.SmuxConfig)
utils.PanicIfErr(err) // Error can only happen if `cfg` is malformed
close(c.handshakeComplete)
}
func (c *Channel) handleHandshake(msg *mgsproto.ClientMessage) {
hrp := mgsproto.HandshakeRequestPayload{}
err := json.Unmarshal(msg.Payload, &hrp)
if err != nil {
c.close(fmt.Errorf("failed to unmarshal the handshake request: %w", err))
return
}
if len(hrp.RequestedClientActions) != 1 {
c.close(fmt.Errorf("handshake request has more than 1 action"))
return
}
if hrp.RequestedClientActions[0].ActionType != mgsproto.SessionType {
c.close(fmt.Errorf("unknown action specified"))
return
}
var handshakeResponse mgsproto.HandshakeResponsePayload
handshakeResponse.ClientVersion = mgsproto.GimletVersion
handshakeResponse.ProcessedClientActions = []mgsproto.ProcessedClientAction{}
processedAction1 := mgsproto.ProcessedClientAction{}
processedAction1.ActionType = mgsproto.SessionType
processedAction1.ActionStatus = mgsproto.Success
handshakeResponse.ProcessedClientActions = append(handshakeResponse.ProcessedClientActions, processedAction1)
err = c.sender.SerializeAndEnqueueControl(mgsproto.HandshakeResponsePayloadType, &handshakeResponse)
if err != nil {
c.close(err)
return
}
}