2020-06-23 04:11:56 +03:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2020-06-30 10:06:33 +03:00
|
|
|
"fmt"
|
2020-06-23 04:11:56 +03:00
|
|
|
"net/http"
|
2020-07-01 01:21:52 +03:00
|
|
|
"net/url"
|
2020-12-28 02:41:21 +03:00
|
|
|
"os"
|
2020-06-23 04:11:56 +03:00
|
|
|
"path"
|
2020-10-04 00:35:03 +03:00
|
|
|
"path/filepath"
|
2020-07-01 01:21:52 +03:00
|
|
|
"strings"
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2020-07-01 01:21:52 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/core"
|
2021-02-19 10:05:52 +03:00
|
|
|
"github.com/owncast/owncast/core/data"
|
|
|
|
"github.com/owncast/owncast/models"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/router/middleware"
|
2021-10-12 01:04:16 +03:00
|
|
|
"github.com/owncast/owncast/static"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
// MetadataPage represents a server-rendered web page for bots and web scrapers.
|
2020-06-30 10:06:33 +03:00
|
|
|
type MetadataPage struct {
|
2021-02-19 10:05:52 +03:00
|
|
|
RequestedURL string
|
|
|
|
Image string
|
|
|
|
Thumbnail string
|
|
|
|
TagsString string
|
|
|
|
Summary string
|
|
|
|
Name string
|
|
|
|
Tags []string
|
|
|
|
SocialHandles []models.SocialHandle
|
2020-06-30 10:06:33 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// IndexHandler handles the default index route.
|
2020-06-23 04:11:56 +03:00
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
2021-10-01 22:40:13 +03:00
|
|
|
middleware.EnableCors(w)
|
2022-01-13 00:53:10 +03:00
|
|
|
|
|
|
|
// Treat recordings and schedule as index requests
|
|
|
|
pathComponents := strings.Split(r.URL.Path, "/")
|
|
|
|
pathRequest := pathComponents[1]
|
|
|
|
|
|
|
|
if pathRequest == "recordings" || pathRequest == "schedule" {
|
|
|
|
r.URL.Path = "index.html"
|
|
|
|
}
|
|
|
|
|
2020-10-09 02:34:40 +03:00
|
|
|
isIndexRequest := r.URL.Path == "/" || filepath.Base(r.URL.Path) == "index.html" || filepath.Base(r.URL.Path) == ""
|
2020-07-07 07:53:30 +03:00
|
|
|
|
2020-10-01 00:14:39 +03:00
|
|
|
// For search engine bots and social scrapers return a special
|
|
|
|
// server-rendered page.
|
2020-07-14 05:07:30 +03:00
|
|
|
if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest {
|
2020-06-30 10:06:33 +03:00
|
|
|
handleScraperMetadataPage(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-01 00:49:04 +03:00
|
|
|
if utils.IsUserAgentAPlayer(r.UserAgent()) && isIndexRequest {
|
|
|
|
http.Redirect(w, r, "/hls/stream.m3u8", http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:14:39 +03:00
|
|
|
// If the ETags match then return a StatusNotModified
|
|
|
|
if responseCode := middleware.ProcessEtags(w, r); responseCode != 0 {
|
|
|
|
w.WriteHeader(responseCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-28 02:41:21 +03:00
|
|
|
// If this is a directory listing request then return a 404
|
|
|
|
info, err := os.Stat(path.Join(config.WebRoot, r.URL.Path))
|
|
|
|
if err != nil || (info.IsDir() && !isIndexRequest) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:14:39 +03:00
|
|
|
// Set a cache control max-age header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
|
|
|
|
2021-08-31 05:43:28 +03:00
|
|
|
// Set our global HTTP headers
|
|
|
|
middleware.SetHeaders(w)
|
2021-04-16 08:17:52 +03:00
|
|
|
|
2020-10-04 00:35:03 +03:00
|
|
|
http.ServeFile(w, r, path.Join(config.WebRoot, r.URL.Path))
|
2020-06-23 04:11:56 +03:00
|
|
|
}
|
2020-06-30 10:06:33 +03:00
|
|
|
|
2022-02-01 03:01:34 +03:00
|
|
|
// Return a basic HTML page with server-rendered metadata from the config
|
2020-06-30 10:06:33 +03:00
|
|
|
// to give to Opengraph clients and web scrapers (bots, web crawlers, etc).
|
|
|
|
func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) {
|
2021-10-12 01:04:16 +03:00
|
|
|
tmpl, err := static.GetBotMetadataTemplate()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2020-06-30 10:06:33 +03:00
|
|
|
|
2021-07-20 20:34:59 +03:00
|
|
|
scheme := "http"
|
|
|
|
|
2021-09-12 10:18:15 +03:00
|
|
|
if siteURL := data.GetServerURL(); siteURL != "" {
|
|
|
|
if parsed, err := url.Parse(siteURL); err == nil && parsed.Scheme != "" {
|
2021-07-20 20:34:59 +03:00
|
|
|
scheme = parsed.Scheme
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fullURL, err := url.Parse(fmt.Sprintf("%s://%s%s", scheme, r.Host, r.URL.Path))
|
2020-10-17 01:04:31 +03:00
|
|
|
if err != nil {
|
2022-01-13 00:53:10 +03:00
|
|
|
log.Errorln(err)
|
2020-10-17 01:04:31 +03:00
|
|
|
}
|
2021-08-28 02:20:16 +03:00
|
|
|
imageURL, err := url.Parse(fmt.Sprintf("%s://%s%s", scheme, r.Host, "/logo/external"))
|
2020-10-17 01:04:31 +03:00
|
|
|
if err != nil {
|
2022-01-13 00:53:10 +03:00
|
|
|
log.Errorln(err)
|
2020-10-17 01:04:31 +03:00
|
|
|
}
|
2020-07-01 01:21:52 +03:00
|
|
|
|
2020-07-20 04:36:04 +03:00
|
|
|
status := core.GetStatus()
|
|
|
|
|
|
|
|
// If the thumbnail does not exist or we're offline then just use the logo image
|
2020-07-01 01:21:52 +03:00
|
|
|
var thumbnailURL string
|
2020-10-04 00:35:03 +03:00
|
|
|
if status.Online && utils.DoesFileExists(filepath.Join(config.WebRoot, "thumbnail.jpg")) {
|
2021-07-28 01:33:57 +03:00
|
|
|
thumbnail, err := url.Parse(fmt.Sprintf("%s://%s%s", scheme, r.Host, "/thumbnail.jpg"))
|
2020-07-01 01:21:52 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
2020-10-17 01:04:31 +03:00
|
|
|
thumbnailURL = imageURL.String()
|
|
|
|
} else {
|
|
|
|
thumbnailURL = thumbnail.String()
|
2020-07-01 01:21:52 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
thumbnailURL = imageURL.String()
|
|
|
|
}
|
|
|
|
|
2021-02-19 10:05:52 +03:00
|
|
|
tagsString := strings.Join(data.GetServerMetadataTags(), ",")
|
|
|
|
metadata := MetadataPage{
|
2021-02-21 03:47:47 +03:00
|
|
|
Name: data.GetServerName(),
|
2021-02-19 10:05:52 +03:00
|
|
|
RequestedURL: fullURL.String(),
|
|
|
|
Image: imageURL.String(),
|
2021-07-14 00:44:03 +03:00
|
|
|
Summary: data.GetServerSummary(),
|
2021-02-19 10:05:52 +03:00
|
|
|
Thumbnail: thumbnailURL,
|
|
|
|
TagsString: tagsString,
|
|
|
|
Tags: data.GetServerMetadataTags(),
|
|
|
|
SocialHandles: data.GetSocialHandles(),
|
|
|
|
}
|
2020-06-30 10:06:33 +03:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2021-07-09 21:16:44 +03:00
|
|
|
if err := tmpl.Execute(w, metadata); err != nil {
|
2022-01-13 00:53:10 +03:00
|
|
|
log.Errorln(err)
|
2020-06-30 10:06:33 +03:00
|
|
|
}
|
|
|
|
}
|