-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfunctions.go
46 lines (40 loc) · 959 Bytes
/
functions.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
package surevego
import (
"bufio"
"encoding/json"
"errors"
"os"
)
// LoadEveJSONFile reads a suricata eve.json from a given path and returns two
// channels. One for parsed EveEvents and one for parsing errors. Those two
// channels need to be handled separately.
func LoadEveJSONFile(path string) (<-chan EveEvent, <-chan error) {
eventChan := make(chan EveEvent)
errorChan := make(chan error)
file, err := os.Open(path)
if err != nil {
go func() {
errorChan <- err
close(eventChan)
close(errorChan)
}()
return nil, errorChan
}
scanner := bufio.NewScanner(file)
go func() {
for scanner.Scan() {
ev := EveEvent{}
err = json.Unmarshal(scanner.Bytes(), &ev)
if err != nil {
errorChan <- errors.New("Error unmarshaling eve.json line: " +
err.Error() + " " + scanner.Text())
} else {
eventChan <- ev
}
}
close(eventChan)
close(errorChan)
defer file.Close()
}()
return eventChan, errorChan
}