From fafe3ee196a0a9f5d02dd4467b00bc61779661da Mon Sep 17 00:00:00 2001 From: Jonathan Barney Date: Wed, 18 Aug 2021 07:20:14 -0700 Subject: [PATCH 1/4] Allow webhook to set multiple headers --- notifiers/webhook.go | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/notifiers/webhook.go b/notifiers/webhook.go index 3831718a..b33f94d9 100644 --- a/notifiers/webhook.go +++ b/notifiers/webhook.go @@ -3,16 +3,17 @@ package notifiers import ( "bytes" "fmt" + "io/ioutil" + "net/http" + "strings" + "time" + "github.com/statping/statping/types/failures" "github.com/statping/statping/types/notifications" "github.com/statping/statping/types/notifier" "github.com/statping/statping/types/null" "github.com/statping/statping/types/services" "github.com/statping/statping/utils" - "io/ioutil" - "net/http" - "strings" - "time" ) var _ notifier.Notifier = (*webhooker)(nil) @@ -85,15 +86,30 @@ func (w *webhooker) Valid(values notifications.Values) error { } func (w *webhooker) sendHttpWebhook(body string) (*http.Response, error) { - utils.Log.Infoln(fmt.Sprintf("sending body: '%v' to %v as a %v request", body, w.Host.String, w.Var1.String)) client := new(http.Client) client.Timeout = 10 * time.Second req, err := http.NewRequest(w.Var1.String, w.Host.String, bytes.NewBufferString(body)) if err != nil { return nil, err } + if w.ApiKey.String != "" { + req.Header.Add("Content-Type", w.ApiKey.String) + } else { + req.Header.Add("Content-Type", "application/json") + } + req.Header.Set("User-Agent", "Statping") + req.Header.Set("Statping-Version", utils.Params.GetString("VERSION")) + + var customHeaders []string + if w.ApiSecret.String != "" { - keyVal := strings.SplitN(w.ApiSecret.String, "=", 2) + customHeaders = strings.Split(w.ApiSecret.String, ",") + } else { + customHeaders = nil + } + + for _, h := range customHeaders { + keyVal := strings.SplitN(h, "=", 2) if len(keyVal) == 2 { if keyVal[0] != "" && keyVal[1] != "" { if strings.ToLower(keyVal[0]) == "host" { @@ -104,13 +120,8 @@ func (w *webhooker) sendHttpWebhook(body string) (*http.Response, error) { } } } - if w.ApiKey.String != "" { - req.Header.Add("Content-Type", w.ApiKey.String) - } else { - req.Header.Add("Content-Type", "application/json") - } - req.Header.Set("User-Agent", "Statping") - req.Header.Set("Statping-Version", utils.Params.GetString("VERSION")) + + utils.Log.Infoln(fmt.Sprintf("sending body: '%v' to %v as a %v request with headers: '%v'", body, w.Host.String, w.Var1.String, req.Header)) resp, err := client.Do(req) if err != nil { return nil, err From 2516aacc96b01de9c8da3717bea6b03813c44de5 Mon Sep 17 00:00:00 2001 From: Jonathan Barney Date: Wed, 18 Aug 2021 07:34:59 -0700 Subject: [PATCH 2/4] fix imports --- notifiers/webhook.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/notifiers/webhook.go b/notifiers/webhook.go index 57722a59..2706fa89 100644 --- a/notifiers/webhook.go +++ b/notifiers/webhook.go @@ -3,16 +3,17 @@ package notifiers import ( "bytes" "fmt" + "io/ioutil" + "net/http" + "strings" + "time" + "github.com/statping-ng/statping-ng/types/failures" "github.com/statping-ng/statping-ng/types/notifications" "github.com/statping-ng/statping-ng/types/notifier" "github.com/statping-ng/statping-ng/types/null" "github.com/statping-ng/statping-ng/types/services" "github.com/statping-ng/statping-ng/utils" - "io/ioutil" - "net/http" - "strings" - "time" ) var _ notifier.Notifier = (*webhooker)(nil) From f4b24bb7068f8b2731552cdeff8305efd10c5206 Mon Sep 17 00:00:00 2001 From: Jonathan Barney Date: Tue, 14 Sep 2021 10:40:12 -0700 Subject: [PATCH 3/4] update user agent --- notifiers/webhook.go | 2 +- utils/utils.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/notifiers/webhook.go b/notifiers/webhook.go index cf78949a..72210379 100644 --- a/notifiers/webhook.go +++ b/notifiers/webhook.go @@ -97,7 +97,7 @@ func (w *webhooker) sendHttpWebhook(body string) (*http.Response, error) { } else { req.Header.Add("Content-Type", "application/json") } - req.Header.Set("User-Agent", "Statping") + req.Header.Set("User-Agent", "Statping-ng") req.Header.Set("Statping-Version", utils.Params.GetString("VERSION")) var customHeaders []string diff --git a/utils/utils.go b/utils/utils.go index 8338a46e..794638f4 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -174,7 +174,7 @@ func HttpRequest(endpoint, method string, contentType interface{}, headers []str return nil, nil, err } // set default headers so end user can overwrite them if needed - req.Header.Set("User-Agent", "Statping") + req.Header.Set("User-Agent", "Statping-ng") req.Header.Set("Statping-Version", Params.GetString("VERSION")) req.Header.Set("Accept", "text/html") From 8431ab5ed6e33b03b6943c2b0b94d9d63d421b22 Mon Sep 17 00:00:00 2001 From: Jonathan Barney Date: Tue, 14 Sep 2021 10:41:01 -0700 Subject: [PATCH 4/4] remove logs --- notifiers/webhook.go | 1 - 1 file changed, 1 deletion(-) diff --git a/notifiers/webhook.go b/notifiers/webhook.go index 72210379..2b2c5b2d 100644 --- a/notifiers/webhook.go +++ b/notifiers/webhook.go @@ -121,7 +121,6 @@ func (w *webhooker) sendHttpWebhook(body string) (*http.Response, error) { } } - utils.Log.Infoln(fmt.Sprintf("sending body: '%v' to %v as a %v request with headers: '%v'", body, w.Host.String, w.Var1.String, req.Header)) resp, err := client.Do(req) if err != nil { return nil, err