add user agent check
Marco Andronaco andronacomarco@gmail.com
Tue, 19 Dec 2023 10:40:02 +0100
2 files changed,
37 insertions(+),
33 deletions(-)
M
fixyoutube.go
→
fixyoutube.go
@@ -7,6 +7,7 @@ "log"
"net/http" "net/url" "os" + "regexp" "slices" "time"@@ -19,6 +20,7 @@ var templatesDirectory = "templates/"
var indexTemplate = template.Must(template.ParseFiles(templatesDirectory + "index.html")) var videoTemplate = template.Must(template.ParseFiles(templatesDirectory + "video.html")) var blacklist = []string{"favicon.ico", "robots.txt"} +var userAgentRegex = regexp.MustCompile(`bot|facebook|embed|got|firefox\/92|firefox\/38|curl|wget|go-http|yahoo|generator|whatsapp|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node`) func indexHandler(w http.ResponseWriter, r *http.Request) { buf := &bytes.Buffer{}@@ -37,6 +39,30 @@ invidious.ClearDB()
//TODO: return a message } +func videoHandler(videoId string, invidiousClient *invidious.Client, w http.ResponseWriter, r *http.Request) { + + res := userAgentRegex.MatchString(r.UserAgent()) + if !res { + url := "https://www.youtube.com/watch?v=" + videoId + http.Redirect(w, r, url, http.StatusMovedPermanently) + return + } + + video, err := invidiousClient.GetVideo(videoId) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + buf := &bytes.Buffer{} + err = videoTemplate.Execute(buf, video) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + buf.WriteTo(w) +} + func watchHandler(invidiousClient *invidious.Client) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { u, err := url.Parse(r.URL.String())@@ -45,23 +71,8 @@ http.Error(w, err.Error(), http.StatusInternalServerError)
return } - params := u.Query() - videoId := params.Get("v") - - video, err := invidiousClient.GetVideo(videoId) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - buf := &bytes.Buffer{} - err = videoTemplate.Execute(buf, video) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - buf.WriteTo(w) + videoId := u.Query().Get("v") + videoHandler(videoId, invidiousClient, w, r) } }@@ -74,20 +85,7 @@ http.Error(w, "Not a valid ID.", http.StatusBadRequest)
return } - video, err := invidiousClient.GetVideo(videoId) - if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - - buf := &bytes.Buffer{} - err = videoTemplate.Execute(buf, video) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - buf.WriteTo(w) + videoHandler(videoId, invidiousClient, w, r) } }@@ -111,6 +109,13 @@ r.HandleFunc("/clear", clearHandler)
r.HandleFunc("/watch", watchHandler(videoapi)) r.HandleFunc("/{videoId}", shortHandler(videoapi)) //TODO: r.HandleFunc("/proxy/{videoId}", proxyHandler) + //TODO: check user agent before serving templates. + + /* + UA_REGEX = r"bot|facebook|embed|got|firefox\/92|firefox\/38|curl|wget|go-http|yahoo|generator|whatsapp|preview|link|proxy|vkshare|images|analyzer|index|crawl|spider|python|cfnetwork|node" + PROXY_HEADERS_REQUEST = { "Range": f"bytes=0-{MAX_SIZE_MB}000000" } + PROXY_HEADERS_RESPONSE = { "Content-Type": "video/mp4" } + */ /* // native go implementation (useless until february 2024)
M
templates/video.html
→
templates/video.html
@@ -1,5 +1,4 @@
<!DOCTYPE html> -<html lang="und"> ███████ ██ ██ ██ ██ ██ ███ ██ ██ ██████ ██ ██ ██████ ███████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ █████ ██ ███ ████ ██ ██ ██ ██ ██ ██ ██ ██████ █████@@ -8,7 +7,7 @@ ██ ██ ██ ██ ██ ███ ███ ██ ███ ██████ ███████
██ ██ A better way to embed YouTube videos on Telegram (inspired by FixTweet). ██ -<head> +<html lang="en"><head> <link rel="canonical" href="https://www.youtube.com/watch?v={{ .VideoId }}" /> <meta property="theme-color" content="0000FF" /> <meta property="twitter:card" content="player" />