-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
146 lines (127 loc) · 3.98 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
"embed"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"text/template"
"github.com/BiRabittoh/fixyoutube-go/invidious"
)
const templatesDirectory = "templates/"
var (
//go:embed templates/index.html templates/video.html
templates embed.FS
indexTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"index.html"))
videoTemplate = template.Must(template.ParseFS(templates, templatesDirectory+"video.html"))
userAgentRegex = regexp.MustCompile(`(?i)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`)
videoRegex = regexp.MustCompile(`(?i)^[a-z0-9_-]{11}$`)
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
buf := &bytes.Buffer{}
err := indexTemplate.Execute(buf, nil)
if err != nil {
logger.Error("Failed to fill index template.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
buf.WriteTo(w)
}
func clearHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Redirect(w, r, "/", http.StatusMovedPermanently)
return
}
err := r.ParseForm()
if err != nil {
logger.Error("Failed to parse form in /clear.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
providedKey := r.PostForm.Get("apiKey")
if providedKey != apiKey {
logger.Debug("Wrong API key: ", providedKey)
http.Error(w, "Wrong or missing API key.", http.StatusForbidden)
return
}
invidious.ClearDB()
logger.Info("Cache cleared.")
http.Error(w, "Done.", http.StatusOK)
}
func videoHandler(videoId string, invidiousClient *invidious.Client, w http.ResponseWriter, r *http.Request) {
url := "https://www.youtube.com/watch?v=" + videoId
userAgent := r.UserAgent()
res := userAgentRegex.MatchString(userAgent)
if !res {
logger.Debug("Regex did not match. Redirecting. UA:", userAgent)
http.Redirect(w, r, url, http.StatusMovedPermanently)
return
}
if !videoRegex.MatchString(videoId) {
logger.Info("Invalid video ID: ", videoId)
http.Error(w, "Invalid video ID.", http.StatusBadRequest)
return
}
video, err := invidiousClient.GetVideo(videoId, true)
if err != nil {
logger.Info("Wrong video ID: ", videoId)
http.Error(w, "Wrong video ID.", http.StatusNotFound)
return
}
if video.Url == "" {
logger.Debug("No URL available. Redirecting.")
http.Redirect(w, r, url, http.StatusMovedPermanently)
return
}
buf := &bytes.Buffer{}
err = videoTemplate.Execute(buf, video)
if err != nil {
logger.Error("Failed to fill video template.")
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())
if err != nil {
logger.Error("Failed to parse URL: ", r.URL.String())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
q := u.Query()
videoId := q.Get("v")
videoHandler(videoId, invidiousClient, w, r)
}
}
func shortHandler(invidiousClient *invidious.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
videoId := r.PathValue("videoId")
videoHandler(videoId, invidiousClient, w, r)
}
}
func proxyHandler(invidiousClient *invidious.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
videoId := r.PathValue("videoId")
vb, s := invidiousClient.ProxyVideoId(videoId)
if s != http.StatusOK {
logger.Error("proxyHandler() failed. Final code: ", s)
http.Error(w, http.StatusText(s), s)
return
}
if !vb.ValidateLength() {
logger.Error("Buffer length is inconsistent.")
status := http.StatusInternalServerError
http.Error(w, http.StatusText(status), status)
return
}
h := w.Header()
h.Set("Status", "200")
h.Set("Content-Type", "video/mp4")
h.Set("Content-Length", strconv.FormatInt(vb.Length, 10))
io.Copy(w, vb.Buffer)
}
}