forked from DCSO/fever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_stdin.go
79 lines (68 loc) · 1.65 KB
/
input_stdin.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
package input
// DCSO FEVER
// Copyright (c) 2020, 2023, DCSO GmbH
import (
"bufio"
"net"
"os"
"github.com/DCSO/fever/types"
"github.com/DCSO/fever/util"
log "github.com/sirupsen/logrus"
)
// StdinInput is an Input reading JSON EVE input from standard input.
type StdinInput struct {
EventChan chan types.Entry
Verbose bool
Running bool
InputListener net.Listener
StopChan chan bool
StoppedChan chan bool
}
// GetName returns a printable name for the input
func (si *StdinInput) GetName() string {
return "Stdin input"
}
func (si *StdinInput) handleStdinStream() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
json := scanner.Bytes()
e, err := util.ParseJSON(json)
if err != nil {
log.Error(err, string(json[:]))
continue
}
si.EventChan <- e
}
close(si.EventChan)
}
// MakeStdinInput returns a new StdinInput reading from stdin and writing
// parsed events to outChan.
func MakeStdinInput(outChan chan types.Entry) *StdinInput {
si := &StdinInput{
EventChan: outChan,
Verbose: false,
StopChan: make(chan bool),
}
return si
}
// Run starts the StdinInput
func (si *StdinInput) Run() {
if !si.Running {
si.Running = true
si.StopChan = make(chan bool)
go si.handleStdinStream()
}
}
// Stop causes the StdinInput to stop reading from stdin and close all
// associated channels, including the passed notification channel.
func (si *StdinInput) Stop(stoppedChan chan bool) {
if si.Running {
si.StoppedChan = stoppedChan
si.Running = false
close(stoppedChan)
}
}
// SetVerbose sets the input's verbosity level
func (si *StdinInput) SetVerbose(verbose bool) {
si.Verbose = verbose
}