This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
/
handler_raw.go
127 lines (111 loc) · 3.05 KB
/
handler_raw.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
package websocket
import (
"bytes"
"compress/zlib"
"encoding/binary"
"fmt"
"io"
"os"
"github.com/andybalholm/brotli"
"github.com/botplayerneo/bili-live-api/dto"
"github.com/botplayerneo/bili-live-api/log"
)
func parseAndHandle(p *dto.WSPayload) error {
payloads := rawDataParserMap[p.ProtocolVersion](p)
for _, payload := range payloads {
handler, ok := opCodeHandlerMap[payload.Operation]
if !ok {
return fmt.Errorf("未知的操作码:%d Body:%s", payload.Operation, string(p.Body))
}
handler(payload)
}
return nil
}
type rawDataParser func(*dto.WSPayload) []*dto.WSPayload
var rawDataParserMap = map[dto.ProtoVer]rawDataParser{
dto.Brotli: brotliDecompressor(),
dto.Zlib: zlibDecompressor(),
dto.JSON: jsonParser(),
dto.Popularity: popularityParser(),
}
func jsonParser() rawDataParser {
return func(p *dto.WSPayload) []*dto.WSPayload {
return []*dto.WSPayload{p}
}
}
func popularityParser() rawDataParser {
return func(p *dto.WSPayload) []*dto.WSPayload {
return []*dto.WSPayload{p}
}
}
func zlibDecompressor() rawDataParser {
return func(p *dto.WSPayload) []*dto.WSPayload {
data := p.Body
buf := bytes.NewReader(data)
reader, err := zlib.NewReader(buf)
if err != nil {
log.Errorf("zlib解压错误: %v", err)
os.Exit(1)
}
b, err := io.ReadAll(reader)
if err != nil {
log.Errorf("zlib解压错误: %v", err)
os.Exit(1)
}
return sliceAndParse(b)
}
}
func brotliDecompressor() rawDataParser {
return func(p *dto.WSPayload) []*dto.WSPayload {
log.Debug("使用Brotli解压")
buf := bytes.NewReader(p.Body)
reader := brotli.NewReader(buf)
b, err := io.ReadAll(reader)
if err != nil {
log.Errorf("brotli解压错误: %v", err)
os.Exit(1)
}
return sliceAndParse(b)
}
}
// Decode 将二进制数据转换为 *dto.WSPayload 类型
func Decode(data []byte) *dto.WSPayload {
l := binary.BigEndian.Uint32(data[0:4])
pv := binary.BigEndian.Uint16(data[6:8])
op := binary.BigEndian.Uint32(data[8:12])
body := data[16:l]
return &dto.WSPayload{
RawBytes: data,
PacketLength: int(l),
HeaderLength: 16,
ProtocolVersion: dto.ProtoVer(pv),
Operation: dto.OPCode(op),
Body: body,
}
}
// Encode 将 *dto.WSPayload 类型转换为二进制数据
func Encode(p *dto.WSPayload) []byte {
if p.RawBytes != nil {
return p.RawBytes
}
header := []byte{0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
p.PacketLength = len(p.Body) + 16
binary.BigEndian.PutUint32(header[0:4], uint32(p.PacketLength))
// header length is a fixed number 16
binary.BigEndian.PutUint16(header[6:8], uint16(p.ProtocolVersion))
binary.BigEndian.PutUint32(header[8:12], uint32(p.Operation))
// sequence id is a fixed number 1
p.RawBytes = append(header, p.Body...)
return p.RawBytes
}
func sliceAndParse(data []byte) []*dto.WSPayload {
total := len(data)
var wsPayloads []*dto.WSPayload
cursor := 0
for cursor < total {
l := int(binary.BigEndian.Uint32(data[cursor : cursor+4]))
wsPayloads = append(wsPayloads, Decode(data[cursor:cursor+l]))
cursor += l
}
return wsPayloads
}