From ab21706d73628729f29689bed706682314327bff Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Mon, 13 Jul 2020 19:07:30 -0700 Subject: [PATCH] Add our own botlist for user-agent matching. Closes #51 --- controllers/index.go | 5 +---- utils/utils.go | 23 +++++++++++++++++++++++ utils/utils_test.go | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 utils/utils_test.go diff --git a/controllers/index.go b/controllers/index.go index 932e242d7..40f6bb07b 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -8,7 +8,6 @@ import ( "strings" "text/template" - "github.com/mssola/user_agent" log "github.com/sirupsen/logrus" "github.com/gabek/owncast/config" @@ -38,9 +37,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { return } - ua := user_agent.New(r.UserAgent()) - - if ua != nil && ua.Bot() && isIndexRequest { + if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest { handleScraperMetadataPage(w, r) return } diff --git a/utils/utils.go b/utils/utils.go index 43b211d6e..928abd81b 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,6 +5,8 @@ import ( "os" "path/filepath" "strings" + + "github.com/mssola/user_agent" ) //GetTemporaryPipePath gets the temporary path for the streampipe.flv file @@ -41,3 +43,24 @@ func Copy(source, destination string) error { return ioutil.WriteFile(destination, input, 0644) } + +// IsUserAgentABot returns if a web client user-agent is seen as a bot +func IsUserAgentABot(userAgent string) bool { + if userAgent == "" { + return false + } + + botStrings := []string{ + "mastodon", + "pleroma", + } + + for _, botString := range botStrings { + if strings.Contains(strings.ToLower(userAgent), botString) { + return true + } + } + + ua := user_agent.New(userAgent) + return ua.Bot() +} diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 000000000..bb25c5ba7 --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,16 @@ +package utils + +import "testing" + +func TestUserAgent(t *testing.T) { + testAgents := []string{ + "Pleroma 1.0.0-1168-ge18c7866-pleroma-dot-site; https://pleroma.site info@pleroma.site", + "Mastodon 1.2.3 Bot", + } + + for _, agent := range testAgents { + if !IsUserAgentABot(agent) { + t.Error("Incorrect parsing of useragent", agent) + } + } +}