/
connection.go
306 lines (282 loc) · 8.94 KB
/
connection.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
/*
Copyright 2018 Google Inc. 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License 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.
*/
package websockets
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
"context"
"github.com/gorilla/websocket"
)
var websocketShimInjectedHeadersPath = []string{"resource", "headers"}
type message struct {
Type int
Data []byte
}
func (m *message) Serialize(version int) interface{} {
if m.Type == websocket.TextMessage {
return string(m.Data)
}
if version == 0 {
return []string{
string(m.Data),
}
}
return []string{
base64.StdEncoding.EncodeToString(m.Data),
}
}
// Connection implements a websocket client connection.
//
// This wraps a websocket.Conn connection as defined by the gorilla/websocket library,
// and encapsulates it in an API that is a little more amenable to how the server side
// of our websocket shim is implemented.
type Connection struct {
ctx context.Context
cancel context.CancelFunc
clientMessages chan *message
serverMessages chan *message
protocolVersion int
}
// This map defines the set of headers that should be stripped from the WS request, as they
// are being verified by the websocket.Client.
var stripHeaderNames = map[string]bool{
"Upgrade": true,
"Connection": true,
"Sec-Websocket-Key": true,
"Sec-Websocket-Version": true,
"Sec-Websocket-Extensions": true,
}
// stripWSHeader strips request headers that are not allowed by the websocket.Client library,
// while preserving the rest.
func stripWSHeader(header http.Header) http.Header {
result := http.Header{}
for k, v := range header {
if !stripHeaderNames[k] {
result[k] = v
}
}
return result
}
// NewConnection creates and returns a new Connection.
func NewConnection(ctx context.Context, targetURL string, header http.Header, errCallback func(err error)) (*Connection, error) {
ctx, cancel := context.WithCancel(ctx)
serverConn, _, err := websocket.DefaultDialer.Dial(targetURL, stripWSHeader(header))
if err != nil {
cancel()
return nil, err
}
// The underlying websocket library assumes that clients are written to read from the connection
// in a tight loop. We prefer to instead have the ability to poll for the next message with a timeout.
//
// The most direct way to bridge those two approaches is to spawn a goroutine that reads in a
// tight loop, and then sends the read messages in to an open channel.
serverMessages := make(chan *message, 10)
// Since we are using a channel to pull messages from the connection, we will also use one to
// push messages. That way our handling of reads and writes are consistent.
clientMessages := make(chan *message, 10)
closeConn := make(chan bool)
go func() {
defer func() {
close(serverMessages)
closeConn <- true
}()
for {
select {
case <-ctx.Done():
return
default:
msgType, msgBytes, err := serverConn.ReadMessage()
if err != nil {
errCallback(fmt.Errorf("failed to read a websocket message from the server: %v", err))
// Errors from the server connection are terminal; once an error is returned
// no subsequent calls will succeed.
return
}
serverMessages <- &message{
Type: msgType,
Data: msgBytes,
}
}
}
}()
go func() {
defer func() {
closeConn <- true
}()
for {
select {
case <-ctx.Done():
return
case clientMsg, ok := <-clientMessages:
if !ok {
return
}
if clientMsg == nil {
continue
}
if err := serverConn.WriteMessage(clientMsg.Type, clientMsg.Data); err != nil {
errCallback(fmt.Errorf("failed to forward websocket data to the server: %v", err))
// Errors writing to the server connection are terminal; once an error is returned
// no subsequent calls will succeed.
return
}
}
}
}()
go func() {
<-closeConn
// if either routines finishes, terminate the other
cancel()
// closing the serverConn. This will cause serverConn.ReadMessage to stop.
if err := serverConn.Close(); err != nil {
errCallback(fmt.Errorf("failure closing a server websocket connection: %v", err))
}
}()
return &Connection{
ctx: ctx,
cancel: cancel,
clientMessages: clientMessages,
serverMessages: serverMessages,
}, nil
}
// Close closes the websocket client connection.
func (conn *Connection) Close() {
conn.clientMessages <- &message{
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
}
// Closing the writing routine.
close(conn.clientMessages)
}
// SendClientMessage sends the given message to the websocket server.
//
// The returned error value is non-nill if the connection has been closed.
func (conn *Connection) SendClientMessage(msg interface{}, injectionEnabled bool, injectedHeaders map[string]string) error {
var clientMessage *message
if textMsg, ok := msg.(string); ok {
clientMessage = &message{
Type: websocket.TextMessage,
Data: []byte(textMsg),
}
} else if blobMsg, ok := msg.([]interface{}); ok && len(blobMsg) == 1 {
if blobText, ok := blobMsg[0].(string); ok {
if conn.protocolVersion == 0 {
clientMessage = &message{
Type: websocket.BinaryMessage,
Data: []byte(blobText),
}
} else {
data, err := base64.StdEncoding.DecodeString(blobText)
if err != nil {
log.Printf("unexpected websocket-shim message format: %+v\n", msg)
return fmt.Errorf("unexpected websocket-shim message format: %+v", msg)
}
clientMessage = &message{
Type: websocket.BinaryMessage,
Data: data,
}
}
}
} else {
log.Printf("unexpected websocket-shim message type: %+v\n", msg)
return fmt.Errorf("unexpected websocket-shim message type: %+v", msg)
}
if injectionEnabled {
injectedMsg, err := injectWebsocketMessage(clientMessage, websocketShimInjectedHeadersPath, injectedHeaders)
if err != nil {
log.Printf("Failed to inject websocket message: %v", err)
} else {
clientMessage = injectedMsg
}
}
select {
case <-conn.ctx.Done():
return fmt.Errorf("attempt to send a client message on a closed websocket connection")
default:
conn.clientMessages <- clientMessage
}
return nil
}
// ReadServerMessages reads the next messages from the websocket server.
//
// The returned error value is non-nill if the connection has been closed.
//
// The returned []string value is nil if the error is non-nil, or if the method
// times out while waiting for a server message.
func (conn *Connection) ReadServerMessages() ([]interface{}, error) {
var msgs []interface{}
select {
case serverMsg, ok := <-conn.serverMessages:
if !ok {
// The server messages channel has been closed.
return nil, fmt.Errorf("attempt to read a server message from a closed websocket connection")
}
msgs = append(msgs, serverMsg.Serialize(conn.protocolVersion))
for {
select {
case serverMsg, ok := <-conn.serverMessages:
if ok {
msgs = append(msgs, serverMsg.Serialize(conn.protocolVersion))
} else {
return msgs, nil
}
default:
return msgs, nil
}
}
case <-time.After(time.Second * 20):
return nil, nil
}
}
// injectWebsocketMessage injects a shim header value into a single websocket message in-place.
// Returns a pointer to a new copy of the struct on success.
func injectWebsocketMessage(msg *message, injectionPath []string, injectionValues map[string]string) (*message, error) {
if msg == nil {
return nil, errors.New("unexpected nil message")
}
if injectionValues == nil || len(injectionValues) == 0 {
return msg, nil
}
// Deserialize the websocket message into a JSON object.
var origJSONComponent map[string]interface{}
err := json.Unmarshal(msg.Data, &origJSONComponent)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal as json message: %v", err)
}
var currJSONComponent map[string]interface{}
var ok bool
currJSONComponent = origJSONComponent
for _, pathComponent := range injectionPath {
currJSONComponent, ok = currJSONComponent[pathComponent].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("failed to inject value due to unexpected message json format")
}
}
// Inject the header values into the specified key. Do not overwrite existing header values.
for key, value := range injectionValues {
if _, ok := currJSONComponent[key]; !ok {
currJSONComponent[key] = value
}
}
// Reserialize the websocket message.
newMsgBytes, err := json.Marshal(&origJSONComponent)
if err != nil {
return nil, fmt.Errorf("failed to marshal json as bytes: %v", err)
}
return &message{Type: msg.Type, Data: newMsgBytes}, nil
}