2023-05-03 20:03:22 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
2024-05-20 12:00:00 +02:00
|
|
|
"errors"
|
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-10-12 12:00:00 +02:00
|
|
|
|
|
|
|
"framagit.org/ppom/reaction/logger"
|
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
|
2024-05-20 12:00:00 +02:00
|
|
|
//func SocketManager(streams map[string]*Stream) {
|
|
|
|
func SocketManager(conf *Conf) {
|
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 {
|
2024-05-20 12:00:00 +02:00
|
|
|
case Info:
|
|
|
|
// response.Config = *conf
|
|
|
|
response.Matches = matches
|
|
|
|
response.Actions = actions
|
2023-05-03 20:03:22 +02:00
|
|
|
case Flush:
|
2024-05-20 12:00:00 +02:00
|
|
|
le := LogEntry{time.Now(), 0, request.Flush.P, request.Flush.S, request.Flush.F, 0, false}
|
2024-05-01 11:16:54 +02:00
|
|
|
|
2024-05-20 12:00:00 +02:00
|
|
|
flushToMatchesC <- request.Flush
|
|
|
|
flushToActionsC <- request.Flush
|
2023-10-01 12:00:00 +02:00
|
|
|
flushToDatabaseC <- le
|
|
|
|
|
2023-05-03 20:03:22 +02:00
|
|
|
default:
|
2024-05-20 12:00:00 +02:00
|
|
|
logger.Println(logger.ERROR, "Invalid Message from cli: unrecognised command type")
|
|
|
|
response.Err = errors.New("unrecognised command 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
|
|
|
}
|
|
|
|
}
|