-
Notifications
You must be signed in to change notification settings - Fork 1
/
rxtx.go
406 lines (374 loc) · 10.9 KB
/
rxtx.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package peasocket
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"math/bits"
)
// rxtx.go will contain basic frame marshalling and unmarshalling logic
// agnostic to the data being sent and the websocket control flow.
// Rx provides a way to handle a Websocket transport.
type Rx struct {
LastReceivedHeader Header
trp io.ReadCloser
RxCallbacks RxCallbacks
}
type RxCallbacks struct {
// OnError executed when a decoding error is encountered after
// consuming a non-zero amoung of bytes from the underlying transport.
// If this callback is set then it becomes the responsability of the callback
// to close the underlying transport.
OnError func(rx *Rx, err error)
// If OnCtl is not nil then this is executed on every control frame received.
// These may or may not have a payload that can be readily read from r.
OnCtl func(rx *Rx, payload io.Reader) error
// If OnMessage is not nil it is called on the Rx for every application
// message received (non-control frames).
// The Application Message is contained in message which will return EOF
// when the whole payload has been read.
// The payload bytes are unmasked unless unmasking explicitly disabled on the Rx.
OnMessage func(rx *Rx, message io.Reader) error
}
// SetRxTransport sets the underlying transport of rx.
func (rx *Rx) SetRxTransport(rc io.ReadCloser) {
rx.trp = rc
}
// ReadNextFrame reads a frame from the underlying transport of rx.
// This method is meant to be used after setting the rx's callbacks.
// If the callback is not set then the payload data will be read and discarded.
func (rx *Rx) ReadNextFrame() (int, error) {
var reader io.Reader = rx.trp
h, n, err := DecodeHeader(reader)
if err != nil {
if n > 0 || errors.Is(err, io.EOF) {
// EOF means the connection is no longer usable, must close.
rx.handleErr(err)
}
return n, err
}
rx.LastReceivedHeader = h
// Perform basic frame validation.
op := h.FrameType()
isFin := h.Fin()
isCtl := op.IsControl()
if isCtl {
if !isFin {
err = errors.New("control frame needs FIN bit set")
} else if h.PayloadLength > MaxControlPayload {
err = errors.New("payload too large for control frame (>255)")
}
}
if err != nil {
rx.handleErr(err)
return n, err
}
// Wrap reader to limit to payload length and unmask the received bytes if payload is masked.
if h.IsMasked() {
reader = newMaskedReader(reader, h.Mask)
}
lr := &io.LimitedReader{R: reader, N: int64(h.PayloadLength)}
if isCtl && rx.RxCallbacks.OnCtl != nil {
err = rx.RxCallbacks.OnCtl(rx, lr)
} else if rx.RxCallbacks.OnMessage != nil {
err = rx.RxCallbacks.OnMessage(rx, lr)
} else {
// No callback to handle message, we discard data.
var discard [256]byte
for err == nil {
_, err = lr.Read(discard[:])
}
if err == io.EOF {
err = nil
}
}
if err == nil && lr.N != 0 {
err = errors.New("callback did not consume all bytes in frame")
}
if err != nil {
rx.handleErr(err)
}
return n + int(h.PayloadLength-uint64(lr.N)), err
}
// handleErr is a convenience wrapper for RxCallbacks.OnError. If RxCallbacks.OnError
// is not defined then handleErr just closes the connection.
func (rx *Rx) handleErr(err error) {
if rx.RxCallbacks.OnError != nil {
rx.RxCallbacks.OnError(rx, err)
return
}
rx.trp.Close()
}
// TxBuffered handles the marshalling of frames over a underlying transport.
type TxBuffered struct {
buf bytes.Buffer
trp io.WriteCloser
TxCallbacks TxCallbacks
}
// NewTxBuffered creates a new TxBuffered ready for use.
func NewTxBuffered(wc io.WriteCloser) *TxBuffered {
return &TxBuffered{trp: wc}
}
// SetTxTransport sets the underlying TxBuffered transport writer.
func (tx *TxBuffered) SetTxTransport(wc io.WriteCloser) {
tx.trp = wc
}
// TxCallbacks stores functions to be called on events during marshalling of websocket frames.
type TxCallbacks struct {
OnError func(tx *TxBuffered, err error)
}
// WriteTextMessage writes an UTF-8 encoded message over the transport.
// This implementation does not check if payload is valid UTF-8. This can be done
// with the unicode/utf8 Valid function.
// payload's bytes will be masked and thus mutated if mask is non-zero.
func (tx *TxBuffered) WriteTextMessage(mask uint32, payload []byte) (int, error) {
return tx.writeMessage(mask, payload, true)
}
// WriteTextMessage writes an arbitrary message over the transport.
// payload's bytes will be masked and thus mutated if mask is non-zero.
func (tx *TxBuffered) WriteMessage(mask uint32, payload []byte) (int, error) {
return tx.writeMessage(mask, payload, false)
}
func (tx *TxBuffered) writeMessage(mask uint32, payload []byte, isUTF8 bool) (int, error) {
frameType := FrameBinary
if isUTF8 {
frameType = FrameText
}
tx.buf.Reset()
h := newHeader(frameType, uint64(len(payload)), mask, true)
_, err := h.Encode(&tx.buf)
if err != nil {
return 0, err
}
if h.IsMasked() {
_ = maskWS(mask, payload)
}
_, err = tx.buf.Write(payload)
if err != nil {
return 0, err
}
n, err := tx.buf.WriteTo(tx.trp)
if err != nil && n > 0 {
tx.handleError(err)
}
return int(n), err
}
// WriteFragmentedMessage writes a fragmented message over websocket without allocating
// memory. It reads the application message from payload and masks it with mask.
func (tx *TxBuffered) WriteFragmentedMessage(mask uint32, payload io.Reader, userBuffer []byte) (int, error) {
if userBuffer == nil {
userBuffer = make([]byte, 1024)
}
if len(userBuffer) < 12 {
return 0, errors.New("buffer too small to write messages")
}
buflen := len(userBuffer)
masked := mask != 0
hSize := headerSizeFromTransportSize(buflen, masked)
maxMessageSize := uint64(buflen - hSize)
h := newHeader(FrameBinary, maxMessageSize, mask, false)
isEOF := false
var n int
for !isEOF {
h.Put(userBuffer)
if n == 0 {
h = newHeader(FrameContinuation, maxMessageSize, mask, false)
}
var nBuf int
for !isEOF && hSize+nBuf < len(userBuffer) {
ngot, err := payload.Read(userBuffer[hSize+nBuf:])
nBuf += ngot
isEOF = errors.Is(err, io.EOF)
if err != nil && !isEOF {
if n > 0 {
tx.handleError(err)
}
return n, err
}
}
if nBuf != int(maxMessageSize) {
h.PayloadLength = uint64(nBuf)
newHsize := headerSizeFromMessageSize(nBuf, masked)
// newHsize will always be lesser than hSize.
if newHsize != hSize {
for i := 0; i < nBuf; i++ {
userBuffer[newHsize+i] = userBuffer[hSize+i]
}
hSize = newHsize
}
h = newHeader(FrameContinuation, uint64(nBuf), mask, true)
h.Put(userBuffer)
}
maskWS(mask, userBuffer[hSize:hSize+nBuf])
ngot, err := tx.trp.Write(userBuffer[:hSize+nBuf])
n += ngot
if err != nil {
if n > 0 {
tx.handleError(err)
}
return n, err
}
}
return n, nil
}
func headerSizeFromMessageSize(mSize int, masked bool) (hSize int) {
maskSize := 0
if masked {
maskSize = 4
}
switch {
case mSize < 126:
// Smallest case, 2 byte header for application message under 126 bytes.
hSize = 2 + maskSize
case mSize <= math.MaxUint16:
// 2 bytes same as last case + 16bit payload length
hSize = (2 + 2) + maskSize
default:
hSize = (2 + 8) + maskSize
}
return hSize
}
func headerSizeFromTransportSize(tSize int, masked bool) (hSize int) {
maskSize := 0
if masked {
maskSize = 4
}
switch {
case tSize < 128-maskSize:
// Smallest case, 2 byte header for application message under 126 bytes.
hSize = 2 + maskSize
case tSize < math.MaxUint16-maskSize:
// 2 bytes same as last case + 16bit payload length
hSize = (2 + 2) + maskSize
default:
hSize = (2 + 8) + maskSize
}
return hSize
}
func (tx *TxBuffered) handleError(err error) {
if tx.TxCallbacks.OnError != nil {
tx.TxCallbacks.OnError(tx, err)
return
}
tx.trp.Close()
}
// WriteClose writes a Close frame over the websocket connection. reason will be mutated
// if mask is non-zero.
func (tx *TxBuffered) WriteClose(mask uint32, status StatusCode, reason []byte) (int, error) {
if len(reason) > maxCloseReason {
return 0, errors.New("close reason too long. must be under 123 bytes")
}
tx.buf.Reset()
pl := uint64(2 + len(reason))
h := newHeader(FrameClose, pl, mask, true)
_, err := h.Encode(&tx.buf)
if err != nil {
return 0, err
}
var codebuf [2]byte
binary.BigEndian.PutUint16(codebuf[:], uint16(status))
if h.IsMasked() {
mask = maskWS(mask, codebuf[:])
}
_, err = tx.buf.Write(codebuf[:2])
if err != nil {
return 0, err
}
if h.IsMasked() {
_ = maskWS(mask, reason)
}
_, err = writeFull(&tx.buf, reason)
if err != nil {
return 0, err
}
n, err := tx.buf.WriteTo(tx.trp)
if err != nil && n > 0 {
tx.handleError(err)
}
return int(n), err
}
// WritePong writes a pong message over the Tx.
func (tx *TxBuffered) WritePong(mask uint32, message []byte) (int, error) {
if len(message) > MaxControlPayload {
return 0, errors.New("pong message too long, must be under 125 bytes")
}
return tx.writepingpong(FramePong, mask, message)
}
// WritePing writes a ping message over the Tx.
func (tx *TxBuffered) WritePing(mask uint32, message []byte) (int, error) {
if len(message) > MaxControlPayload {
return 0, errors.New("ping message too long, must be under 125 bytes")
}
return tx.writepingpong(FramePing, mask, message)
}
func (tx *TxBuffered) writepingpong(frame FrameType, mask uint32, message []byte) (int, error) {
tx.buf.Reset()
pl := uint64(len(message))
h := newHeader(frame, pl, mask, true)
_, err := h.Encode(&tx.buf)
if err != nil {
return 0, err
}
if h.IsMasked() {
_ = maskWS(mask, message)
}
_, err = writeFull(&tx.buf, message)
if err != nil {
return 0, err
}
n, err := tx.buf.WriteTo(tx.trp)
if err != nil && n > 0 {
tx.handleError(err)
}
return int(n), err
}
// newMaskedReader creates a new MaskedReader ready for use.
func newMaskedReader(r io.Reader, maskKey uint32) *maskedReader {
return &maskedReader{R: r, MaskKey: maskKey}
}
// maskedReader applies the [WebSocket masking algorithm]
// to the underlying reader.
//
// [WebSocket masking algorithm]: https://tools.ietf.org/html/rfc6455#section-5.3
type maskedReader struct {
R io.Reader
MaskKey uint32
}
// Read implements the [io.Reader] interface for the websocket masking algorithm.
func (mr *maskedReader) Read(b []byte) (int, error) {
lenRead, err := mr.R.Read(b)
if lenRead == 0 {
return 0, err
}
mr.MaskKey = maskWS(mr.MaskKey, b[:lenRead])
return lenRead, nil
}
func maskWS(key uint32, b []byte) uint32 {
if key == 0 {
return 0
}
for len(b) >= 4 {
v := binary.BigEndian.Uint32(b)
binary.BigEndian.PutUint32(b, v^key)
b = b[4:]
}
if len(b) != 0 {
// xor remaining bytes and shift mask.
for i := range b {
b[i] ^= byte(key >> 24)
key = bits.RotateLeft32(key, 8)
}
}
return key
}
// writeFull is a convenience function to perform dst.Write and detect
// bad writer implementations while at it.
func writeFull(dst io.Writer, src []byte) (int, error) {
n, err := dst.Write(src)
if n != len(src) && err == nil {
err = fmt.Errorf("bad writer implementation %T", dst)
}
return n, err
}