-
Notifications
You must be signed in to change notification settings - Fork 1
/
hub.go
542 lines (432 loc) · 11.7 KB
/
hub.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package websub
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/jpillora/backoff"
"github.com/mitchellh/mapstructure"
"hash"
"io"
"log"
"meow.tf/websub/handler"
"meow.tf/websub/model"
"meow.tf/websub/store"
"net/http"
"net/url"
"reflect"
"runtime"
"strconv"
"strings"
"time"
)
// Validator is a function to validate a subscription request.
// If error is not nil, hub.mode=verify will be called with the error.
type Validator func(model.Subscription) error
// ContentProvider is a function to extract content out of the specific content topic.
type ContentProvider func(topic string) ([]byte, string, error)
// Option represents a Hub option.
type Option func(h *Hub)
// Hub represents a WebSub hub.
type Hub struct {
*handler.Handler
client *http.Client
store store.Store
validator Validator
contentProvider ContentProvider
worker Worker
hasher string
url string
maxLease time.Duration
}
var (
v = validator.New()
)
// WithValidator sets the subscription validator.
func WithValidator(validator Validator) Option {
return func(h *Hub) {
h.validator = validator
}
}
// WithContentProvider sets the content provider for external hub.mode=publish requests.
func WithContentProvider(provider ContentProvider) Option {
return func(h *Hub) {
h.contentProvider = provider
}
}
// WithHasher lets you set other hmac hashers/types (like sha256, sha384, sha512, etc)
func WithHasher(hasher string) Option {
return func(h *Hub) {
h.hasher = hasher
}
}
// WithWorker lets you set the worker used to distribute subscription responses.
// This can be done with any number of systems, such as Amazon SQS, Beanstalk, etc.
func WithWorker(worker Worker) Option {
return func(h *Hub) {
h.worker = worker
}
}
// WithURL lets you set the hub url.
// By default, this is auto detected on first request for ease of use.
func WithURL(url string) Option {
return func(h *Hub) {
h.url = url
}
}
// WithMaxLease lets you set the hub's max lease time.
// By default, this is 24 hours.
func WithMaxLease(maxLease time.Duration) Option {
return func(h *Hub) {
h.maxLease = maxLease
}
}
// New creates a new WebSub Hub instance.
// store is required to store all of the subscriptions.
func New(store store.Store, opts ...Option) *Hub {
h := &Hub{
Handler: handler.New(),
client: &http.Client{
Timeout: 30 * time.Second,
},
store: store,
contentProvider: HttpContent,
hasher: "sha256",
maxLease: 24 * time.Hour,
}
for _, opt := range opts {
opt(h)
}
if h.worker == nil {
h.worker = NewGoWorker(h, runtime.NumCPU())
h.worker.Start()
}
return h
}
// ServeHTTP is a generic webserver handler for websub.
// It takes in "hub.mode" from the form, and passes it to the appropriate handlers.
func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
hubMode := r.FormValue("hub.mode")
if hubMode == "" {
http.Error(w, "missing hub.mode parameter", http.StatusBadRequest)
return
}
// If url is not set, set to something we can "guess"
if h.url == "" {
proto := "http"
// Usually X-Forwarded cannot be trusted, but in this case it's the first request that defines it.
// For our case, this simply sets the hub url via "auto detection".
// it is STRONGLY advised to set the url using WithURL beforehand.
if r.Header.Get("X-Forwarded-Proto") == "https" {
proto = r.Header.Get("X-Forwarded-Proto")
}
u := &url.URL{
Scheme: proto,
Host: r.Host,
Path: r.RequestURI,
}
h.url = strings.TrimRight(u.String(), "/")
}
switch hubMode {
case model.ModeSubscribe:
var req model.SubscribeRequest
if err := DecodeForm(r, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err := h.HandleSubscribe(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusAccepted)
case model.ModeUnsubscribe:
var req model.UnsubscribeRequest
if err := DecodeForm(r, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err := h.HandleUnsubscribe(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusAccepted)
case model.ModePublish:
var req model.PublishRequest
if err := DecodeForm(r, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err := h.HandlePublish(req)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.WriteHeader(http.StatusAccepted)
default:
http.Error(w, "hub.mode not recognized", http.StatusBadRequest)
}
}
// HandleSubscribe handles a hub.mode=subscribe request.
func (h *Hub) HandleSubscribe(req model.SubscribeRequest) error {
// validate for required fields
if err := v.Struct(req); err != nil {
return err
}
// Default lease
leaseDuration := 240 * time.Hour
if req.LeaseSeconds > 0 {
if req.LeaseSeconds < 60 || time.Duration(req.LeaseSeconds)*time.Second > h.maxLease {
return errors.New("invalid hub.lease_seconds value")
} else {
leaseDuration = time.Duration(req.LeaseSeconds) * time.Second
}
}
sub := model.Subscription{
Topic: req.Topic,
Callback: req.Callback,
Secret: req.Secret,
Expires: time.Now().Add(leaseDuration),
}
if h.validator != nil {
err := h.validator(sub)
if err != nil {
sub.Reason = err
return h.Verify(model.ModeDenied, sub)
}
}
existingSub, err := h.store.Get(req.Topic, req.Callback)
if existingSub != nil && err == nil {
// Update existingSub instead.
// TODO: Can Secret be updated?
sub = *existingSub
sub.Expires = time.Now().Add(leaseDuration)
}
go func(hubMode string, sub model.Subscription) {
err := h.Verify(hubMode, sub)
if err != nil {
h.Call(&VerificationFailed{
Subscription: sub,
Error: err,
})
} else {
h.Call(&Verified{
Subscription: sub,
})
}
}(req.Mode, sub)
return nil
}
// HandleUnsubscribe handles a hub.mode=unsubscribe
func (h *Hub) HandleUnsubscribe(req model.UnsubscribeRequest) error {
// validate for required fields
if err := v.Struct(req); err != nil {
return err
}
sub := model.Subscription{
Topic: req.Topic,
Callback: req.Callback,
}
if h.validator != nil {
err := h.validator(sub)
if err != nil {
sub.Reason = err
return h.Verify(model.ModeDenied, sub)
}
}
go func(hubMode string, sub model.Subscription) {
err := h.Verify(hubMode, sub)
if err != nil {
log.Println("Error:", err)
}
}(req.Mode, sub)
return nil
}
// Verify sends a response to a subscription model with the specified data.
// If the subscription failed, Reason can be set to send hub.reason in the callback.
func (h *Hub) Verify(mode string, sub model.Subscription) error {
u, err := url.Parse(sub.Callback)
if err != nil {
return err
}
challenge := uuid.New().String()
q := u.Query()
q.Set("hub.mode", mode)
q.Set("hub.topic", sub.Topic)
if mode != model.ModeDenied {
q.Set("hub.challenge", challenge)
q.Set("hub.lease_seconds", strconv.Itoa(int(sub.LeaseTime/time.Second)))
} else if sub.Reason != nil {
q.Set("hub.reason", sub.Reason.Error())
}
u.RawQuery = q.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", "Go WebSub 1.0 ("+runtime.Version()+")")
res, err := h.client.Do(req)
if err != nil {
return err
}
if res.StatusCode != 200 {
// Uh oh!
return errors.New("unexpected status code")
}
defer res.Body.Close()
if mode == model.ModeDenied {
io.Copy(io.Discard, res.Body)
return nil
}
// Read max of challenge size bytes
data := make([]byte, len(challenge))
read, err := io.ReadFull(res.Body, data)
if err != nil && err != io.ErrUnexpectedEOF {
return err
}
data = data[0:read]
if string(data) != challenge {
// Nope.
return errors.New(fmt.Sprint("verification: challenge did not match for "+u.Host+", expected: ", challenge, " actual: ", string(data)))
}
if mode == model.ModeSubscribe {
// Update the subscription and set it as verified
// time.Now().Add(time.Duration(leaseSeconds) * time.Second), topic, callback
err = h.store.Add(sub)
} else if mode == model.ModeUnsubscribe {
// Delete the subscription
err = h.store.Remove(sub)
}
return err
}
// HandlePublish handles a request to publish from a publisher.
func (h *Hub) HandlePublish(req model.PublishRequest) error {
if err := v.Struct(req); err != nil {
return err
}
data, contentType, err := h.contentProvider(req.Topic)
if err != nil {
return err
}
return h.Publish(req.Topic, contentType, data)
}
// Publish queues responses to the worker for a publish.
func (h *Hub) Publish(topic, contentType string, data []byte) error {
subs, err := h.store.All(topic)
if err != nil {
return err
}
h.Call(&Publish{
Topic: topic,
ContentType: contentType,
Data: data,
})
hub := model.Hub{
Hasher: h.hasher,
URL: h.url,
}
for _, sub := range subs {
h.worker.Add(PublishJob{
Hub: hub,
Subscription: sub,
ContentType: contentType,
Data: data,
})
}
return nil
}
// callCallback sends a request to the specified URL with the publish data.
func (h *Hub) callCallback(job PublishJob) bool {
req, err := http.NewRequest("POST", job.Subscription.Callback, bytes.NewReader(job.Data))
if err != nil {
return false
}
if job.Subscription.Secret != "" {
mac := hmac.New(NewHasher(h.hasher), []byte(job.Subscription.Secret))
mac.Write(job.Data)
req.Header.Set("X-Hub-Signature", h.hasher+"="+hex.EncodeToString(mac.Sum(nil)))
}
req.Header.Set("Content-Type", job.ContentType)
req.Header.Set("Link", fmt.Sprintf("<%s>; rel=\"hub\", <%s>; rel=\"self\"", h.url, job.Subscription.Topic))
b := &backoff.Backoff{
Min: 100 * time.Millisecond,
Max: 10 * time.Minute,
Factor: 2,
Jitter: false,
}
var attempts int
for {
res, err := h.client.Do(req)
if err == nil {
res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode <= 299 {
return true
} else if res.StatusCode == http.StatusGone {
h.store.Remove(job.Subscription)
return false
}
}
attempts++
if attempts >= 3 {
break
}
<-time.After(b.Duration())
}
return false
}
// NewHasher takes a string and returns a hash.Hash based on type.
func NewHasher(hasher string) func() hash.Hash {
switch hasher {
case "sha1":
return sha1.New
case "sha256":
return sha256.New
case "sha384":
return sha512.New384
case "sha512":
return sha512.New
}
panic("Invalid hasher type supplied")
}
// DecodeForm decodes a request form into a struct using the mapstructure package.
func DecodeForm(r *http.Request, dest interface{}) error {
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "form",
Result: dest,
// This hook is a trick to allow us to map from []string -> string in the case of elements.
// This is only required because we're mapping from r.Form -> struct.
DecodeHook: func(from reflect.Kind, to reflect.Kind, v interface{}) (interface{}, error) {
if from == reflect.Slice && (to == reflect.String || to == reflect.Int) {
switch s := v.(type) {
case []string:
if len(s) < 1 {
return "", nil
}
// Switch statement seems wasteful here, but if we want to add uint/etc we can easily.
switch to {
case reflect.Int:
return strconv.Atoi(s[0])
}
return s[0], nil
}
return v, nil
}
return v, nil
},
})
if err != nil {
return err
}
return decoder.Decode(r.Form)
}