2023-05-03 20:03:22 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"log"
|
2023-05-04 01:01:22 +02:00
|
|
|
"net"
|
2023-05-03 20:03:22 +02:00
|
|
|
"os"
|
2023-05-05 15:33:00 +02:00
|
|
|
"path"
|
2023-09-09 19:32:23 +02:00
|
|
|
"time"
|
2023-05-03 20:03:22 +02:00
|
|
|
)
|
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
func genClientStatus() ClientStatus {
|
|
|
|
cs := make(ClientStatus)
|
|
|
|
matchesLock.Lock()
|
2023-05-03 20:03:22 +02:00
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
// Painful data manipulation
|
2023-09-24 16:44:16 +02:00
|
|
|
for pf, times := range matches {
|
|
|
|
pattern, filter := pf.p, pf.f
|
2023-09-24 16:01:56 +02:00
|
|
|
if cs[filter.stream.name] == nil {
|
|
|
|
cs[filter.stream.name] = make(map[string]MapPatternStatus)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-09-24 16:01:56 +02:00
|
|
|
if cs[filter.stream.name][filter.name] == nil {
|
|
|
|
cs[filter.stream.name][filter.name] = make(MapPatternStatus)
|
|
|
|
}
|
2023-09-24 16:44:16 +02:00
|
|
|
cs[filter.stream.name][filter.name][pattern] = &PatternStatus{len(times), nil}
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
matchesLock.Unlock()
|
|
|
|
actionsLock.Lock()
|
2023-05-03 20:03:22 +02:00
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
// Painful data manipulation
|
2023-10-01 12:00:00 +02:00
|
|
|
for pa := range actions {
|
|
|
|
pattern, action := pa.p, pa.a
|
2023-09-24 16:01:56 +02:00
|
|
|
if cs[action.filter.stream.name] == nil {
|
|
|
|
cs[action.filter.stream.name] = make(map[string]MapPatternStatus)
|
|
|
|
}
|
|
|
|
if cs[action.filter.stream.name][action.filter.name] == nil {
|
|
|
|
cs[action.filter.stream.name][action.filter.name] = make(MapPatternStatus)
|
|
|
|
}
|
2023-09-24 16:44:16 +02:00
|
|
|
if cs[action.filter.stream.name][action.filter.name][pattern] == nil {
|
|
|
|
cs[action.filter.stream.name][action.filter.name][pattern] = new(PatternStatus)
|
|
|
|
}
|
|
|
|
ps := cs[action.filter.stream.name][action.filter.name][pattern]
|
|
|
|
if ps.Actions == nil {
|
2023-09-24 16:01:56 +02:00
|
|
|
ps.Actions = make(map[string][]string)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-10-01 12:00:00 +02:00
|
|
|
for then := range actions[pa] {
|
|
|
|
ps.Actions[action.name] = append(ps.Actions[action.name], then.Format(time.DateTime))
|
|
|
|
}
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-09-24 16:01:56 +02:00
|
|
|
actionsLock.Unlock()
|
|
|
|
return cs
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
|
2023-10-01 12:00:00 +02:00
|
|
|
func genFlushedMatches(og map[*Filter]int) map[string]map[string]int {
|
|
|
|
ret := make(map[string]map[string]int)
|
|
|
|
for filter, nb := range og {
|
|
|
|
if ret[filter.stream.name] == nil {
|
|
|
|
ret[filter.stream.name] = make(map[string]int)
|
|
|
|
}
|
|
|
|
ret[filter.stream.name][filter.name] = nb
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func genFlushedActions(og map[*Action]int) map[string]map[string]map[string]int {
|
|
|
|
ret := make(map[string]map[string]map[string]int)
|
|
|
|
for action, nb := range og {
|
|
|
|
if ret[action.filter.stream.name] == nil {
|
|
|
|
ret[action.filter.stream.name] = make(map[string]map[string]int)
|
|
|
|
}
|
|
|
|
if ret[action.filter.stream.name][action.filter.name] == nil {
|
|
|
|
ret[action.filter.stream.name][action.filter.name] = make(map[string]int)
|
|
|
|
}
|
|
|
|
ret[action.filter.stream.name][action.filter.name][action.name] = nb
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-05-04 01:01:22 +02:00
|
|
|
func createOpenSocket() net.Listener {
|
2023-09-03 12:13:18 +02:00
|
|
|
err := os.MkdirAll(path.Dir(*SocketPath), 0755)
|
2023-05-05 15:33:00 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("FATAL Failed to create socket directory")
|
|
|
|
}
|
2023-09-03 12:13:18 +02:00
|
|
|
_, err = os.Stat(*SocketPath)
|
2023-05-03 20:03:22 +02:00
|
|
|
if err == nil {
|
2023-05-05 15:33:00 +02:00
|
|
|
log.Println("WARN socket", SocketPath, "already exists: Is the daemon already running? Deleting.")
|
2023-09-03 12:13:18 +02:00
|
|
|
err = os.Remove(*SocketPath)
|
2023-05-03 20:03:22 +02:00
|
|
|
if err != nil {
|
2023-05-05 15:33:00 +02:00
|
|
|
log.Fatalln("FATAL Failed to remove socket:", err)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-03 12:13:18 +02:00
|
|
|
ln, err := net.Listen("unix", *SocketPath)
|
2023-05-03 20:03:22 +02:00
|
|
|
if err != nil {
|
2023-05-05 15:33:00 +02:00
|
|
|
log.Fatalln("FATAL Failed to create socket:", err)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-05-04 01:01:22 +02:00
|
|
|
return ln
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle connections
|
2023-10-01 12:00:00 +02:00
|
|
|
func SocketManager(streams map[string]*Stream) {
|
2023-05-04 01:01:22 +02:00
|
|
|
ln := createOpenSocket()
|
|
|
|
defer ln.Close()
|
2023-05-03 20:03:22 +02:00
|
|
|
for {
|
2023-05-04 01:01:22 +02:00
|
|
|
conn, err := ln.Accept()
|
2023-05-03 20:03:22 +02:00
|
|
|
if err != nil {
|
2023-05-04 01:01:22 +02:00
|
|
|
log.Println("ERROR Failed to open connection from cli:", err)
|
2023-05-03 23:56:25 +02:00
|
|
|
continue
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-05-04 01:01:22 +02:00
|
|
|
go func(conn net.Conn) {
|
2023-09-24 16:01:56 +02:00
|
|
|
defer conn.Close()
|
2023-05-04 01:01:22 +02:00
|
|
|
var request Request
|
2023-05-03 20:03:22 +02:00
|
|
|
var response Response
|
2023-05-04 01:01:22 +02:00
|
|
|
|
|
|
|
err := gob.NewDecoder(conn).Decode(&request)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR Invalid Message from cli:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-03 20:03:22 +02:00
|
|
|
switch request.Request {
|
2023-09-24 16:01:56 +02:00
|
|
|
case Show:
|
|
|
|
response.ClientStatus = genClientStatus()
|
2023-05-03 20:03:22 +02:00
|
|
|
case Flush:
|
2023-10-01 12:00:00 +02:00
|
|
|
le := LogEntry{time.Now(), request.Pattern, "", "", false}
|
|
|
|
matches := FlushMatchOrder{request.Pattern, make(chan map[*Filter]int)}
|
|
|
|
actions := FlushActionOrder{request.Pattern, make(chan map[*Action]int)}
|
|
|
|
flushToMatchesC <- matches
|
|
|
|
flushToActionsC <- actions
|
|
|
|
flushToDatabaseC <- le
|
|
|
|
|
|
|
|
response.FlushedMatches = genFlushedMatches(<-matches.ret)
|
|
|
|
response.FlushedActions = genFlushedActions(<-actions.ret)
|
2023-05-03 20:03:22 +02:00
|
|
|
default:
|
2023-05-04 01:01:22 +02:00
|
|
|
log.Println("ERROR Invalid Message from cli: unrecognised Request type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
err = gob.NewEncoder(conn).Encode(response)
|
2023-05-04 01:01:22 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR Can't respond to cli:", err)
|
|
|
|
return
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-05-04 01:01:22 +02:00
|
|
|
}(conn)
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
}
|