2022-05-08 20:49:53 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"beelzebub/parser"
|
|
|
|
"beelzebub/protocols"
|
2022-05-11 22:58:03 +02:00
|
|
|
"beelzebub/tracer"
|
2022-05-08 20:49:53 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var quit = make(chan struct{})
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
configurationsParser := parser.Init("./configurations/beelzebub.yaml", "./configurations/services/")
|
|
|
|
|
|
|
|
coreConfigurations, err := configurationsParser.ReadConfigurationsCore()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fileLogs := configureLogging(coreConfigurations.Core.Logging)
|
|
|
|
defer fileLogs.Close()
|
|
|
|
|
|
|
|
beelzebubServicesConfiguration, err := configurationsParser.ReadConfigurationsServices()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Protocol strategies
|
|
|
|
secureShellStrategy := &protocols.SecureShellStrategy{}
|
|
|
|
hypertextTransferProtocolStrategy := &protocols.HypertextTransferProtocolStrategy{}
|
|
|
|
|
|
|
|
// Protocol manager
|
2022-05-11 22:58:03 +02:00
|
|
|
serviceManager := protocols.InitProtocolManager(logStrategy, hypertextTransferProtocolStrategy)
|
2022-05-08 20:49:53 +02:00
|
|
|
|
|
|
|
for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration {
|
|
|
|
switch beelzebubServiceConfiguration.Protocol {
|
|
|
|
case "http":
|
|
|
|
serviceManager.SetProtocolStrategy(hypertextTransferProtocolStrategy)
|
|
|
|
break
|
|
|
|
case "ssh":
|
|
|
|
serviceManager.SetProtocolStrategy(secureShellStrategy)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Fatalf("Protocol %s not managed", beelzebubServiceConfiguration.Protocol)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := serviceManager.InitService(beelzebubServiceConfiguration)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error during init protocol: %s, %s", beelzebubServiceConfiguration.Protocol, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
<-quit
|
|
|
|
}
|
2022-05-11 22:58:03 +02:00
|
|
|
func logStrategy(event tracer.Event) {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"status": event.Status.String(),
|
|
|
|
"event": event,
|
|
|
|
}).Info("New Event")
|
|
|
|
}
|
2022-05-08 20:49:53 +02:00
|
|
|
|
|
|
|
func configureLogging(configurations parser.Logging) *os.File {
|
|
|
|
file, err := os.OpenFile(configurations.LogsPath, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error opening file: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-05-11 22:58:03 +02:00
|
|
|
log.SetOutput(io.MultiWriter(os.Stdout, file))
|
2022-05-08 20:49:53 +02:00
|
|
|
|
|
|
|
log.SetFormatter(&log.JSONFormatter{
|
|
|
|
DisableTimestamp: configurations.LogDisableTimestamp,
|
|
|
|
})
|
|
|
|
log.SetReportCaller(configurations.DebugReportCaller)
|
|
|
|
if configurations.Debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
} else {
|
|
|
|
log.SetLevel(log.InfoLevel)
|
|
|
|
}
|
|
|
|
return file
|
|
|
|
}
|