This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.go
393 lines (329 loc) · 10.5 KB
/
backend.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
package main
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1" // #nosec G505 - As this is only obfuscation this is acceptable
"crypto/x509"
"fmt"
"net/url"
"strings"
"text/template"
"time"
"github.com/SlyMarbo/rss"
"github.com/boltdb/bolt"
ostatus "github.com/emersion/go-ostatus"
"github.com/emersion/go-ostatus/activitystream"
"github.com/emersion/go-ostatus/pubsubhubbub"
"github.com/emersion/go-ostatus/salmon"
"github.com/emersion/go-ostatus/xrd"
"github.com/emersion/go-ostatus/xrd/lrdd"
"github.com/emersion/go-ostatus/xrd/webfinger"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
const keyBits = 2048
var (
keysBucket = []byte("RSAKeys")
dateBucket = []byte("FeedLastUpdate")
)
type subscription struct {
ticker *time.Ticker
notifies chan<- pubsubhubbub.Event
}
type backend struct {
salmon.PublicKeyBackend
baseURL string
db *bolt.DB
domain string
feeds *feedDB
topics map[string]*subscription
}
func newBackend(db *bolt.DB, baseURL string, feeds *feedDB) (*backend, error) {
u, err := url.Parse(baseURL)
if err != nil {
return nil, errors.Wrap(err, "Unable to parse base-URL")
}
return &backend{
PublicKeyBackend: salmon.NewPublicKeyBackend(),
baseURL: baseURL,
db: db,
domain: u.Host,
feeds: feeds,
topics: make(map[string]*subscription),
}, nil
}
func (b *backend) Feed(topicURL string) (*activitystream.Feed, error) {
feedName := b.uriToFeedName(topicURL)
if feedName == "" {
log.WithField("topic", topicURL).Warn("Tried to fetch invalid topic")
return nil, errors.New("Invalid topic")
}
items := b.feeds.entriesByFeedName(feedName)
if items == nil {
log.WithField("feed_name", feedName).Warn("Tried to fetch unknown feed")
return nil, errors.New("Unknown feed")
}
feed, err := b.rssItemsToFeed(feedName, items)
if err != nil {
log.WithField("feed_name", feedName).WithError(err).Error("Unable to generate feed")
}
return feed, err
}
func (b backend) getHostMeta() *xrd.Resource {
return &xrd.Resource{
Links: []*xrd.Link{
{Rel: lrdd.Rel, Type: "application/jrd+json", Template: b.baseURL + webfinger.WellKnownPathTemplate},
},
}
}
func (b backend) getFeedEnvelope(feedName string) *activitystream.Feed {
feedURL := fmt.Sprintf("%s/@%s.atom", b.baseURL, feedName)
acctURL := fmt.Sprintf("acct:%s@%s", feedName, b.domain)
var lastUpdate = time.Now()
if items := b.feeds.entriesByFeedName(feedName); len(items) > 0 {
lastUpdate = items[0].Date
}
fi := b.feeds.infoByFeedName(feedName)
return &activitystream.Feed{
ID: feedURL,
Title: feedName,
Logo: cfg.AvatarURL,
Subtitle: fmt.Sprintf("rss-status feed fetcher for %q feed", feedName),
Updated: activitystream.NewTime(lastUpdate),
Link: []activitystream.Link{
{Rel: "alternate", Type: "text/html", Href: fi.ProfileURL},
{Rel: "self", Type: "application/atom+xml", Href: feedURL},
{Rel: pubsubhubbub.RelHub, Href: b.baseURL + ostatus.HubPath},
{Rel: salmon.Rel, Href: b.baseURL + ostatus.SalmonPath},
},
Author: &activitystream.Person{
ID: acctURL,
URI: acctURL,
Name: feedName,
Email: fmt.Sprintf("%s@%s", feedName, b.domain),
Summary: fmt.Sprintf("rss-status feed fetcher for %q feed", feedName),
ObjectType: activitystream.ObjectPerson,
Link: []activitystream.Link{
{Rel: "alternate", Type: "text/html", Href: fi.ProfileURL},
{Rel: "avatar", Href: cfg.AvatarURL},
},
PreferredUsername: feedName,
DisplayName: fi.DisplayName,
Note: fmt.Sprintf("rss-status feed fetcher for %q feed", feedName),
},
}
}
func (b backend) getFeedKey(feedName string) (crypto.PublicKey, error) {
var pub crypto.PublicKey
err := b.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(keysBucket)
if err != nil {
return err
}
k := []byte(feedName)
v := b.Get(k)
var priv *rsa.PrivateKey
if v == nil {
priv, err = rsa.GenerateKey(rand.Reader, keyBits)
if err != nil {
return err
}
v = x509.MarshalPKCS1PrivateKey(priv)
if err = b.Put(k, v); err != nil {
return err
}
} else {
priv, err = x509.ParsePKCS1PrivateKey(v)
if err != nil {
return err
}
}
pub = priv.Public()
return nil
})
return pub, err
}
func (b backend) getFeedLastUpdate(feedName string) (time.Time, error) {
t := time.Now() // Fallback: Assume last update was now
err := b.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket(dateBucket)
if b == nil {
return nil // The bucket is not yet there, just use current time
}
var err error
k := []byte(feedName)
v := b.Get(k)
if v != nil {
t, err = time.Parse(time.RFC3339Nano, string(v))
}
return err
})
return t, err
}
func (b *backend) Notify(entry *activitystream.Entry) error {
log.WithFields(log.Fields{
"object_type": entry.ObjectType,
"verb": entry.Verb,
}).Debug("Received event notification")
if entry.ObjectType != activitystream.ObjectActivity {
return errors.New("Unsupported object type")
}
switch entry.Verb {
case activitystream.VerbFollow, activitystream.VerbUnfollow:
return nil // Nothing to do
default:
return errors.New("Unsupported verb")
}
}
// obfuscateFeedEntryID is to create url-safe obfuscations of the real feed
// IDs while maintaining low-propability for collisions
func (b *backend) obfuscateFeedEntryID(id string) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(id))) // #nosec G401 - As this is only obfuscation this is acceptable
}
func (b *backend) Resource(uri string, rel []string) (*xrd.Resource, error) {
feedName := b.uriToFeedName(uri)
if feedName == "" {
log.WithField("topic", uri).Warn("Tried to fetch invalid topic")
return nil, errors.New("Invalid topic")
}
pub, err := b.getFeedKey(feedName)
if err != nil {
log.WithField("feed_name", feedName).WithError(err).Error("Unable to get / generate public key")
return nil, errors.Wrap(err, "Unable to get / generate public key")
}
publicKeyURL, err := salmon.FormatPublicKeyDataURL(pub)
if err != nil {
log.WithField("feed_name", feedName).WithError(err).Error("Unable to create public key data URL")
return nil, errors.Wrap(err, "Unable to create public key data URL")
}
accountURI := fmt.Sprintf("acct:%s@%s", feedName, b.domain)
resource := &xrd.Resource{
Subject: accountURI,
Links: []*xrd.Link{
{Rel: webfinger.RelProfilePage, Type: "text/html", Href: fmt.Sprintf("%s/@%s.atom", b.baseURL, feedName)},
{Rel: pubsubhubbub.RelUpdatesFrom, Type: "application/atom+xml", Href: fmt.Sprintf("%s/@%s.atom", b.baseURL, feedName)},
{Rel: salmon.Rel, Href: b.baseURL + ostatus.SalmonPath},
{Rel: salmon.RelMagicPublicKey, Href: publicKeyURL},
},
}
return resource, nil
}
func (b backend) rssItemsToFeed(feedName string, items []*rss.Item) (*activitystream.Feed, error) {
feed := b.getFeedEnvelope(feedName)
tpl, err := template.New("feedFormat").Parse(`<p>{{ if .Link }}<a href="{{ .Link }}">{{ .Title }}</a>{{ else }}{{ .Title }}{{ end }}</p>{{ .Summary }}`)
if err != nil {
return nil, errors.Wrap(err, "Unable to parse entry template")
}
for _, i := range items {
content := new(bytes.Buffer)
if err := tpl.Execute(content, i); err != nil {
return nil, errors.Wrap(err, "Unable to execute template")
}
entry := &activitystream.Entry{
ID: fmt.Sprintf("%s/status/%s/%x", b.baseURL, feedName, b.obfuscateFeedEntryID(i.ID)),
Title: "Post",
ObjectType: activitystream.ObjectNote,
Verb: activitystream.VerbPost,
Published: activitystream.NewTime(i.Date),
Updated: activitystream.NewTime(i.Date),
Link: []activitystream.Link{
{Rel: "self", Type: "application/atom+xml", Href: fmt.Sprintf("%s/status/%s/%x.atom", b.baseURL, feedName, b.obfuscateFeedEntryID(i.ID))},
{Rel: "alternate", Type: "text/html", Href: i.Link},
{Rel: "mentioned", ObjectType: activitystream.ObjectCollection, Href: activitystream.CollectionPublic},
},
Content: &activitystream.Text{
Type: "html",
Lang: "en",
Body: content.String(),
},
}
feed.Entry = append(feed.Entry, entry)
}
return feed, nil
}
func (b backend) setFeedLastUpdate(feedName string, lastUpdate time.Time) error {
return b.db.Update(func(tx *bolt.Tx) error {
b, err := tx.CreateBucketIfNotExists(dateBucket)
if err != nil {
return err
}
k := []byte(feedName)
v := []byte(lastUpdate.Format(time.RFC3339Nano))
return b.Put(k, v)
})
}
func (b *backend) Subscribe(topicURL string, notifies chan<- pubsubhubbub.Event) error {
feedName := b.uriToFeedName(topicURL)
if feedName == "" {
log.WithField("topic", topicURL).Warn("Tried to subscribe invalid topic")
return errors.New("Invalid topic")
}
lastPostDate, err := b.getFeedLastUpdate(feedName)
if err != nil {
log.WithError(err).WithField("feed_name", feedName).Error("Unable to parse db stored last update")
return errors.Wrap(err, "Unable to parse db stored last update")
}
ticker := time.NewTicker(cfg.FeedPollInterval)
b.topics[topicURL] = &subscription{ticker, notifies}
go func() {
defer close(notifies)
for range ticker.C {
updated, err := b.feeds.Update(feedName)
if err != nil {
log.WithError(err).WithField("feed_name", feedName).Error("Unable to refresh feed")
continue
}
// If the feed database refused the refresh do not send updates
if !updated {
continue
}
items := []*rss.Item{}
maxDate := lastPostDate
for _, i := range b.feeds.entriesByFeedName(feedName) {
if i.Date.After(lastPostDate) {
items = append(items, i)
if i.Date.After(maxDate) {
maxDate = i.Date
}
}
}
feed, err := b.rssItemsToFeed(feedName, items)
if err != nil {
log.WithError(err).WithField("feed_name", feedName).Error("Unable to build feed")
continue
}
lastPostDate = maxDate
notifies <- feed
log.WithField("feed_name", feedName).Debug("Sent notification for feed update")
if err = b.setFeedLastUpdate(feedName, lastPostDate); err != nil {
log.WithError(err).WithField("feed_name", feedName).Error("Unable to store last update")
}
}
}()
return nil
}
func (b *backend) Unsubscribe(notifies chan<- pubsubhubbub.Event) error {
for topic, sub := range b.topics {
if notifies == sub.notifies {
delete(b.topics, topic)
sub.ticker.Stop()
return nil
}
}
return nil
}
func (b backend) uriToFeedName(uri string) string {
u, err := url.Parse(uri)
if err != nil {
return ""
}
switch u.Scheme {
case "acct":
return strings.SplitN(u.Opaque, "@", 2)[0]
case "http", "https", "":
return strings.TrimSuffix(strings.Trim(u.Path, "/@"), ".atom")
}
return ""
}