2023-05-03 20:03:22 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
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-10-04 12:00:00 +02:00
|
|
|
"sync"
|
2023-09-09 19:32:23 +02:00
|
|
|
"time"
|
2023-10-12 12:00:00 +02:00
|
|
|
|
|
|
|
"framagit.org/ppom/reaction/logger"
|
2023-05-03 20:03:22 +02:00
|
|
|
)
|
|
|
|
|
2023-10-04 12:00:00 +02:00
|
|
|
func genClientStatus(local_actions ActionsMap, local_matches MatchesMap, local_actionsLock, local_matchesLock *sync.Mutex) ClientStatus {
|
2023-09-24 16:01:56 +02:00
|
|
|
cs := make(ClientStatus)
|
2023-10-04 12:00:00 +02:00
|
|
|
local_matchesLock.Lock()
|
2023-05-03 20:03:22 +02:00
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
// Painful data manipulation
|
2023-10-04 12:00:00 +02:00
|
|
|
for pf, times := range local_matches {
|
2023-09-24 16:44:16 +02:00
|
|
|
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-10-04 12:00:00 +02:00
|
|
|
local_matchesLock.Unlock()
|
|
|
|
local_actionsLock.Lock()
|
2023-05-03 20:03:22 +02:00
|
|
|
|
2023-09-24 16:01:56 +02:00
|
|
|
// Painful data manipulation
|
2023-10-04 12:00:00 +02:00
|
|
|
for pa, times := range local_actions {
|
2023-10-01 12:00:00 +02:00
|
|
|
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-04 12:00:00 +02:00
|
|
|
for then := range times {
|
2023-10-01 12:00:00 +02:00
|
|
|
ps.Actions[action.name] = append(ps.Actions[action.name], then.Format(time.DateTime))
|
|
|
|
}
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
2023-10-04 12:00:00 +02:00
|
|
|
local_actionsLock.Unlock()
|
2023-09-24 16:01:56 +02:00
|
|
|
return cs
|
2023-05-03 20:03:22 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Failed to create socket directory")
|
2023-05-05 15:33:00 +02:00
|
|
|
}
|
2023-09-03 12:13:18 +02:00
|
|
|
_, err = os.Stat(*SocketPath)
|
2023-05-03 20:03:22 +02:00
|
|
|
if err == nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.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-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("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-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("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-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.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 {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.ERROR, "Invalid Message from cli:", err)
|
2023-05-04 01:01:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-03 20:03:22 +02:00
|
|
|
switch request.Request {
|
2023-09-24 16:01:56 +02:00
|
|
|
case Show:
|
2023-10-04 12:00:00 +02:00
|
|
|
response.ClientStatus = genClientStatus(actions, matches, &actionsLock, &matchesLock)
|
2023-05-03 20:03:22 +02:00
|
|
|
case Flush:
|
2023-10-20 12:00:00 +02:00
|
|
|
le := LogEntry{time.Now(), request.Pattern, "", "", 0, false}
|
2023-10-04 12:00:00 +02:00
|
|
|
matchesC := FlushMatchOrder{request.Pattern, make(chan MatchesMap)}
|
|
|
|
actionsC := FlushActionOrder{request.Pattern, make(chan ActionsMap)}
|
|
|
|
flushToMatchesC <- matchesC
|
|
|
|
flushToActionsC <- actionsC
|
2023-10-01 12:00:00 +02:00
|
|
|
flushToDatabaseC <- le
|
|
|
|
|
2023-10-04 12:00:00 +02:00
|
|
|
var lock sync.Mutex
|
|
|
|
response.ClientStatus = genClientStatus(<-actionsC.ret, <-matchesC.ret, &lock, &lock)
|
2023-05-03 20:03:22 +02:00
|
|
|
default:
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.ERROR, "Invalid Message from cli: unrecognised Request type")
|
2023-05-04 01:01:22 +02:00
|
|
|
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 {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.ERROR, "Can't respond to cli:", err)
|
2023-05-04 01:01:22 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|