-
Notifications
You must be signed in to change notification settings - Fork 2
/
notify.go
67 lines (52 loc) · 1.2 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
package main
import (
"encoding/json"
"fmt"
"net"
model "github.com/nettica-com/nettica-admin/model"
log "github.com/sirupsen/logrus"
)
// Notify sends a message to 127.0.0.1:25264, the Nettica Agent app
func NotifyDNS(name string) {
var note model.AgentNotification
note.Type = "dns"
note.Text = name
bytes, err := json.Marshal(note)
if err != nil {
log.Errorf("Error marshalling notification: %v", err)
return
}
err = Notify(bytes)
if err != nil {
log.Errorf("Error sending notification: %v", err)
}
}
func NotifyInfo(message string) {
var note model.AgentNotification
note.Type = "info"
note.Text = message
bytes, err := json.Marshal(note)
if err != nil {
log.Errorf("Error marshalling notification: %v", err)
return
}
err = Notify(bytes)
if err != nil {
log.Errorf("Error sending notification: %v", err)
}
}
func Notify(message []byte) error {
raddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:25264")
if err != nil {
return err
}
conn, err := net.DialUDP("udp", nil, raddr)
if err != nil {
return err
}
defer conn.Close()
//conn.WriteMsgUDP(message, nil, raddr)
fmt.Fprint(conn, string(message))
log.Debugf("Notification: %v", string(message))
return nil
}