-
Notifications
You must be signed in to change notification settings - Fork 6
/
parse.go
242 lines (216 loc) · 6.14 KB
/
parse.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
package main
import (
"encoding/json"
"log"
"os"
"strings"
"github.com/CN-TU/go-flows/flows"
)
func decodeOneFeature(feature interface{}) interface{} {
switch feature := feature.(type) {
case []interface{}:
ret := make([]interface{}, len(feature))
for i, elem := range feature {
ret[i] = decodeOneFeature(elem)
}
return ret
case map[string]interface{}:
var k, v interface{}
found := false
for k, v = range feature {
if !found {
found = true
} else {
log.Fatalf("Only one key allowed in calls (unexpected %s)\n", k)
}
}
if args, ok := v.([]interface{}); !ok {
log.Fatalf("Call arguments must be an array (unexpected %s)\n", v)
} else {
return decodeOneFeature(append([]interface{}{k}, args...))
}
case json.Number:
if i, err := feature.Int64(); err == nil {
return i
} else if f, err := feature.Float64(); err == nil {
return f
} else {
log.Fatalf("Can't decode %s!\n", feature.String())
}
}
return feature
}
func decodeFeatures(features interface{}) (ret []interface{}) {
if features, ok := features.([]interface{}); ok {
ret = make([]interface{}, len(features))
for i, elem := range features {
ret[i] = decodeOneFeature(elem)
}
return
}
log.Fatal("Feature list must be an array")
return
}
type jsonType int
const (
jsonAuto jsonType = iota
jsonV2
jsonSimple
)
func decodeV2(decoded featureJSONv2, id int) (features []interface{}, control, filter, key []string, bidirectional, allowZero bool, opt flows.FlowOptions) {
flows := decoded.Preprocessing.Flows
if id < 0 || id >= len(flows) {
log.Fatalf("Only %d flows in the file ⇒ id must be between 0 and %d (is %d)\n", len(flows), len(flows)-1, id)
}
return decodeSimple(flows[id], id)
}
var requiredKeys = []string{"active_timeout", "idle_timeout", "bidirectional", "features", "key_features"}
func toStringArray(decoded featureJSONsimple, name string) []string {
arr, ok := decoded[name].([]interface{})
if !ok {
log.Fatalf("%s must be array of strings", name)
}
ret := make([]string, len(arr))
for i := range ret {
ret[i], ok = arr[i].(string)
if !ok {
log.Fatalf("%s must be an array of strings", name)
}
}
return ret
}
func toTimeout(decoded featureJSONsimple, name string) flows.DateTimeNanoseconds {
val := decoded[name].(json.Number)
if val, ok := val.Float64(); ok == nil {
return flows.DateTimeNanoseconds(val * float64(flows.SecondsInNanoseconds))
}
if val, ok := val.Int64(); ok == nil {
return flows.DateTimeNanoseconds(val) * flows.SecondsInNanoseconds
}
log.Fatalf("%s must be a number", name)
return 0
}
func decodeSimple(decoded featureJSONsimple, _ int) (features []interface{}, control, filter, key []string, bidirectional, allowZero bool, opt flows.FlowOptions) {
// Check if we have every required value
for _, val := range requiredKeys {
if _, ok := decoded[val]; !ok {
log.Fatalf("Key %s is required in the flow description, but missing", val)
}
}
features = decodeFeatures(decoded["features"])
key = toStringArray(decoded, "key_features")
bidirectional, ok := decoded["bidirectional"].(bool)
if !ok {
log.Fatalf("bidirectional must be a boolean")
}
if _, ok := decoded["_control_features"]; ok {
control = toStringArray(decoded, "_control_features")
}
if _, ok := decoded["_filter_features"]; ok {
filter = toStringArray(decoded, "_filter_features")
}
if zero, ok := decoded["_allow_zero"]; ok {
if val, ok := zero.(bool); ok {
allowZero = val
} else {
log.Fatal("_allow_zero must be a boolean")
}
}
if packet, ok := decoded["_per_packet"]; ok {
if val, ok := packet.(bool); ok {
opt.PerPacket = val
} else {
log.Fatal("_per_packet must be a boolean")
}
}
opt.ActiveTimeout = toTimeout(decoded, "active_timeout")
opt.IdleTimeout = toTimeout(decoded, "idle_timeout")
opt.TCPExpiry = true
if expiry, ok := decoded["_expire_TCP"]; ok {
if val, ok := expiry.(bool); ok {
opt.TCPExpiry = val
} else {
log.Fatal("_expire_TCP must be a boolean")
}
}
opt.CustomSettings = decoded
return
}
/* simple format:
{
"active_timeout": <Number>,
"idle_timeout": <Number>,
"bidirectional": <bool>,
"features": [...],
"key_features": [...],
"_control_features": [...],
"_filter_features": [...],
"_per_packet": <bool>,
"_allow_zero": <bool>,
"_expire_TCP": <bool>
}
timeouts, features, key_features and bidirectional are required
_per_packet, _allow_zero are assumed false if missing
_expire_TCP is assumed true if missing (tcp expire works only if at least the five tuple is present in the key)
further keys can be queried from features
*/
type featureJSONsimple map[string]interface{}
/* v2 format:
{
"version": "v2",
"preprocessing": {
"flows": [
<simpleformat>
]
}
}
*/
type featureJSONv2 struct {
Version string
Preprocessing struct {
Flows []featureJSONsimple
}
}
func decodeJSON(inputfile string, format jsonType, id int) (features []interface{}, control, filter, key []string, bidirectional, allowZero bool, opt flows.FlowOptions) {
f, err := os.Open(inputfile)
if err != nil {
log.Fatalln("Can't open ", inputfile)
}
defer f.Close()
dec := json.NewDecoder(f)
dec.UseNumber()
switch format {
case jsonV2:
var decoded featureJSONv2
if err := dec.Decode(&decoded); err != nil {
log.Fatalln("Couldn' parse feature spec:", err)
}
return decodeV2(decoded, id)
case jsonSimple:
var decoded featureJSONsimple
if err := dec.Decode(&decoded); err != nil {
log.Fatalln("Couldn' parse feature spec:", err)
}
return decodeSimple(decoded, id)
case jsonAuto:
//first see if we have a version in the file
var decoded featureJSONv2
if err := dec.Decode(&decoded); err != nil {
log.Fatalln("Couldn' parse feature spec:", err)
}
if decoded.Version != "" {
if strings.HasPrefix(decoded.Version, "v2") {
return decodeV2(decoded, id)
}
log.Fatalf("Unknown file format version '%s'\n", decoded.Version)
}
f.Seek(0, 0)
var decodedSimple featureJSONsimple
if err := dec.Decode(&decodedSimple); err != nil {
log.Fatalln("Couldn' parse feature spec:", err)
}
//should be simple - or something we don't know
return decodeSimple(decodedSimple, id)
}
panic("Unknown format specification")
}