-
Notifications
You must be signed in to change notification settings - Fork 71
/
notify.go
89 lines (81 loc) · 2.49 KB
/
notify.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/securitybunker/databunker/src/autocontext"
)
func notifyBadLogin(notifyURL string, mode string, identity string) {
if len(notifyURL) == 0 {
return
}
requestBody := fmt.Sprintf(`{"action":"%s","identity":"%s","mode":"%s"}`,
"badlogin", identity, mode)
host := autocontext.GetAuto("host")
go notify(notifyURL, host, []byte(requestBody))
}
func notifyProfileNew(notifyURL string, profile []byte, mode string, identity string) {
if len(notifyURL) == 0 {
return
}
requestBody := fmt.Sprintf(`{"action":"%s","identity":"%s","mode":"%s","profile":%s}`,
"profilenew", identity, mode, profile)
host := autocontext.GetAuto("host")
go notify(notifyURL, host, []byte(requestBody))
}
func notifyProfileChange(notifyURL string, old []byte, profile []byte, mode string, identity string) {
if len(notifyURL) == 0 {
return
}
requestBody := fmt.Sprintf(`{"action":"%s","identity":"%s","mode":"%s","old":%s,"profile":%s}`,
"profilechange", identity, mode, old, profile)
host := autocontext.GetAuto("host")
go notify(notifyURL, host, []byte(requestBody))
}
func notifyForgetMe(notifyURL string, profile []byte, mode string, identity string) {
if len(notifyURL) == 0 {
return
}
requestBody := fmt.Sprintf(`{"action":"%s","identity":"%s","mode":"%s","profile":%s}`,
"forgetme", identity, mode, profile)
host := autocontext.GetAuto("host")
go notify(notifyURL, host, []byte(requestBody))
}
func notifyConsentChange(notifyURL string, brief string, status string, mode string, identity string) {
if len(notifyURL) == 0 {
return
}
requestBody, _ := json.Marshal(map[string]string{
"action": "consentchange",
"brief": brief,
"status": status,
"mode": mode,
"identity": identity,
})
host := autocontext.GetAuto("host")
go notify(notifyURL, host, requestBody)
}
func notify(notifyURL string, host interface{}, requestBody []byte) {
req, _ := http.NewRequest("POST", notifyURL, bytes.NewBuffer(requestBody))
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
if host != nil {
req.Header.Add("Original-Host", host.(string))
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("error in notify: %s", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("error in body read: %s", err)
return
}
log.Printf("Notification result: %s", string(body))
}