-
Notifications
You must be signed in to change notification settings - Fork 1
/
hub.go
806 lines (692 loc) · 19.8 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
package websub
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/tomnomnom/linkheader"
)
var (
// a non 2xx status code was returned when getting topic content
ErrNon2xxGettingContent = errors.New(
"a non 2xx status code was returned when getting topic content")
// a non 2xx status code was returned when posting content to a subscriber
ErrNon2xxPostingContent = errors.New(
"a non 2xx status code was returned when posting content to a subscriber")
)
// A Hub is a websub hub.
type Hub struct {
// See websub.HubAllowPostBodyAsContent
allowPostBodyAsContent bool
// expose topics to /topics
exposeTopics bool
// The hub url that specifies this hub.
hubURL string
// The user-agent to send on all http requests this hub makes.
userAgent string
// The hash function used to sign content distribution requests
//
// One of "sha1", "sha256", "sha384", or "sha512".
hashFunction string
// The number of times to retry failed POST requests to subscribers.
retryLimit int
// The amount of time to wait between each failed POST request to subscribers.
retryInterval time.Duration
// The max amount of time a subscription can be.
maxLease time.Duration
// The minimum amount of time a subscription can be.
minLease time.Duration
// The default amount of time a subscription is, if the subscriber doesn't specify.
defaultLease time.Duration
// The interval to clear expired subscriptions from the memory.
cleanupInterval time.Duration
// maps topic and callback to subscription, in that order.
subscriptions map[string]map[string]*HubSubscription
// Mutex lock for subscriptions map
mu *sync.RWMutex
// Validators that validate each incoming subscription request.
validators []*HubSubValidatorFunc
// Sniffers that intercept publishes as if they were subscribed to a topic.
sniffers map[string][]*HubTopicSnifferFunc
// All failed publishes are sent through this channel.
failedPublishes chan *hubPublish
// Newly found topics are sent through this channel
// Used for publishing topic updates when h.exposeTopics is true.
newTopic chan string
}
// represents a single POST request to make to a subscriber
type hubPublish struct {
// The HTTP callback to the subscriber
callback string
// The topic URL that was updated
topic string
// The secret to sign the message with. Empty string means none.
secret string
// The content that was published.
content []byte
// The Content-Type of the content
contentType string
// The number of times this POST request has failed.
failedCount int
}
// HubSubscription is a subscription used in the context of a Hub.
type HubSubscription struct {
// The HTTP callback to the subscriber.
Callback string
// The topic URL the subscriber is subscribing to.
Topic string
// The number of seconds the subscription will be active.
LeaseLength int
// The date/time the subscription will expire.
// Is not set at validation time.
Expires time.Time
// The secret provided by the subscriber. Empty string means none.
Secret string
}
func (h Hub) HubURL() string {
return h.hubURL
}
// NewHub creates a new hub with the specified options and starts background goroutines.
func NewHub(hubURL string, options ...HubOption) *Hub {
h := &Hub{
hubURL: strings.TrimRight(hubURL, "/"),
userAgent: "go-websub-hub",
hashFunction: "sha1",
retryInterval: time.Minute,
retryLimit: 5,
maxLease: time.Hour * 24 * 30,
minLease: time.Minute * 5,
defaultLease: time.Hour * 24 * 10,
cleanupInterval: time.Minute,
failedPublishes: make(chan *hubPublish),
newTopic: make(chan string),
subscriptions: make(map[string]map[string]*HubSubscription),
mu: &sync.RWMutex{},
sniffers: make(map[string][]*HubTopicSnifferFunc),
}
for _, opt := range options {
opt(h)
}
go h.handleFailedPublishes()
go h.removeExpiredSubscriptions(h.cleanupInterval)
if h.exposeTopics {
go h.publishTopicUpdates()
} else {
go h.consumeTopicUpdates()
}
return h
}
// A HubOption specifies an option for a hub.
type HubOption func(*Hub)
// HubWithCleanupInterval sets the interval expired subscriptions are removed from the memory
func HubWithCleanupInterval(interval time.Duration) HubOption {
return func(h *Hub) {
h.cleanupInterval = interval
}
}
// HubWithLeaseSettings sets the minimum, maximum, and default lease for a subscription on a hub
//
// When a requested lease is outside of the allowed range, the lease becomes pinned
// to the minimum or maximum value. If a subscriber doesn't provide a lease length,
// the default lease length is used.
//
// - Default minimum lease is 5 minutes
//
// - Default maximum lease is 720 hours (30 days)
//
// - Default default lease is 240 hours (10 days)
func HubWithLeaseSettings(minLease, maxLease, defaultLease time.Duration) HubOption {
return func(h *Hub) {
h.minLease = minLease
h.maxLease = maxLease
h.defaultLease = defaultLease
}
}
// HubExposeTopics enables a /topics endpoint that lists all available/active topics.
func HubExposeTopics(enable bool) HubOption {
return func(h *Hub) {
h.exposeTopics = enable
}
}
// HubAllowPostBodyAsContent allows publishers to post content as the body
// of the POST request if they provide hub.content = "body" and hub.mode = "publish".
// In this case, the Content-Type of the post request is used when distributing publish events.
//
// NOTE: Because of the lack of authentication for publishers, this allows
// any machine with internet access to the hub to publish any content
// under any topic. Use with caution.
func HubAllowPostBodyAsContent(enable bool) HubOption {
return func(h *Hub) {
h.allowPostBodyAsContent = enable
}
}
// HubWithUserAgent sets the user-agent for a hub
//
// Default user agent is "go-websub-hub"
func HubWithUserAgent(userAgent string) HubOption {
return func(h *Hub) {
h.userAgent = userAgent
}
}
// HubWithHashFunction sets the hash function used to compute hub signatures
// for subscriptions with a secret.
//
// One of "sha1", "sha256", "sha384", "sha512"
//
// Default is "sha1" for compatability, however it is insecure.
func HubWithHashFunction(hashFunction string) HubOption {
return func(h *Hub) {
h.hashFunction = hashFunction
}
}
// HubWithRetryLimits sets the retry limits for a hub.
//
// Defaults to 5 retries, and a one minute interval.
func HubWithRetryLimits(retryLimit int, retryInterval time.Duration) HubOption {
return func(h *Hub) {
h.retryLimit = retryLimit
h.retryInterval = retryInterval
}
}
// ServeHTTP handles incoming HTTP requests
func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/topics" && h.exposeTopics {
h.getTopicsHTTP(w, r)
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("Method not allowed"))
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "couldnt not read request body", http.StatusInternalServerError)
return
}
r.Body.Close()
var useBody = true
q := r.URL.Query()
if !h.allowPostBodyAsContent || q.Get("hub.content") != "body" {
if r.Header.Get("Content-Type") != "application/x-www-form-urlencoded" {
http.Error(w,
"invalid Content-Type; should be \"application/x-www-form-urlencoded\"", 400)
return
}
useBody = false
// combine query into q
q, err = url.ParseQuery(strings.TrimPrefix(string(body)+"&"+q.Encode(), "&"))
if err != nil {
http.Error(w, err.Error(), 400)
return
}
}
if q.Get("hub.mode") == "" {
w.WriteHeader(400)
w.Write([]byte("'hub.mode' missing or empty"))
return
}
switch q.Get("hub.mode") {
case "publish":
topic := q.Get("hub.topic")
if topic == "" {
topic = q.Get("hub.url")
}
if topic == "" {
w.WriteHeader(400)
w.Write([]byte("missing 'hub.topic' or 'hub.url'"))
return
}
var content []byte
var contentType string
if useBody {
content = body
contentType = r.Header.Get("Content-Type")
} else {
content, contentType, err = h.getTopicContent(topic)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
w.WriteHeader(200)
h.Publish(topic, contentType, content)
return
case "unsubscribe":
fallthrough
case "subscribe":
unsubscribe := q.Get("hub.mode") == "unsubscribe"
leaseLength := int(h.defaultLease.Seconds())
if q.Get("hub.lease_length") != "" {
leaseLength, err = strconv.Atoi(q.Get("hub.lease_seconds"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if leaseLength > int(h.maxLease.Seconds()) {
leaseLength = int(h.maxLease.Seconds())
}
if leaseLength < int(h.minLease.Seconds()) {
leaseLength = int(h.minLease.Seconds())
}
var sub *HubSubscription
update := false
topic := q.Get("hub.topic")
callback := q.Get("hub.callback")
h.mu.RLock()
cond := h.subscriptions[topic] != nil
h.mu.RUnlock()
if cond {
h.mu.Lock()
sub = h.subscriptions[topic][callback]
h.mu.Unlock()
}
if sub == nil && unsubscribe {
w.WriteHeader(404)
w.Write([]byte("subscription not found"))
return
}
if sub == nil {
sub = &HubSubscription{
Callback: callback,
Topic: topic,
Secret: q.Get("hub.secret"),
LeaseLength: leaseLength,
}
} else {
update = true
}
if sub.Callback == "" {
w.WriteHeader(400)
w.Write([]byte("'hub.callback' missing or empty"))
return
}
if sub.Topic == "" {
w.WriteHeader(400)
w.Write([]byte("'hub.topic' missing or empty"))
return
}
if len([]byte(sub.Secret)) > 200 {
w.WriteHeader(400)
w.Write([]byte("'hub.secret' must be less than 200 bytes"))
return
}
w.WriteHeader(202)
go func() {
if !unsubscribe {
ok, reason := h.validateSubscription(sub)
if !ok {
err = h.sendValidationDenied(sub, reason)
log.Err(err).Msg("could not deny subscription")
return
}
}
ok, err := h.verifyIntent(sub, "subscribe")
if err != nil {
log.Err(err).Msg("could not verify (un)subscription")
return
}
if ok {
if unsubscribe {
h.mu.Lock()
delete(h.subscriptions[sub.Topic], sub.Callback)
h.mu.Unlock()
} else {
sub.Expires = time.Now().Add(time.Duration(leaseLength) * time.Second)
if update {
sub.LeaseLength = leaseLength
sub.Secret = q.Get("hub.secret")
} else {
h.mu.RLock()
cond := h.subscriptions[sub.Topic] == nil
h.mu.RUnlock()
if cond {
h.mu.Lock()
h.subscriptions[sub.Topic] = make(map[string]*HubSubscription)
h.mu.Unlock()
h.newTopic <- sub.Topic
}
h.mu.Lock()
h.subscriptions[sub.Topic][sub.Callback] = sub
h.mu.Unlock()
}
}
}
}()
default:
w.WriteHeader(400)
w.Write([]byte("'hub.mode' not recognized"))
return
}
}
// HubSubValidatorFunc validates subscription requests.
//
// The validation stops as soon as one validator returns ok=false. The provided
// reason is sent to the subscriber telling them their subscription request was denied.
//
// The expiry date will not be set by the time the validators are called.
type HubSubValidatorFunc func(sub *HubSubscription) (ok bool, reason string)
// AddValidator adds a validator for subscription requests.
// Multiple validators can exist on one hub.
//
// All subscriptions are accepted by default.
func (h *Hub) AddValidator(validator HubSubValidatorFunc) {
h.validators = append(h.validators, &validator)
}
// checks all validators associated with the hub for the subscription.
//
// The validation stops as soon as one validator returns ok=false.
func (h *Hub) validateSubscription(sub *HubSubscription) (ok bool, reason string) {
for _, validator := range h.validators {
ok, reason := (*validator)(sub)
if !ok {
return ok, reason
}
}
return true, ""
}
// sends a GET request to the subscription callback, telling the subscriber they were denied.
func (h *Hub) sendValidationDenied(sub *HubSubscription, reason string) error {
callback, err := url.Parse(sub.Callback)
if err != nil {
return err
}
q := callback.Query()
q.Add("hub.mode", "denied")
q.Add("hub.topic", sub.Topic)
q.Add("hub.reason", reason)
callback.RawQuery = q.Encode()
req, err := http.NewRequest("GET", callback.String(), nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", h.userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
io.Copy(io.Discard, resp.Body)
return nil
}
// Sends a GET request to the subscription callback, verifying if the subscriber wants to
// subscribe or unsubscribe from a subscription.
func (h *Hub) verifyIntent(sub *HubSubscription, mode string) (ok bool, err error) {
callback, err := url.Parse(sub.Callback)
if err != nil {
return false, err
}
challenge := uuid.NewString()
q := callback.Query()
q.Add("hub.mode", mode)
q.Add("hub.topic", sub.Topic)
q.Add("hub.challenge", challenge)
q.Add("hub.lease_seconds", fmt.Sprint(sub.LeaseLength))
callback.RawQuery = q.Encode()
req, err := http.NewRequest("GET", callback.String(), nil)
if err != nil {
return false, err
}
req.Header.Set("User-Agent", h.userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return false, err
}
if string(body) != challenge {
return false, nil
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
// Non 2xx
return false, nil
}
return true, nil
}
// HubTopicSnifferFunc sniffs on topics as if it was a subscriber.
type HubTopicSnifferFunc func(topic string, contentType string, body io.Reader)
// AddSniffer allows one to "sniff" publishes, receiving events
// as if they were subscribers.
//
// If an emptry string is provided as the topic, all publishes are sniffed.
func (h *Hub) AddSniffer(topic string, sniffer HubTopicSnifferFunc) {
h.sniffers[topic] = append(h.sniffers[topic], &sniffer)
}
// Publish publishes a topic with the specified content and content-type.
func (h *Hub) Publish(topic, contentType string, content []byte) {
h.mu.RLock()
cond := h.subscriptions[topic] == nil
h.mu.RUnlock()
if cond {
h.mu.Lock()
h.subscriptions[topic] = make(map[string]*HubSubscription)
h.mu.Unlock()
h.newTopic <- topic
}
// call all sniffers for this topic, even if no subscriptions exist.
go func() {
for sniffedTopic, sniffers := range h.sniffers {
if sniffedTopic == "" || sniffedTopic == topic {
for _, sniffer := range sniffers {
(*sniffer)(topic, contentType, bytes.NewReader(content))
}
}
}
}()
h.mu.RLock()
for _, sub := range h.subscriptions[topic] {
if sub.Expires.After(time.Now()) {
go func(sub *HubSubscription) {
pub := &hubPublish{
callback: sub.Callback,
topic: topic,
secret: sub.Secret,
content: content,
contentType: contentType,
}
err := h.disbatchPublish(pub)
if err != nil {
log.Err(err).
Str("callback", pub.callback).
Str("topic", pub.topic).
Str("contentType", pub.contentType).
Msg("could not disbatch publish")
pub.failedCount++
h.failedPublishes <- pub
}
}(sub)
}
}
h.mu.RUnlock()
}
// disbatchPublish sends a publish request (POST) for the publish object.
func (h *Hub) disbatchPublish(pub *hubPublish) error {
req, err := http.NewRequest("POST", pub.callback, bytes.NewReader(pub.content))
if err != nil {
return err
}
req.Header.Set("Content-Type", pub.contentType)
req.Header.Set("Link", linkheader.Links{
{
Rel: "self",
URL: pub.topic,
},
{
Rel: "hub",
URL: h.hubURL,
},
}.String())
if pub.secret != "" {
req.Header.Set("X-Hub-Signature", h.newSignature(pub.content, pub.secret))
}
req.Header.Set("User-Agent", h.userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
io.Copy(io.Discard, resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return ErrNon2xxPostingContent
}
return nil
}
// newSignature generates an X-Hub-Signature header
func (h *Hub) newSignature(content []byte, secret string) string {
hash, hashName := calculateHash(h.hashFunction, secret, content)
return hashName + "=" + hash
}
// gets the topic content from the topic URL, via GET request.
func (h *Hub) getTopicContent(topic string) (content []byte, contentType string, err error) {
req, err := http.NewRequest("GET", topic, nil)
if err != nil {
return nil, "", err
}
req.Header.Set("User-Agent", h.userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, "", err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
// Non 2xx
return nil, "", ErrNon2xxGettingContent
}
bytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, "", err
}
return bytes, resp.Header.Get("Content-Type"), nil
}
// retry failed publishes based on retryInterval and retryLimit
func (h *Hub) handleFailedPublishes() {
for {
pub := <-h.failedPublishes
go func(pub *hubPublish) {
<-time.After(h.retryInterval)
err := h.disbatchPublish(pub)
if err != nil {
log.Err(err).
Str("callback", pub.callback).
Str("topic", pub.topic).
Str("contentType", pub.contentType).
Msg("could not disbatch publish")
pub.failedCount++
if h.retryLimit >= pub.failedCount {
h.failedPublishes <- pub
}
}
}(pub)
}
}
// periodically removes expired subscriptions from the memory.
func (h *Hub) removeExpiredSubscriptions(interval time.Duration) {
t := time.NewTicker(interval)
for {
<-t.C
var deleteMe []struct {
topic, callback string
}
h.mu.RLock()
for topic, subs := range h.subscriptions {
for callback, sub := range subs {
if !sub.Expires.After(time.Now()) {
deleteMe = append(deleteMe, struct {
topic, callback string
}{
topic: topic,
callback: callback,
})
}
}
}
h.mu.RUnlock()
if len(deleteMe) > 0 {
h.mu.Lock()
for _, v := range deleteMe {
delete(h.subscriptions[v.topic], v.callback)
}
h.mu.Unlock()
}
}
}
// GetTopics returns a sorted array of all topics.
//
// Includes topics with no subscribers, or no publishes.
func (h *Hub) GetTopics() (topics []string) {
h.mu.RLock()
topics = make([]string, 0, len(h.subscriptions))
for topic := range h.subscriptions {
topics = append(topics, topic)
}
h.mu.RUnlock()
sort.Strings(topics)
return
}
// Handles requests to /topics when h.exposeTopics is enabled.
func (h *Hub) getTopicsHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
bytes, err := json.Marshal(HubTopicUpdates{
AllTopics: h.GetTopics(),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Link", linkheader.Links{
{
Rel: "self",
URL: h.hubURL + "/topics",
},
{
Rel: "hub",
URL: h.hubURL + "/",
}}.String())
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(bytes)
}
// HubTopicUpdates is the JSON structure that is exposed to /topics when h.exposeTopics is true
type HubTopicUpdates struct {
// All topics currently known
AllTopics []string `json:"allTopics"`
// Only on websub events: the new topic that was added
NewTopic string `json:"newTopic,omitempty"`
}
func (h *Hub) publishTopicUpdates() {
for topic := range h.newTopic {
bytes, err := json.Marshal(HubTopicUpdates{
AllTopics: h.GetTopics(),
NewTopic: topic,
})
if err != nil {
log.Err(err).Msg("could not marshal topic json on publish")
return
}
// Must be run in goroutine, otherwise there is deadlock
// on the first publish to this topic because h.newTopic
// is unbuffered and h.Publish sends a new message
go h.Publish(h.hubURL+"/topics", "application/json", bytes)
}
}
func (h *Hub) consumeTopicUpdates() {
for range h.newTopic {
// do nothing
}
}