Skip to content

Commit

Permalink
Close #102: adds ntfy support to notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
aetaric committed Sep 30, 2024
1 parent 2e5c54b commit 86d55c1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
11 changes: 11 additions & 0 deletions checkrr.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ notifications:
- unknowndetected
- startrun
- endrun
ntfy:
host: ""
topic: ""
token: "" # either use a token or a user and pass, not both.
user: ""
pass: ""
notificationtypes:
- reacquire
- unknowndetected
- startrun
- endrun
stats: # These will slow down the runtime substantually, but... DATA
influxdb1:
url: ""
Expand Down
9 changes: 9 additions & 0 deletions notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func (n *Notifications) Connect() {
n.EnabledServices = append(n.EnabledServices, splunk)
}
}

if n.config.Sub("ntfy") != nil {
ntfy := NtfyNotifs{Log: *log.StandardLogger()}
ntfy.FromConfig(*n.config.Sub("ntfy"))
ntfyConnected := ntfy.Connect()
if ntfyConnected {
n.EnabledServices = append(n.EnabledServices, ntfy)
}
}
}

func (n *Notifications) FromConfig(c viper.Viper) {
Expand Down
72 changes: 72 additions & 0 deletions notifications/ntfy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package notifications

import (
"encoding/base64"
"fmt"
"net/http"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

type NtfyNotifs struct {
host string
topic string
token string
user string
pass string
AllowedNotifs []string
Log log.Logger
}

func (n *NtfyNotifs) FromConfig(config viper.Viper) {
n.host = config.GetString("host")
n.topic = config.GetString("topic")
token := config.GetString("token")
n.AllowedNotifs = config.GetStringSlice("notificationtypes")
if token == "" {
n.user = config.GetString("user")
n.pass = config.GetString("password")
} else {
n.token = config.GetString("token")
}

if n.token == "" && n.user == "" {
n.Log.WithFields(log.Fields{"Startup": true, "Ntfy Connected": false}).Error("Error connecting. Please either use a token or a user. Not both.")
}
}

func (n *NtfyNotifs) Connect() bool {
// stub method. ntfy is http only calls
return true
}

func (n NtfyNotifs) Notify(title string, description string, notifType string, path string) bool {
var allowed bool = false
for _, notif := range n.AllowedNotifs {
if notif == notifType {
allowed = true
}
}
if allowed {
req, err := http.NewRequest("POST", fmt.Sprintf("https://%s/%s", n.host, n.token), strings.NewReader(fmt.Sprintf("%s: %s", description, path)))
if err != nil {
n.Log.WithFields(log.Fields{"Notifications": "Ntfy"}).Error(fmt.Sprintf("Error setting up the http request: %s", err))
}
if n.user != "" {
formatted := fmt.Sprintf("%s:%s", n.user, n.pass)
authHeader := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(formatted)))
req.Header.Set("Authorization", authHeader)
} else if n.token != "" {
req.Header.Set("Authorization", fmt.Sprint("Bearer %s", n.token))
}

req.Header.Set("Title", title)
req.Header.Set("Tags", notifType)
http.DefaultClient.Do(req)
return true
} else {
return false
}
}

0 comments on commit 86d55c1

Please sign in to comment.