-
Notifications
You must be signed in to change notification settings - Fork 458
/
websocket.go
294 lines (260 loc) · 7.39 KB
/
websocket.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
// Copyright 2021, Chef. All rights reserved.
// https://github.com/q191201771/lal
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: joestarzxh
package base
import (
"bufio"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"math"
"github.com/q191201771/naza/pkg/bele"
)
// WsOpcode The WebSocket Protocol
// https://tools.ietf.org/html/rfc6455
//
// 0 1 2 3
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
// +-+-+-+-+-------+-+-------------+-------------------------------+
// |F|R|R|R| opcode|M| Payload len | Extended payload length |
// |I|S|S|S| (4) |A| (7) | (16/64) |
// |N|V|V|V| |S| | (if payload len==126/127) |
// | |1|2|3| |K| | |
// +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
// | Extended payload length continued, if payload len == 127 |
// + - - - - - - - - - - - - - - - +-------------------------------+
// | |Masking-key, if MASK set to 1 |
// +-------------------------------+-------------------------------+
// | Masking-key (continued) | Payload Data |
// +-------------------------------- - - - - - - - - - - - - - - - +
// : Payload Data continued ... :
// + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
// | Payload Data continued ... |
// +---------------------------------------------------------------+
// opcode:
// * %x0 denotes a continuation frame
// * %x1 denotes a text frame
// * %x2 denotes a binary frame
// * %x3-7 are reserved for further non-control frames
// * %x8 denotes a connection close
// * %x9 denotes a ping
// * %xA denotes a pong
// * %xB-F are reserved for further control frames
// Payload length: 7 bits, 7+16 bits, or 7+64 bits
// Masking-key: 0 or 4 bytes
// mark 加密
//
// for i := 0; i < datalen; i {
// m := markingkeys[i%4]
// data[i] = msg[i] ^ m
// }
type WsOpcode = uint8
const (
Wso_Continuous WsOpcode = iota //连续消息片断
Wso_Text //文本消息片断,
Wso_Binary //二进制消息片断,
// Wso_Rsv3 非控制消息片断保留的操作码,
Wso_Rsv3
Wso_Rsv4
Wso_Rsv5
Wso_Rsv6
Wso_Rsv7
Wso_Close //连接关闭,
Wso_Ping //心跳检查的ping,
Wso_Pong //心跳检查的pong,
// Wso_RsvB 为将来的控制消息片断的保留操作码
Wso_RsvB
Wso_RsvC
Wso_RsvD
Wso_RsvE
Wso_RsvF
)
type WsHeader struct {
Fin bool
Rsv1 bool
Rsv2 bool
Rsv3 bool
Opcode WsOpcode
PayloadLength uint64
Masked bool
MaskKey uint32
}
const WsMagicStr = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
func MakeWsFrameHeader(wsHeader WsHeader) (buf []byte) {
headerSize := 2
payload := uint64(0)
switch {
case wsHeader.PayloadLength < 126:
payload = wsHeader.PayloadLength
case wsHeader.PayloadLength <= math.MaxUint16:
payload = 126
headerSize += 2
case wsHeader.PayloadLength > math.MaxUint16:
payload = 127
headerSize += 8
}
if wsHeader.Masked {
headerSize += 4
}
buf = make([]byte, headerSize, headerSize)
if wsHeader.Fin {
buf[0] |= 1 << 7
}
if wsHeader.Rsv1 {
buf[0] |= 1 << 6
}
if wsHeader.Rsv2 {
buf[0] |= 1 << 5
}
if wsHeader.Rsv3 {
buf[0] |= 1 << 4
}
buf[0] |= wsHeader.Opcode
if wsHeader.Masked {
buf[1] |= 1 << 7
}
buf[1] |= (uint8(payload) & 0x7F)
if payload == 126 {
bele.BePutUint16(buf[2:], uint16(wsHeader.PayloadLength))
} else if payload == 127 {
bele.BePutUint64(buf[2:], wsHeader.PayloadLength)
}
if wsHeader.Masked {
bele.LePutUint32(buf[headerSize-4:], wsHeader.MaskKey)
}
return buf
}
func UpdateWebSocketHeader(secWebSocketKey, protocol string) []byte {
firstLine := "HTTP/1.1 101 Switching Protocol\r\n"
sha1Sum := sha1.Sum([]byte(secWebSocketKey + WsMagicStr))
secWebSocketAccept := base64.StdEncoding.EncodeToString(sha1Sum[:])
var webSocketResponseHeaderStr string
if protocol == "" {
webSocketResponseHeaderStr = firstLine +
"Server: " + LalHttpflvSubSessionServer + "\r\n" +
"Sec-WebSocket-Accept:" + secWebSocketAccept + "\r\n" +
"Keep-Alive: timeout=15, max=100\r\n" +
"Connection: Upgrade\r\n" +
"Upgrade: websocket\r\n" +
CorsHeaders +
"\r\n"
} else {
webSocketResponseHeaderStr = firstLine +
"Server: " + LalHttpflvSubSessionServer + "\r\n" +
"Sec-WebSocket-Accept:" + secWebSocketAccept + "\r\n" +
"Keep-Alive: timeout=15, max=100\r\n" +
"Connection: Upgrade\r\n" +
"Upgrade: websocket\r\n" +
CorsHeaders +
"Sec-WebSocket-Protocol:" + protocol + "\r\n" +
"\r\n"
}
return []byte(webSocketResponseHeaderStr)
}
func ReadWsPayload(r *bufio.Reader) ([]byte, error) {
var h WsHeader
buf := make([]byte, 2)
_, err := io.ReadFull(r, buf)
if err != nil {
return nil, err
}
h.Fin = (buf[0] & 0x80) != 0
h.Rsv1 = (buf[0] & 0x40) != 0
h.Rsv2 = (buf[0] & 0x20) != 0
h.Rsv3 = (buf[0] & 0x10) != 0
h.Opcode = buf[0] & 0x0f
if buf[1]&0x80 != 0 {
h.Masked = true
}
length := buf[1] & 0x7f
switch {
case length < 126:
h.PayloadLength = uint64(length)
case length == 126:
buf = make([]byte, 2)
_, err := io.ReadFull(r, buf)
if err != nil {
return nil, err
}
h.PayloadLength = uint64(binary.BigEndian.Uint16(buf))
case length == 127:
buf = make([]byte, 8)
_, err := io.ReadFull(r, buf)
if err != nil {
return nil, err
}
h.PayloadLength = binary.BigEndian.Uint64(buf)
default:
err = fmt.Errorf("header error: the most significant bit must be 0")
return nil, err
}
if h.Masked {
buf = make([]byte, 4)
_, err := io.ReadFull(r, buf)
if err != nil {
return nil, err
}
h.MaskKey = bele.BeUint32(buf)
}
payload := make([]byte, h.PayloadLength)
_, err = io.ReadFull(r, payload)
if err != nil {
return nil, err
}
if h.Masked {
mask := make([]byte, 4)
binary.BigEndian.PutUint32(mask, h.MaskKey)
cipher(payload, mask, 0)
}
return payload, nil
}
func cipher(payload []byte, mask []byte, offset int) {
n := len(payload)
if n < 8 {
for i := 0; i < n; i++ {
payload[i] ^= mask[(offset+i)%4]
}
return
}
// Calculate position in mask due to previously processed bytes number.
mpos := offset % 4
// Count number of bytes will processed one by one from the beginning of payload.
ln := remain[mpos]
// Count number of bytes will processed one by one from the end of payload.
// This is done to process payload by 8 bytes in each iteration of main loop.
rn := (n - ln) % 8
for i := 0; i < ln; i++ {
payload[i] ^= mask[(mpos+i)%4]
}
for i := n - rn; i < n; i++ {
payload[i] ^= mask[(mpos+i)%4]
}
// NOTE: we use here binary.LittleEndian regardless of what is real
// endianness on machine is. To do so, we have to use binary.LittleEndian in
// the masking loop below as well.
var (
m = binary.LittleEndian.Uint32((mask[:]))
m2 = uint64(m)<<32 | uint64(m)
)
// Skip already processed right part.
// Get number of uint64 parts remaining to process.
n = (n - ln - rn) >> 3
for i := 0; i < n; i++ {
var (
j = ln + (i << 3)
chunk = payload[j : j+8]
)
p := binary.LittleEndian.Uint64(chunk)
p = p ^ m2
binary.LittleEndian.PutUint64(chunk, p)
}
}
// remain maps position in masking key [0,4) to number
// of bytes that need to be processed manually inside Cipher().
var remain = [4]int{0, 3, 2, 1}