forked from falcosecurity/falcosidekick-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (107 loc) · 3.15 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"github.com/falcosecurity/falcosidekick/types"
"golang.org/x/net/websocket"
)
type eventPayload struct {
UUID string `json:"uuid,omitempty"`
Event types.FalcoPayload `json:"event,omitempty"`
Stats map[string]int64 `json:"stats,omitempty"`
Outputs []string `json:"outputs,omitempty"`
}
type eventStore struct {
statsByUUID map[string]map[string]int64
Events []types.FalcoPayload `json:"events,omitempty"`
Stats map[string]int64 `json:"stats,omitempty"`
Outputs []string `json:"outputs,omitempty"`
}
var (
broadcast chan eventStore
store eventStore
retention int
)
func init() {
broadcast = make(chan eventStore, 20)
store.statsByUUID = make(map[string]map[string]int64)
store.Stats = make(map[string]int64)
}
func main() {
a := flag.String("a", "0.0.0.0", "Listen Address")
p := flag.Int("p", 2802, "Listen Port")
r := flag.Int("r", 200, "Number of events to keep in retention")
flag.Parse()
if ip := net.ParseIP(*a); ip == nil {
log.Fatalf("[ERROR] : Failed to parse Address")
}
retention = *r
http.HandleFunc("/", mainHandler)
http.HandleFunc("/healthz", healthHandler)
http.HandleFunc("/events", eventsHandler)
http.Handle("/ui", http.StripPrefix("/ui", http.FileServer(http.Dir("./static"))))
http.Handle("/ws", websocket.Handler(socket))
log.Printf("[INFO] : Falco Sidekick Web UI is up and listening on %s:%d\n", *a, *p)
log.Printf("[INFO] : Retention is %d last events\n", retention)
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", *a, *p), nil); err != nil {
log.Fatalf("[ERROR] : %v\n", err.Error())
}
}
func mainHandler(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
http.Error(w, "Please send a valid request body", http.StatusBadRequest)
return
}
if r.Method != http.MethodPost {
http.Error(w, "Please send with post http method", http.StatusBadRequest)
return
}
d := json.NewDecoder(r.Body)
d.UseNumber()
var e eventPayload
err := d.Decode(&e)
if err != nil {
http.Error(w, "Please send a valid request body", http.StatusBadRequest)
return
}
store.Outputs = e.Outputs
if len(store.Events) >= retention {
store.Events = append(store.Events[1:len(store.Events)-1], e.Event)
} else {
store.Events = append(store.Events, e.Event)
}
store.statsByUUID[e.UUID] = e.Stats
temp := make(map[string]int64)
for _, i := range store.statsByUUID {
for j, k := range i {
temp[j] += k
}
}
store.Stats = temp
broadcast <- store
}
// healthHandler is a simple handler to test if daemon is UP.
func healthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
// nolint: errcheck
w.Write([]byte(`{"status": "ok"}`))
}
func eventsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
s, _ := json.Marshal(store)
// nolint: errcheck
w.Write(s)
}
func socket(ws *websocket.Conn) {
log.Printf("[INFO] : A Websocket connection to WebUI has been established\n")
for {
events := <-broadcast
if err := websocket.JSON.Send(ws, events); err != nil {
break
}
}
}