2024-07-02 07:44:37 +03:00
|
|
|
package handlers
|
2020-06-23 04:11:56 +03:00
|
|
|
|
|
|
|
import (
|
2024-01-22 04:18:47 +03:00
|
|
|
"bytes"
|
2022-09-11 01:37:07 +03:00
|
|
|
"encoding/json"
|
2022-12-13 03:57:17 +03:00
|
|
|
"fmt"
|
2020-06-23 04:11:56 +03:00
|
|
|
"net/http"
|
2023-08-17 04:19:09 +03:00
|
|
|
"net/url"
|
2020-10-04 00:35:03 +03:00
|
|
|
"path/filepath"
|
2022-09-11 01:37:07 +03:00
|
|
|
"strings"
|
2024-01-22 04:18:47 +03:00
|
|
|
"time"
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2023-08-17 04:19:09 +03:00
|
|
|
"github.com/owncast/owncast/config"
|
|
|
|
"github.com/owncast/owncast/core"
|
2024-01-22 04:18:47 +03:00
|
|
|
"github.com/owncast/owncast/core/cache"
|
2023-08-17 04:19:09 +03:00
|
|
|
"github.com/owncast/owncast/models"
|
2024-11-16 06:20:58 +03:00
|
|
|
"github.com/owncast/owncast/persistence/configrepository"
|
2022-09-11 01:37:07 +03:00
|
|
|
"github.com/owncast/owncast/static"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
2024-07-02 06:12:08 +03:00
|
|
|
"github.com/owncast/owncast/webserver/router/middleware"
|
2023-08-17 04:19:09 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2024-01-22 04:18:47 +03:00
|
|
|
var gc = cache.GetGlobalCache()
|
|
|
|
|
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
|
|
|
|
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-06-30 10:06:33 +03:00
|
|
|
|
2021-09-01 00:49:04 +03:00
|
|
|
if utils.IsUserAgentAPlayer(r.UserAgent()) && isIndexRequest {
|
|
|
|
http.Redirect(w, r, "/hls/stream.m3u8", http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-17 04:19:09 +03:00
|
|
|
// For search engine bots and social scrapers return a special
|
|
|
|
// server-rendered page.
|
|
|
|
if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest {
|
2023-12-20 05:20:46 +03:00
|
|
|
handleScraperMetadataPage(w, r)
|
2023-08-17 04:19:09 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:14:39 +03:00
|
|
|
// Set a cache control max-age header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
|
|
|
|
2022-12-13 03:57:17 +03:00
|
|
|
nonceRandom, _ := utils.GenerateRandomString(5)
|
|
|
|
|
2021-08-31 05:43:28 +03:00
|
|
|
// Set our global HTTP headers
|
2022-12-13 03:57:17 +03:00
|
|
|
middleware.SetHeaders(w, fmt.Sprintf("nonce-%s", nonceRandom))
|
|
|
|
|
|
|
|
if isIndexRequest {
|
|
|
|
renderIndexHtml(w, nonceRandom)
|
|
|
|
return
|
|
|
|
}
|
2021-04-16 08:17:52 +03:00
|
|
|
|
2022-06-20 01:30:32 +03:00
|
|
|
serveWeb(w, r)
|
2020-06-30 10:06:33 +03:00
|
|
|
}
|
2022-09-11 01:37:07 +03:00
|
|
|
|
2022-12-13 03:57:17 +03:00
|
|
|
func renderIndexHtml(w http.ResponseWriter, nonce string) {
|
2022-09-11 01:37:07 +03:00
|
|
|
type serverSideContent struct {
|
|
|
|
Name string
|
|
|
|
Summary string
|
|
|
|
RequestedURL string
|
|
|
|
TagsString string
|
|
|
|
ThumbnailURL string
|
|
|
|
Thumbnail string
|
|
|
|
Image string
|
|
|
|
StatusJSON string
|
|
|
|
ServerConfigJSON string
|
2023-06-14 05:28:35 +03:00
|
|
|
EmbedVideo string
|
2022-12-13 03:57:17 +03:00
|
|
|
Nonce string
|
2022-09-11 01:37:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
status := getStatusResponse()
|
|
|
|
sb, err := json.Marshal(status)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config := getConfigResponse()
|
|
|
|
cb, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-11-16 06:20:58 +03:00
|
|
|
configRepository := configrepository.Get()
|
2022-09-11 01:37:07 +03:00
|
|
|
content := serverSideContent{
|
2024-11-16 06:20:58 +03:00
|
|
|
Name: configRepository.GetServerName(),
|
|
|
|
Summary: configRepository.GetServerSummary(),
|
|
|
|
RequestedURL: fmt.Sprintf("%s%s", configRepository.GetServerURL(), "/"),
|
|
|
|
TagsString: strings.Join(configRepository.GetServerMetadataTags(), ","),
|
2023-06-14 05:28:35 +03:00
|
|
|
ThumbnailURL: "thumbnail.jpg",
|
|
|
|
Thumbnail: "thumbnail.jpg",
|
|
|
|
Image: "logo/external",
|
2022-09-11 01:37:07 +03:00
|
|
|
StatusJSON: string(sb),
|
|
|
|
ServerConfigJSON: string(cb),
|
2023-06-14 05:28:35 +03:00
|
|
|
EmbedVideo: "embed/video",
|
2022-12-13 03:57:17 +03:00
|
|
|
Nonce: nonce,
|
2022-09-11 01:37:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
index, err := static.GetWebIndexTemplate()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := index.Execute(w, content); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2023-08-17 04:19:09 +03:00
|
|
|
|
|
|
|
// MetadataPage represents a server-rendered web page for bots and web scrapers.
|
|
|
|
type MetadataPage struct {
|
|
|
|
RequestedURL string
|
|
|
|
Image string
|
|
|
|
Thumbnail string
|
|
|
|
TagsString string
|
|
|
|
Summary string
|
|
|
|
Name string
|
|
|
|
Tags []string
|
|
|
|
SocialHandles []models.SocialHandle
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a basic HTML page with server-rendered metadata from the config
|
|
|
|
// to give to Opengraph clients and web scrapers (bots, web crawlers, etc).
|
|
|
|
func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) {
|
2024-01-22 04:18:47 +03:00
|
|
|
cacheKey := "bot-scraper-html"
|
|
|
|
cacheHtmlExpiration := time.Duration(10) * time.Second
|
|
|
|
c := gc.GetOrCreateCache(cacheKey, cacheHtmlExpiration)
|
|
|
|
|
|
|
|
cachedHtml := c.GetValueForKey(cacheKey)
|
|
|
|
if cachedHtml != nil {
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
_, _ = w.Write(cachedHtml)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-17 04:19:09 +03:00
|
|
|
tmpl, err := static.GetBotMetadataTemplate()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
scheme := "http"
|
2024-11-16 06:20:58 +03:00
|
|
|
configRepository := configrepository.Get()
|
|
|
|
if siteURL := configRepository.GetServerURL(); siteURL != "" {
|
2023-08-17 04:19:09 +03:00
|
|
|
if parsed, err := url.Parse(siteURL); err == nil && parsed.Scheme != "" {
|
|
|
|
scheme = parsed.Scheme
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fullURL, err := url.Parse(fmt.Sprintf("%s://%s%s", scheme, r.Host, r.URL.Path))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
imageURL, err := url.Parse(fmt.Sprintf("%s://%s%s", scheme, r.Host, "/logo/external"))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
status := core.GetStatus()
|
|
|
|
|
|
|
|
// If the thumbnail does not exist or we're offline then just use the logo image
|
|
|
|
var thumbnailURL string
|
|
|
|
if status.Online && utils.DoesFileExists(filepath.Join(config.DataDirectory, "tmp", "thumbnail.jpg")) {
|
|
|
|
thumbnail, err := url.Parse(fmt.Sprintf("%s://%s%s", scheme, r.Host, "/thumbnail.jpg"))
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
thumbnailURL = imageURL.String()
|
|
|
|
} else {
|
|
|
|
thumbnailURL = thumbnail.String()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
thumbnailURL = imageURL.String()
|
|
|
|
}
|
|
|
|
|
2024-11-16 06:20:58 +03:00
|
|
|
tagsString := strings.Join(configRepository.GetServerMetadataTags(), ",")
|
2023-08-17 04:19:09 +03:00
|
|
|
metadata := MetadataPage{
|
2024-11-16 06:20:58 +03:00
|
|
|
Name: configRepository.GetServerName(),
|
2023-08-17 04:19:09 +03:00
|
|
|
RequestedURL: fullURL.String(),
|
|
|
|
Image: imageURL.String(),
|
2024-11-16 06:20:58 +03:00
|
|
|
Summary: configRepository.GetServerSummary(),
|
2023-08-17 04:19:09 +03:00
|
|
|
Thumbnail: thumbnailURL,
|
|
|
|
TagsString: tagsString,
|
2024-11-16 06:20:58 +03:00
|
|
|
Tags: configRepository.GetServerMetadataTags(),
|
|
|
|
SocialHandles: configRepository.GetSocialHandles(),
|
2023-08-17 04:19:09 +03:00
|
|
|
}
|
|
|
|
|
2024-01-22 04:18:47 +03:00
|
|
|
// Cache the rendered HTML
|
|
|
|
var b bytes.Buffer
|
|
|
|
if err := tmpl.Execute(&b, metadata); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
c.Set(cacheKey, b.Bytes())
|
|
|
|
|
2023-08-17 04:19:09 +03:00
|
|
|
// Set a cache header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
2024-01-22 04:18:47 +03:00
|
|
|
if _, err = w.Write(b.Bytes()); err != nil {
|
2023-08-17 04:19:09 +03:00
|
|
|
log.Errorln(err)
|
|
|
|
}
|
|
|
|
}
|