-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
webhook.go
143 lines (121 loc) · 2.67 KB
/
webhook.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package lib
import (
"bytes"
"io"
"net/http"
"strings"
"sync"
"text/template"
"github.com/TimothyYe/godns/internal/settings"
"github.com/TimothyYe/godns/internal/utils"
log "github.com/sirupsen/logrus"
)
type Webhook struct {
conf *settings.Settings
client *http.Client
}
var (
instance *Webhook
once sync.Once
)
func GetWebhook(conf *settings.Settings) *Webhook {
once.Do(func() {
instance = &Webhook{
conf: conf,
client: utils.GetHTTPClient(conf),
}
})
return instance
}
func (w *Webhook) Execute(domain, currentIP string) error {
if w.conf.Webhook.URL == "" {
log.Debug("Webhook URL is empty, skip sending notification")
return nil
}
// set request method
method := http.MethodGet
if w.conf.Webhook.RequestBody != "" {
method = http.MethodPost
}
reqURL, reqBody := "", ""
var err error
// send HTTP get request
if method == http.MethodGet {
reqURL, err = w.buildReqURL(domain, currentIP, w.conf.IPType)
if err != nil {
return err
}
} else {
reqURL = w.conf.Webhook.URL
reqBody, err = w.buildReqBody(domain, currentIP, w.conf.IPType)
if err != nil {
return err
}
}
var req *http.Request
req, err = http.NewRequest(method, reqURL, strings.NewReader(reqBody))
if err != nil {
log.Error("Failed to create request:", err)
return err
}
if method == http.MethodPost {
req.Header.Add("Content-Type", "application/json")
}
resp, err := w.client.Do(req)
if err != nil {
log.Error("Failed to send request:", err)
return err
}
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("Failed to read response body:", err)
return err
}
log.Debugf("Webhook response: %s", string(content))
return nil
}
func (w *Webhook) buildReqURL(domain, currentIP, ipType string) (string, error) {
t := template.New("req template")
if _, err := t.Parse(w.conf.Webhook.URL); err != nil {
log.Error("Failed to parse template:", err)
return "", err
}
data := struct {
CurrentIP string
Domain string
IPType string
}{
currentIP,
domain,
ipType,
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, data); err != nil {
log.Error(err)
return "", err
}
return tpl.String(), nil
}
func (w *Webhook) buildReqBody(domain, currentIP, ipType string) (string, error) {
t := template.New("reqBody template")
if _, err := t.Parse(w.conf.Webhook.RequestBody); err != nil {
log.Error("Failed to parse template:", err)
return "", err
}
data := struct {
CurrentIP string
Domain string
IPType string
}{
currentIP,
domain,
ipType,
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, data); err != nil {
log.Error(err)
return "", err
}
return tpl.String(), nil
}