Swap out server-side agent detection.

This commit is contained in:
Gabe Kangas 2020-06-30 15:21:52 -07:00
parent c28f5d49f6
commit b4c9654dac
4 changed files with 44 additions and 18 deletions

View File

@ -2,31 +2,36 @@ package controllers
import (
"fmt"
"log"
"net/http"
"net/url"
"path"
"strings"
"text/template"
"github.com/mssola/user_agent"
log "github.com/sirupsen/logrus"
"github.com/gabek/owncast/config"
"github.com/gabek/owncast/core"
"github.com/gabek/owncast/router/middleware"
"github.com/gabek/owncast/utils"
"xojoc.pw/useragent"
)
type MetadataPage struct {
Config config.InstanceDetails
RequestedURL string
Image string
Thumbnail string
TagsString string
}
//IndexHandler handles the default index route
func IndexHandler(w http.ResponseWriter, r *http.Request) {
middleware.EnableCors(&w)
ua := useragent.Parse(r.UserAgent())
ua := user_agent.New(r.UserAgent())
if ua.Type == useragent.Crawler && r.URL.Path == "/" {
if ua != nil && ua.Bot() && (r.URL.Path == "/" || r.URL.Path == "/index.html") {
handleScraperMetadataPage(w, r)
return
}
@ -46,11 +51,27 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles(path.Join("static", "metadata.html")))
fullURL := fmt.Sprintf("http://%s%s", r.Host, r.URL.Path)
metadata := MetadataPage{config.Config.InstanceDetails, fullURL}
fullURL, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, r.URL.Path))
imageURL, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, config.Config.InstanceDetails.Logo["large"]))
// If the thumbnail does not exist then just use the logo image
var thumbnailURL string
if utils.DoesFileExists("webroot/thumbnail.jpg") {
thumbnail, err := url.Parse(fmt.Sprintf("http://%s%s", r.Host, "/thumbnail.jpg"))
if err != nil {
log.Errorln(err)
}
thumbnailURL = thumbnail.String()
} else {
thumbnailURL = imageURL.String()
}
tagsString := strings.Join(config.Config.InstanceDetails.Tags, ",")
metadata := MetadataPage{config.Config.InstanceDetails, fullURL.String(), imageURL.String(), thumbnailURL, tagsString}
w.Header().Set("Content-Type", "text/html")
err := tmpl.Execute(w, metadata)
err = tmpl.Execute(w, metadata)
if err != nil {
log.Panicln(err)

2
go.mod
View File

@ -10,6 +10,7 @@ require (
github.com/ipfs/interface-go-ipfs-core v0.2.7
github.com/libp2p/go-libp2p-peer v0.2.0
github.com/libp2p/go-libp2p-peerstore v0.2.6
github.com/mssola/user_agent v0.5.2
github.com/multiformats/go-multiaddr v0.2.2
github.com/radovskyb/watcher v1.0.7
github.com/sirupsen/logrus v1.6.0
@ -18,5 +19,4 @@ require (
github.com/yutopp/go-rtmp v0.0.0-20191212152852-4e41609a99bb
golang.org/x/net v0.0.0-20200602114024-627f9648deb9
gopkg.in/yaml.v2 v2.3.0
xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe
)

4
go.sum
View File

@ -749,6 +749,8 @@ github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78=
github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/mssola/user_agent v0.5.2 h1:CZkTUahjL1+OcZ5zv3kZr8QiJ8jy2H08vZIEkBeRbxo=
github.com/mssola/user_agent v0.5.2/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
github.com/multiformats/go-multiaddr v0.0.1/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44=
@ -1192,5 +1194,3 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck=
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=
xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe h1:KHyqPlOEFFT7OPh4WR7qFzNNndwj1VuwV+rZ+Tb3bio=
xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe/go.mod h1:71om/Qz9HbIEjbUrkrzmJiF26FSh6tcwqSFdBBkLtJQ=

View File

@ -4,25 +4,30 @@
<head>
<meta charset="utf-8">
<title>{{.Config.Name}}</title>
<title>{{.Config.Title}}</title>
<meta name="description" content="{{.Config.Summary}}">
<meta property="og:title" content="{{.Config.Title}}">
<meta property="og:site_name" content="{{.Config.Title}}">
<meta property="og:url" content="{{.RequestedURL}}">
<meta property="og:description" content="{{.Config.Summary}}">
<meta property="og:type" content="video">
<meta property="og:image" content="{{.Config.Logo.large}}">
<meta property="og:image:url" content="{{.Config.Logo.large}}">
<meta property='og:video' content='{{.RequestedURL}}hls/stream.m3u8' />
<meta property="og:type" content="video.other">
<meta property="video:tag" content="{{.TagsString}}">
<meta property="og:image" content="{{.Image}}">
<meta property="og:image:url" content="{{.Image}}">
<meta property="og:image:alt" content="{{.Thumbnail}}">
<meta property="og:video" content='{{.RequestedURL}}hls/stream.m3u8' />
<meta property="og:video:height" content="640" />
<meta property="og:video:width" content="385" />
<meta property="og:video:type" content="application/x-mpegURL" />
<meta property="og:video:actor" content="{{.Config.Name}}" />
<meta property="twitter:title" content="{{.Config.Title}}">
<meta property="twitter:url" content="{{.RequestedURL}}">
<meta property="twitter:description" content="{{.Config.Summary}}">
<meta property="twitter:image" content="{{.Config.Logo.large}}">
<meta property="twitter:image" content="{{.Image}}">
<meta property="twitter:site" content="{{.Config.SocialHandles.twitter}}">
</head>
@ -32,7 +37,7 @@
<h2>{{.Config.Name}}</h2>
<center>
<img src="/thumbnail.jpg" width=10% />
<img src="{{.Thumbnail}}" width=10% />
</center>
<h3>{{.Config.Summary}}</h3>