-
Notifications
You must be signed in to change notification settings - Fork 133
/
custom.go
114 lines (97 loc) · 3.47 KB
/
custom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package custom
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"text/template"
"github.com/Masterminds/sprig"
"github.com/pkg/errors"
"go.uber.org/multierr"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/notify/pkg/utils"
"github.com/projectdiscovery/notify/pkg/utils/httpreq"
sliceutil "github.com/projectdiscovery/utils/slice"
)
type Provider struct {
Custom []*Options `yaml:"custom,omitempty"`
counter int
}
type Options struct {
ID string `yaml:"id,omitempty"`
CustomWebhookURL string `yaml:"custom_webhook_url,omitempty"`
CustomMethod string `yaml:"custom_method,omitempty"`
CustomHeaders map[string]string `yaml:"custom_headers,omitempty"`
CustomFormat string `yaml:"custom_format,omitempty"`
CustomSprig string `yaml:"custom_sprig,omitempty"`
}
func New(options []*Options, ids []string) (*Provider, error) {
provider := &Provider{}
for _, o := range options {
if len(ids) == 0 || sliceutil.Contains(ids, o.ID) {
provider.Custom = append(provider.Custom, o)
}
}
provider.counter = 0
return provider, nil
}
func (p *Provider) Send(message, CliFormat string) error {
var CustomErr error
for _, pr := range p.Custom {
var msg string
if pr.CustomSprig != "" {
// Convert a string to JSON
data := make(map[string]interface{})
if err := json.Unmarshal([]byte(message), &data); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to unmarshal message to JSON for id: %s ", pr.ID))
}
funcMap := sprig.TxtFuncMap()
// Add custom functions if needed using funcMap["funcName"] = func
tmpl, err := template.New("sprig").Funcs(funcMap).Parse(pr.CustomSprig)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed to parse custom sprig template for id: %s ", pr.ID))
CustomErr = multierr.Append(CustomErr, err)
continue
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, data)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed to execute custom sprig template for id: %s: %s", pr.ID, data))
CustomErr = multierr.Append(CustomErr, err)
continue
}
msg = buf.String()
} else if strings.Contains(pr.CustomFormat, "{{dataJsonString}}") {
// Escape the message to a JSON string
b, err := json.Marshal(message)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to escape message to JSON for id: %s: %s", pr.ID, message))
}
dataJsonString := string(b)
// Replace the "{{dataJsonString}}" substring in the custom format with the escaped JSON string
msg = strings.ReplaceAll(pr.CustomFormat, "{{dataJsonString}}", dataJsonString)
} else {
// Otherwise, use the original message
msg = utils.FormatMessage(message, utils.SelectFormat(CliFormat, pr.CustomFormat), p.counter)
}
body := bytes.NewBufferString(msg)
r, err := http.NewRequest(pr.CustomMethod, pr.CustomWebhookURL, body)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed to send custom notification for id: %s: %s", pr.ID, msg))
CustomErr = multierr.Append(CustomErr, err)
continue
}
for k, v := range pr.CustomHeaders {
r.Header.Set(k, v)
}
_, err = httpreq.NewClient().Do(r)
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("failed to send custom notification for id: %s: %s", pr.ID, msg))
CustomErr = multierr.Append(CustomErr, err)
continue
}
gologger.Verbose().Msgf("custom notification sent for id: %s: %s", pr.ID, msg)
}
return CustomErr
}