-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathwebhook.go
More file actions
321 lines (300 loc) · 8.33 KB
/
webhook.go
File metadata and controls
321 lines (300 loc) · 8.33 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright 2025 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package webhook
import (
"bytes"
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
"github.com/blinklabs-io/adder/event"
"github.com/blinklabs-io/adder/internal/logging"
"github.com/blinklabs-io/adder/internal/version"
"github.com/blinklabs-io/adder/plugin"
)
const (
mainnetNetworkMagic uint32 = 764824073
previewNetworkMagic uint32 = 2
preprodNetworkMagic uint32 = 1
)
type WebhookOutput struct {
errorChan chan<- error
eventChan chan event.Event
logger plugin.Logger
format string
url string
username string
password string
skipVerify bool
}
func New(options ...WebhookOptionFunc) *WebhookOutput {
w := &WebhookOutput{
eventChan: make(chan event.Event, 10),
format: "adder",
url: "http://localhost:3000",
skipVerify: false,
}
for _, option := range options {
option(w)
}
return w
}
// Start the webhook output
func (w *WebhookOutput) Start() error {
logger := logging.GetLogger()
logger.Info("starting webhook server")
go func() {
for {
evt, ok := <-w.eventChan
// Channel has been closed, which means we're shutting down
if !ok {
return
}
payload := evt.Payload
if payload == nil {
panic(fmt.Errorf("ERROR: %v", payload))
}
context := evt.Context
switch evt.Type {
case "chainsync.block":
if context == nil {
panic(fmt.Errorf("ERROR: %v", context))
}
be := payload.(event.BlockEvent)
bc := context.(event.BlockContext)
evt.Payload = be
evt.Context = bc
case "chainsync.rollback":
re := payload.(event.RollbackEvent)
evt.Payload = re
case "chainsync.transaction":
te := payload.(event.TransactionEvent)
evt.Payload = te
default:
logger.Error("unknown event type: " + evt.Type)
return
}
// TODO: error handle (#334)
err := w.SendWebhook(&evt)
if err != nil {
logger.Error(fmt.Sprintf("ERROR: %s", err))
}
}
}()
return nil
}
func basicAuth(username, password string) string {
auth := username + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
}
func formatWebhook(e *event.Event, format string) []byte {
var data []byte
var err error
switch format {
case "discord":
var dwe DiscordWebhookEvent
var dme DiscordMessageEmbed
var dmes []*DiscordMessageEmbed
var dmefs []*DiscordMessageEmbedField
switch e.Type {
case "chainsync.block":
be := e.Payload.(event.BlockEvent)
bc := e.Context.(event.BlockContext)
dme.Title = "New Cardano Block"
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Block Number",
Value: strconv.FormatUint(bc.BlockNumber, 10),
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Slot Number",
Value: strconv.FormatUint(bc.SlotNumber, 10),
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Block Hash",
Value: be.BlockHash,
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Issuer Vkey",
Value: be.IssuerVkey,
})
baseURL := getBaseURL(bc.NetworkMagic)
dme.URL = fmt.Sprintf("%s/block/%s", baseURL, be.BlockHash)
case "chainsync.rollback":
be := e.Payload.(event.RollbackEvent)
dme.Title = "Cardano Rollback"
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Slot Number",
Value: strconv.FormatUint(be.SlotNumber, 10),
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Block Hash",
Value: be.BlockHash,
})
case "chainsync.transaction":
te := e.Payload.(event.TransactionEvent)
tc := e.Context.(event.TransactionContext)
dme.Title = "New Cardano Transaction"
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Block Number",
Value: strconv.FormatUint(tc.BlockNumber, 10),
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Slot Number",
Value: strconv.FormatUint(tc.SlotNumber, 10),
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Inputs",
Value: strconv.Itoa(len(te.Inputs)),
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Outputs",
Value: strconv.Itoa(len(te.Outputs)),
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Fee",
Value: strconv.FormatUint(te.Fee, 10),
})
dmefs = append(dmefs, &DiscordMessageEmbedField{
Name: "Transaction Hash",
Value: tc.TransactionHash,
})
baseURL := getBaseURL(tc.NetworkMagic)
dme.URL = fmt.Sprintf("%s/tx/%s", baseURL, tc.TransactionHash)
default:
dwe.Content = fmt.Sprintf("%v", e.Payload)
}
dme.Fields = dmefs
dmes = append(dmes, &dme)
dwe.Embeds = dmes
data, err = json.Marshal(dwe)
if err != nil {
return data
}
default:
data, err = json.Marshal(e)
if err != nil {
return data
}
}
return data
}
type DiscordWebhookEvent struct {
Content string `json:"content,omitempty"`
Embeds []*DiscordMessageEmbed `json:"embeds,omitempty"`
}
type DiscordMessageEmbed struct {
URL string `json:"url,omitempty"`
Title string `json:"title,omitempty"`
Fields []*DiscordMessageEmbedField `json:"fields,omitempty"`
}
type DiscordMessageEmbedField struct {
Name string `json:"name"`
Value string `json:"value"`
}
func getBaseURL(networkMagic uint32) string {
switch networkMagic {
case mainnetNetworkMagic:
return "https://cexplorer.io"
case preprodNetworkMagic:
return "https://preprod.cexplorer.io"
case previewNetworkMagic:
return "https://preview.cexplorer.io"
default:
return "https://cexplorer.io" // default to mainnet if unknown network
}
}
func (w *WebhookOutput) SendWebhook(e *event.Event) error {
logger := logging.GetLogger()
logger.Info(fmt.Sprintf("sending event %s to %s", e.Type, w.url))
data := formatWebhook(e, w.format)
// Setup request
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
w.url,
bytes.NewReader(data),
)
if err != nil {
return fmt.Errorf("%w", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add(
"User-Agent",
"Adder/"+version.GetVersionString(),
)
// Setup authorization
if w.username != "" && w.password != "" {
req.Header.Add("Authorization", basicAuth(w.username, w.password))
}
// Setup custom transport to allow self-signed SSL
defaultTransport := http.DefaultTransport.(*http.Transport)
// #nosec G402
customTransport := &http.Transport{
Proxy: defaultTransport.Proxy,
DialContext: defaultTransport.DialContext,
MaxIdleConns: defaultTransport.MaxIdleConns,
IdleConnTimeout: defaultTransport.IdleConnTimeout,
ExpectContinueTimeout: defaultTransport.ExpectContinueTimeout,
TLSHandshakeTimeout: defaultTransport.TLSHandshakeTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: w.skipVerify},
}
client := &http.Client{Transport: customTransport}
// Send payload
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("%w", err)
}
if resp == nil {
return fmt.Errorf("failed to send payload: %s", data)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("%w", err)
}
defer resp.Body.Close()
logger.Info(
fmt.Sprintf("sent: %s, payload: %s, body: %s, response: %s, status: %d",
w.url,
string(data),
string(respBody),
resp.Status,
resp.StatusCode,
),
)
return nil
}
// Stop the embedded output
func (w *WebhookOutput) Stop() error {
close(w.eventChan)
return nil
}
// SetErrorChan sets the error channel
func (w *WebhookOutput) SetErrorChan(ch chan<- error) {
w.errorChan = ch
}
// InputChan returns the input event channel
func (w *WebhookOutput) InputChan() chan<- event.Event {
return w.eventChan
}
// OutputChan always returns nil
func (w *WebhookOutput) OutputChan() <-chan event.Event {
return nil
}