-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
223 lines (202 loc) · 4.94 KB
/
util.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
package main
import (
"fmt"
"io"
"strconv"
"strings"
"bufio"
)
// Order is important
const (
BPF_L7_PROTOCOL_UNKNOWN = iota
BPF_L7_PROTOCOL_REDIS
)
const (
L7_PROTOCOL_REDIS = "REDIS"
L7_PROTOCOL_UNKNOWN = "UNKNOWN"
)
// Order is important
const (
BPF_REDIS_METHOD_UNKNOWN = iota
METHOD_REDIS_COMMAND
METHOD_REDIS_PUSHED_EVENT
METHOD_REDIS_PING
)
// for redis, user space
const (
REDIS_COMMAND = "COMMAND"
REDIS_PUSHED_EVENT = "PUSHED_EVENT"
REDIS_PING = "PING"
)
type L7Event struct {
Fd uint64
Pid uint32
Status uint32
Duration uint64
Protocol string // L7_PROTOCOL_HTTP
Tls bool // Whether request was encrypted
Method string
Payload [1024]uint8
PayloadSize uint32 // How much of the payload was copied
PayloadReadComplete bool // Whether the payload was copied completely
Failed bool // Request failed
WriteTimeNs uint64 // start time of write syscall
Tid uint32
Seq uint32 // tcp seq num
EventReadTime int64
}
type bpfL7Event struct {
Fd uint64
WriteTimeNs uint64
Pid uint32
Status uint32
Duration uint64
Protocol uint8
Method uint8
Padding uint16
Payload [1024]uint8
PayloadSize uint32
PayloadReadComplete uint8
Failed uint8
IsTls uint8
_ [1]byte
Seq uint32
Tid uint32
_ [4]byte
}
// Custom types for the enumeration
type L7ProtocolConversion uint32
type RedisMethodConversion uint32
// String representation of the enumeration values
func (e L7ProtocolConversion) String() string {
switch e {
case BPF_L7_PROTOCOL_REDIS:
return L7_PROTOCOL_REDIS
case BPF_L7_PROTOCOL_UNKNOWN:
return L7_PROTOCOL_UNKNOWN
default:
return "Unknown"
}
}
// String representation of the enumeration values
func (e RedisMethodConversion) String() string {
switch e {
case METHOD_REDIS_COMMAND:
return REDIS_COMMAND
case METHOD_REDIS_PUSHED_EVENT:
return REDIS_PUSHED_EVENT
case METHOD_REDIS_PING:
return REDIS_PING
default:
return "Unknown"
}
}
// Redis data types
const (
SimpleStringPrefix = '+'
ErrorPrefix = '-'
IntegerPrefix = ':'
BulkStringPrefix = '$'
ArrayPrefix = '*'
)
// RedisValue represents a decoded Redis value
type RedisValue interface{}
// ParseRedisProtocol parses a Redis protocol message
func ParseRedisProtocol(reader *bufio.Reader) (RedisValue, error) {
prefix, err := reader.ReadByte()
if err != nil {
return nil, err
}
switch prefix {
case SimpleStringPrefix:
return parseSimpleString(reader)
case ErrorPrefix:
return parseError(reader)
case IntegerPrefix:
return parseInteger(reader)
case BulkStringPrefix:
return parseBulkString(reader)
case ArrayPrefix:
return parseArray(reader)
default:
return nil, fmt.Errorf("unknown prefix: %c", prefix)
}
}
func parseSimpleString(reader *bufio.Reader) (string, error) {
line, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSuffix(line, "\r\n"), nil
}
func parseError(reader *bufio.Reader) (string, error) {
line, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return strings.TrimSuffix(line, "\r\n"), nil
}
func parseInteger(reader *bufio.Reader) (int64, error) {
line, err := reader.ReadString('\n')
if err != nil {
return 0, err
}
return strconv.ParseInt(strings.TrimSuffix(line, "\r\n"), 10, 64)
}
func parseBulkString(reader *bufio.Reader) (string, error) {
lengthLine, err := reader.ReadString('\n')
if err != nil {
return "", err
}
length, err := strconv.Atoi(strings.TrimSuffix(lengthLine, "\r\n"))
if err != nil {
return "", err
}
if length == -1 {
return "", nil // Null bulk string
}
data := make([]byte, length+2) // +2 for \r\n
if _, err := io.ReadFull(reader, data); err != nil {
return "", err
}
return string(data[:length]), nil
}
func parseArray(reader *bufio.Reader) ([]RedisValue, error) {
lengthLine, err := reader.ReadString('\n')
if err != nil {
return nil, err
}
length, err := strconv.Atoi(strings.TrimSuffix(lengthLine, "\r\n"))
if err != nil {
return nil, err
}
if length == -1 {
return nil, nil // Null array
}
array := make([]RedisValue, length)
for i := 0; i < length; i++ {
value, err := ParseRedisProtocol(reader)
if err != nil {
return nil, err
}
array[i] = value
}
return array, nil
}
// ConvertRedisValueToString converts a RedisValue to a string
func ConvertValueToString(value RedisValue) string {
switch v := value.(type) {
case string:
return v
case int64:
return strconv.FormatInt(v, 10)
case []RedisValue:
strArray := make([]string, len(v))
for i, elem := range v {
strArray[i] = ConvertValueToString(elem)
}
return strings.Join(strArray, " ")
default:
return "Unknown"
}
}