Restrict to YouTube related domains

This commit is contained in:
FireMasterK 2021-07-21 23:53:27 +05:30
parent 6019c4efd1
commit 016a677be7
No known key found for this signature in database
GPG Key ID: 49451E4482CC5BCD
1 changed files with 30 additions and 0 deletions

30
main.go
View File

@ -42,6 +42,13 @@ var h2client = &http.Client{
// user agent to use
var ua = "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101"
var allowed_hosts = []string{
"youtube.com",
"googlevideo.com",
"ytimg.com",
"ggpht.com",
}
type requesthandler struct{}
func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
@ -63,6 +70,29 @@ func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}
parts := strings.Split(strings.ToLower(host), ".")
if len(parts) < 2 {
io.WriteString(w, "Invalid hostname.")
return
}
domain := parts[len(parts)-2] + "." + parts[len(parts)-1]
disallowed := true
for _, value := range allowed_hosts {
if domain == value {
disallowed = false
break
}
}
if disallowed {
io.WriteString(w, "Non YouTube domains are not supported.")
return
}
path := req.URL.EscapedPath()
path = strings.Replace(path, "/ggpht", "", 1)