/
websocketchannel.go
285 lines (239 loc) · 8.62 KB
/
websocketchannel.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
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// communicator package implement base communicator for network connections.
package communicator
import (
"errors"
"fmt"
"net/http"
"net/url"
"path"
"runtime/debug"
"sync"
"time"
"github.com/aws/amazon-ssm-agent/agent/context"
"github.com/aws/amazon-ssm-agent/agent/log"
"github.com/aws/amazon-ssm-agent/agent/session/communicator/websocketutil"
mgsconfig "github.com/aws/amazon-ssm-agent/agent/session/config"
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/gorilla/websocket"
)
// IWebSocketChannel is the interface for ControlChannel and DataChannel.
type IWebSocketChannel interface {
Initialize(context context.T,
channelId string,
channelType string,
channelRole string,
channelToken string,
region string,
signer *v4.Signer,
onMessageHandler func([]byte),
onErrorHandler func(error)) error
Open(log log.T, dialer *websocket.Dialer) error
Close(log log.T) error
GetChannelToken() string
SetChannelToken(token string)
StartPings(log log.T, pingInterval time.Duration)
SendMessage(log log.T, input []byte, inputType int) error
SetUrl(url string)
SetSubProtocol(subProtocol string)
}
// WebSocketChannel parent class for ControlChannel and DataChannel.
type WebSocketChannel struct {
OnMessage func([]byte)
OnError func(error)
Context context.T
ChannelToken string
Connection *websocket.Conn
Url string
SubProtocol string
Signer *v4.Signer
Region string
IsOpen bool
writeLock *sync.Mutex
stopPinging chan bool
}
// Initialize a WebSocketChannel object.
func (webSocketChannel *WebSocketChannel) Initialize(context context.T,
channelId string,
channelType string,
channelRole string,
channelToken string,
region string,
signer *v4.Signer,
onMessageHandler func([]byte),
onErrorHandler func(error)) error {
hostName := mgsconfig.GetMgsEndpoint(context, region)
if hostName == "" {
return fmt.Errorf("no MGS endpoint found")
}
channelUrl, err := url.Parse(mgsconfig.WebSocketPrefix + hostName)
if err != nil {
return err
}
channelUrl.Path = path.Join(channelUrl.Path, mgsconfig.APIVersion)
channelUrl.Path = path.Join(channelUrl.Path, channelType)
channelUrl.Path = path.Join(channelUrl.Path, channelId)
query := channelUrl.Query()
if channelType == mgsconfig.ControlChannel {
query.Set(mgsconfig.StreamQueryParameter, "input")
query.Add(mgsconfig.RoleQueryParameter, channelRole)
} else if channelType == mgsconfig.DataChannel {
query.Set(mgsconfig.RoleQueryParameter, channelRole)
}
channelUrl.RawQuery = query.Encode()
webSocketChannel.Url = channelUrl.String()
webSocketChannel.Context = context
webSocketChannel.Region = region
webSocketChannel.Signer = signer
webSocketChannel.ChannelToken = channelToken
webSocketChannel.OnError = onErrorHandler
webSocketChannel.OnMessage = onMessageHandler
return nil
}
// SetUrl sets the url for the WebSocketChannel.
func (webSocketChannel *WebSocketChannel) SetUrl(url string) {
webSocketChannel.Url = url
}
// SetSubProtocol sets the subprotocol for the WebSocketChannel.
func (webSocketChannel *WebSocketChannel) SetSubProtocol(subProtocol string) {
webSocketChannel.SubProtocol = subProtocol
}
// getV4SignatureHeader gets the signed header.
func (webSocketChannel *WebSocketChannel) getV4SignatureHeader(log log.T, Url string) (http.Header, error) {
request, err := http.NewRequest("GET", Url, nil)
if webSocketChannel.Signer != nil {
_, err = webSocketChannel.Signer.Sign(request, nil, mgsconfig.ServiceName, webSocketChannel.Region, time.Now())
}
return request.Header, err
}
// GetChannelToken returns channelToken field.
func (webSocketChannel *WebSocketChannel) GetChannelToken() string {
return webSocketChannel.ChannelToken
}
// SetChannelToken updates the token field.
func (webSocketChannel *WebSocketChannel) SetChannelToken(token string) {
webSocketChannel.ChannelToken = token
}
// Open upgrades the http connection to a websocket connection.
func (webSocketChannel *WebSocketChannel) Open(log log.T, dialer *websocket.Dialer) error {
// initialize the write mutex
webSocketChannel.writeLock = &sync.Mutex{}
header, err := webSocketChannel.getV4SignatureHeader(log, webSocketChannel.Url)
if err != nil {
log.Errorf("Failed to get the v4 signature, %v", err)
}
ws, err := websocketutil.NewWebsocketUtil(log, webSocketChannel.Context.AppConfig(), dialer).OpenConnection(webSocketChannel.Url, header)
if err != nil {
return err
}
webSocketChannel.Connection = ws
webSocketChannel.IsOpen = true
webSocketChannel.stopPinging = make(chan bool, 1)
webSocketChannel.StartPings(log, mgsconfig.WebSocketPingInterval)
// spin up a different routine to listen to the incoming traffic
go func() {
defer log.Info("Ending websocket listener")
log.Info("Starting websocket listener")
defer func() {
if msg := recover(); msg != nil {
log.Errorf("WebsocketChannel listener run panic: %v", msg)
log.Errorf("Stacktrace:\n%s", debug.Stack())
}
}()
retryCount := 0
for {
if webSocketChannel.IsOpen == false {
log.Info("Ending the channel listening routine since the channel is closed")
break
}
messageType, rawMessage, err := webSocketChannel.Connection.ReadMessage()
if err != nil {
retryCount++
if retryCount >= mgsconfig.RetryAttempt {
log.Warnf("Reach the retry limit %v for receive messages. Error: %v", mgsconfig.RetryAttempt, err.Error())
webSocketChannel.OnError(err)
break
}
log.Debugf(
"An error happened when receiving the message. Retried times: %d, MessageType: %v, Error: %s",
retryCount,
messageType,
err.Error())
} else if messageType != websocket.TextMessage && messageType != websocket.BinaryMessage {
// We only accept text messages which are interpreted as UTF-8 or binary encoded text.
log.Errorf("Invalid message type %d. We only accept UTF-8 or binary encoded text", messageType)
} else {
retryCount = 0
webSocketChannel.OnMessage(rawMessage)
}
}
}()
return nil
}
// StartPings starts the pinging process to keep the websocket channel alive.
func (webSocketChannel *WebSocketChannel) StartPings(log log.T, pingInterval time.Duration) {
go func(done chan bool) {
log.Info("Starting websocket pinger")
defer log.Info("Ending websocket pinger")
defer func() {
if r := recover(); r != nil {
log.Errorf("Websocket channel start pings panic: %v", r)
log.Errorf("Stacktrace:\n%s", debug.Stack())
}
}()
ticker := time.NewTicker(pingInterval)
defer ticker.Stop()
for {
select {
case <-done:
return
case <-ticker.C:
log.Debug("WebsocketChannel: Send ping. Message.")
err := webSocketChannel.SendMessage(log, []byte("keepalive"), websocket.PingMessage)
if err != nil {
log.Warnf("Error while sending websocket ping: %v", err)
// reconnection logic
return
}
}
}
}(webSocketChannel.stopPinging) // explicitly passed in case it changes on Close/Reopen
}
// Close closes the corresponding connection.
func (webSocketChannel *WebSocketChannel) Close(log log.T) error {
log.Info("Closing websocket channel connection to: " + webSocketChannel.Url)
// Send signal to stop receiving message
if webSocketChannel.IsOpen == true {
webSocketChannel.IsOpen = false
webSocketChannel.stopPinging <- true
close(webSocketChannel.stopPinging)
return websocketutil.NewWebsocketUtil(log, webSocketChannel.Context.AppConfig(), nil).CloseConnection(webSocketChannel.Connection)
}
log.Debugf("Websocket channel connection to: " + webSocketChannel.Url + " is already Closed!")
return nil
}
// SendMessage sends a byte message through the websocket connection.
// Examples of message type are websocket.TextMessage or websocket.Binary
func (webSocketChannel *WebSocketChannel) SendMessage(log log.T, input []byte, inputType int) error {
if webSocketChannel.IsOpen == false {
return errors.New("Can't send message: Connection is closed.")
}
if len(input) < 1 {
return errors.New("Can't send message: Empty input.")
}
webSocketChannel.writeLock.Lock()
defer webSocketChannel.writeLock.Unlock()
err := webSocketChannel.Connection.WriteMessage(inputType, input)
return err
}