From 5b5de2dc4b26a2643329298cffed3a2b0ffe8be9 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 18 Oct 2016 10:45:22 +0100 Subject: [PATCH] Set "Go-NEB" as the User-Agent on RSS feed polling Some services, notably Reddit, basically force you to set a custom UA in order to use their RSS feeds. All the common default non-browser UAs are HEAVILY rate-limited such that you can't realistically use them. --- .../matrix-org/go-neb/services/rssbot/rssbot.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go index c2e5be0..f89919f 100644 --- a/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go +++ b/src/github.com/matrix-org/go-neb/services/rssbot/rssbot.go @@ -306,9 +306,20 @@ func itemToHTML(feed *gofeed.Feed, item gofeed.Item) matrix.HTMLMessage { )) } +type userAgentRoundTripper struct { + Transport http.RoundTripper +} + +func (rt userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { + req.Header.Set("User-Agent", "Go-NEB") + return rt.Transport.RoundTrip(req) +} + func init() { lruCache := lrucache.New(1024*1024*20, 0) // 20 MB cache, no max-age - cachingClient = httpcache.NewTransport(lruCache).Client() + cachingClient = &http.Client{ + Transport: userAgentRoundTripper{httpcache.NewTransport(lruCache)}, + } types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { r := &rssBotService{ id: serviceID,