generated from falcosecurity/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
182 lines (149 loc) · 4.24 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
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
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"io/fs"
"log"
"net"
"net/http"
"sync"
"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"`
Retention int `json:"retention,omitempty"`
}
type displayMode = string
const (
light displayMode = "light"
dark displayMode = "dark"
)
type configuration struct {
DisplayMode displayMode `json:"displayMode"`
}
var (
config configuration
broadcast chan eventStore
store eventStore
mutex *sync.RWMutex
)
//go:embed dist
var frontend embed.FS
func init() {
broadcast = make(chan eventStore, 20)
store.statsByUUID = make(map[string]map[string]int64)
store.Stats = make(map[string]int64)
mutex = &sync.RWMutex{}
}
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")
d := flag.Bool("d", false, "Enable dark mode as default")
flag.Parse()
if ip := net.ParseIP(*a); ip == nil {
log.Fatalf("[ERROR] : Failed to parse Address")
}
store.Retention = *r
fsys, err := fs.Sub(frontend, "dist")
if err != nil {
log.Fatalf("[ERROR] : Failed to read dist folder")
}
dm := light
if *d {
dm = dark
}
config = configuration{DisplayMode: dm}
http.Handle("/ui/", http.StripPrefix("/ui", http.FileServer(http.FS(fsys))))
http.HandleFunc("/healthz", healthHandler)
http.HandleFunc("/events", eventsHandler)
http.HandleFunc("/config", configHandler)
http.Handle("/ws", websocket.Handler(socket))
http.HandleFunc("/", mainHandler)
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", store.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
}
mutex.Lock()
store.Outputs = e.Outputs
if len(store.Events) >= store.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
mutex.Unlock()
temp := make(map[string]int64)
mutex.RLock()
for _, i := range store.statsByUUID {
for j, k := range i {
temp[j] += k
temp["total"] += k
}
}
mutex.RUnlock()
mutex.Lock()
store.Stats = temp
mutex.Unlock()
broadcast <- store
}
// configHandler sends backend configuration to the UI.
func configHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Add("Content-Type", "application/json")
// nolint: errcheck
json.NewEncoder(w).Encode(config)
}
// healthHandler is a simple handler to test if daemon is UP.
func healthHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "application/json")
// nolint: errcheck
w.Write([]byte(`{"status": "ok"}`))
}
func eventsHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
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
}
}
}