From e1b005ce197209e089106330f2ca5f4766cb1c56 Mon Sep 17 00:00:00 2001 From: darius Date: Thu, 9 May 2024 17:52:16 +0200 Subject: [PATCH] file location fix --- wazuh-notify-go/log/log.go | 9 +++++++-- wazuh-notify-go/notification/discord.go | 16 ++++++++++++++++ wazuh-notify-go/services/init.go | 20 +++++++++++++++++--- wazuh-notify-go/types/types.go | 2 +- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/wazuh-notify-go/log/log.go b/wazuh-notify-go/log/log.go index 4864f5a..ebd9eab 100644 --- a/wazuh-notify-go/log/log.go +++ b/wazuh-notify-go/log/log.go @@ -2,13 +2,18 @@ package log import ( "os" + "path" "time" ) -var f, _ = os.OpenFile("active-responses.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) +var logFile *os.File + +func OpenLogFile(BasePath string) { + logFile, _ = os.OpenFile(path.Join(BasePath, "../../log/active-responses.log"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) +} func Log(message string) { - if _, err := f.WriteString("\n" + time.Now().Format(time.DateTime) + message); err != nil { + if _, err := logFile.WriteString("\n" + message + ": " + time.Now().String()); err != nil { panic(err) } } diff --git a/wazuh-notify-go/notification/discord.go b/wazuh-notify-go/notification/discord.go index 1c89b41..2620f7a 100644 --- a/wazuh-notify-go/notification/discord.go +++ b/wazuh-notify-go/notification/discord.go @@ -22,12 +22,28 @@ func SendDiscord(params types.Params) { "Tags: " + params.Tags + "\n\n" + params.Click + var color int + + switch params.Priority { + case 1: + color = 0x339900 + case 2: + color = 0x99cc33 + case 3: + color = 0xffcc00 + case 4: + color = 0xff9966 + case 5: + color = 0xcc3300 + } + message := types.Message{ Username: params.Sender, Embeds: []types.Embed{ { Title: params.Sender, Description: embedDescription, + Color: color, }, }, } diff --git a/wazuh-notify-go/services/init.go b/wazuh-notify-go/services/init.go index 2f8cdb8..4817740 100644 --- a/wazuh-notify-go/services/init.go +++ b/wazuh-notify-go/services/init.go @@ -7,6 +7,8 @@ import ( "github.com/joho/godotenv" "gopkg.in/yaml.v2" "os" + "path" + "runtime" "wazuh-notify/log" "wazuh-notify/types" ) @@ -14,18 +16,30 @@ import ( var inputParams types.Params var configParams types.Params var wazuhData types.WazuhMessage +var BasePath string func InitNotify() types.Params { - err := godotenv.Load() + _, currentFile, _, _ := runtime.Caller(1) + + BasePath = path.Dir(currentFile) + + log.OpenLogFile(BasePath) + + err := godotenv.Load(path.Join(BasePath, "../../etc/.env")) if err != nil { log.Log("env failed to load") + godotenv.Load(path.Join(BasePath, "/.env")) } else { log.Log("env loaded") } wazuhInput() - yamlFile, err := os.ReadFile("./config.yaml") + yamlFile, err := os.ReadFile(path.Join(BasePath, "../../etc/config.yaml")) + if err != nil { + log.Log("yaml failed to load") + yamlFile, err = os.ReadFile(path.Join(BasePath, "config.yaml")) + } yaml.Unmarshal(yamlFile, &configParams) log.Log("yaml loaded") @@ -39,7 +53,7 @@ func InitNotify() types.Params { flag.Parse() - log.Log("yaml loaded") + log.Log("params loaded") inputParams.Targets = configParams.Targets return inputParams diff --git a/wazuh-notify-go/types/types.go b/wazuh-notify-go/types/types.go index cb2970c..75a77e2 100644 --- a/wazuh-notify-go/types/types.go +++ b/wazuh-notify-go/types/types.go @@ -25,5 +25,5 @@ type Message struct { type Embed struct { Title string `json:"title,omitempty"` Description string `json:"description,omitempty"` - Color string `json:"color,omitempty"` + Color int `json:"color,omitempty"` }