forked from dimiro1/ipe
/
webhooks.go
172 lines (141 loc) · 5.09 KB
/
webhooks.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2014 Claudemiro Alves Feitosa Neto. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package ipe
import (
"bytes"
"encoding/json"
"net/http"
"time"
"github.com/dimiro1/ipe/utils"
log "github.com/golang/glog"
)
// A WebHook is sent as a HTTP POST request to the url which you specify.
// The POST request payload (body) contains a JSON document, and follows the following format:
// {
// "time_ms": 1327078148132
// "events": [
// { "name": "event_name", "some": "data" }
// ]
// }
//
// Security
// Encryption
//
// You may use a HTTP or a HTTPS url for WebHooks. In most cases HTTP is sufficient, but HTTPS can be useful if your data is sensitive or if you wish to protect against replay attacks for example.
// Authentication
//
// Since anyone could in principle send WebHooks to your application, it’s important to verify that these WebHooks originated from Pusher. Valid WebHooks will therefore contain these headers which contain a HMAC signature of the WebHook payload (body):
//
// X-Pusher-Key: The App Key.
// X-Pusher-Signature: A HMAC SHA256 hex digest formed by signing the POST payload (body) with the token’s secret.
type webHook struct {
TimeMs int64 `json:"time_ms"`
Events []hookEvent `json:"events"`
}
type hookEvent struct {
Name string `json:"name"`
Channel string `json:"channel"`
Event string `json:"event,omitempty"`
Data interface{} `json:"data,omitempty"`
SocketID string `json:"socket_id,omitempty"`
UserID string `json:"user_id,omitempty"`
}
func newChannelOcuppiedHook(channel *channel) hookEvent {
return hookEvent{Name: "channel_occupied", Channel: channel.ChannelID}
}
func newChannelVacatedHook(channel *channel) hookEvent {
return hookEvent{Name: "channel_vacated", Channel: channel.ChannelID}
}
func newMemberAddedHook(channel *channel, s *subscription) hookEvent {
return hookEvent{Name: "member_added", Channel: channel.ChannelID, UserID: s.ID}
}
func newMemberRemovedHook(channel *channel, s *subscription) hookEvent {
return hookEvent{Name: "member_removed", Channel: channel.ChannelID, UserID: s.ID}
}
func newClientHook(channel *channel, s *subscription, event string, data interface{}) hookEvent {
return hookEvent{Name: "client_event", Channel: channel.ChannelID, Event: event, Data: data, SocketID: s.Connection.SocketID}
}
// channel_occupied
// { "name": "channel_occupied", "channel": "test_channel" }
func (a *app) TriggerChannelOccupiedHook(c *channel) {
event := newChannelOcuppiedHook(c)
triggerHook(event.Name, a, c, event)
}
// channel_vacated
// { "name": "channel_vacated", "channel": "test_channel" }
func (a *app) TriggerChannelVacatedHook(c *channel) {
event := newChannelVacatedHook(c)
triggerHook(event.Name, a, c, event)
}
// {
// "name": "client_event",
// "channel": "name of the channel the event was published on",
// "event": "name of the event",
// "data": "data associated with the event",
// "socket_id": "socket_id of the sending socket",
// "user_id": "user_id associated with the sending socket" # Only for presence channels
// }
func (a *app) TriggerClientEventHook(c *channel, s *subscription, clientEvent string, data interface{}) {
event := newClientHook(c, s, clientEvent, data)
if c.IsPresence() {
event.UserID = s.ID
}
triggerHook(event.Name, a, c, event)
}
// {
// "name": "member_added",
// "channel": "presence-your_channel_name",
// "user_id": "a_user_id"
// }
func (a *app) TriggerMemberAddedHook(c *channel, s *subscription) {
event := newMemberAddedHook(c, s)
triggerHook(event.Name, a, c, event)
}
// {
// "name": "member_removed",
// "channel": "presence-your_channel_name",
// "user_id": "a_user_id"
// }
func (a *app) TriggerMemberRemovedHook(c *channel, s *subscription) {
event := newMemberRemovedHook(c, s)
triggerHook(event.Name, a, c, event)
}
func triggerHook(name string, a *app, c *channel, event hookEvent) {
if !a.WebHooks {
log.Infof("Webhooks are not enabled for app: %s", a.Name)
return
}
go func() {
log.Infof("Triggering %s event", name)
hook := webHook{TimeMs: time.Now().Unix()}
hook.Events = append(hook.Events, event)
var js []byte
var err error
js, err = json.Marshal(hook)
if err != nil {
log.Errorf("Error decoding json: %+v", err)
return
}
var req *http.Request
req, err = http.NewRequest("POST", a.URLWebHook, bytes.NewReader(js))
if err != nil {
log.Errorf("Error creating request: %+v", err)
return
}
req.Header.Set("User-Agent", "Ipe UA; (+https://github.com/dimiro1/ipe)")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Pusher-Key", a.Key)
req.Header.Set("X-Pusher-Signature", utils.HashMAC(js, []byte(a.Secret)))
log.V(1).Infof("%+v", req.Header)
log.V(1).Infof("%+v", string(js))
resp, err := http.DefaultClient.Do(req)
// See: http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/index.html#close_http_resp_body
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
log.Errorf("Error posting %s event: %+v", name, err)
}
}()
}