Make webhook configurable

This commit is contained in:
Nik Rozman
2023-10-27 18:32:41 +02:00
parent 2f9c2c1969
commit 8faf339232
5 changed files with 85 additions and 18 deletions

View File

@@ -4,12 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/go-ini/ini"
"net/http"
)
// WebhookURL is the URL of the Discord webhook.
const WebhookURL = "https://discord.com/api/webhooks/1116824798421594233/ARw2KQvPPIt2wLlw4Ssp98o0VWkjr-FdZ2kpFono8zu5UC-N1Uyysy73wbL_DvYJutya"
// AlertData represents the data to be sent in the alert.
type AlertData struct {
Embeds []Embed `json:"embeds"`
@@ -24,6 +22,16 @@ type Embed struct {
// SendAlert sends an alert to the specified Discord webhook.
func SendAlert(alert string) error {
cfg, err := ini.Load("config.ini")
if err != nil {
fmt.Printf("Failed to read configuration file: %v\n", err)
return nil
}
// WebhookURL is the URL of the Discord webhook.
generalSection := cfg.Section("General")
WebhookURL := generalSection.Key("WebhookURL").String()
url := WebhookURL
alertData := AlertData{

View File

@@ -34,12 +34,12 @@ func (ec *EventCounter) CountEvent(ip, port string) {
ec.mutex.Unlock()
}
// StartMonitoring starts the event monitoring and alerts.
func (ec *EventCounter) StartMonitoring() {
// StartMonitoring starts the event monitoring and alerts with a specified threshold.
func (ec *EventCounter) StartMonitoring(threshold int) {
for range ec.resetTimer.C {
ec.mutex.Lock()
for key, count := range ec.counts {
if count >= 10 {
if count >= threshold {
ip, port := parseKey(key)
ec.alertHandler(ip, port, count)
}
@@ -68,6 +68,6 @@ func (ec *EventCounter) StopMonitoring() {
// HandleAlert is a placeholder for alert handling logic.
func HandleAlert(ip, port string, count int) {
SendAlert(fmt.Sprintf("Alert: Source IP %s, Port %s exceeded the threshold with a count of %d\n", ip, port, count))
SendAlert(fmt.Sprintf("Source IP %s, Port %s exceeded the threshold with a count of %d\n", ip, port, count))
fmt.Printf("Alert: Source IP %s, Port %s exceeded the threshold with a count of %d\n", ip, port, count)
}