85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"git.nadeko.net/Fijxu/http3-ytproxy/internal/httpc"
|
|
)
|
|
|
|
const (
|
|
path_prefix = ""
|
|
)
|
|
|
|
var strip_headers = []string{
|
|
"Accept-Encoding",
|
|
"Authorization",
|
|
"Origin",
|
|
"Referer",
|
|
"Cookie",
|
|
"Set-Cookie",
|
|
"Etag",
|
|
"Alt-Svc",
|
|
"Server",
|
|
"Cache-Control",
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/report-to
|
|
"report-to",
|
|
}
|
|
|
|
func CopyHeaders(from http.Header, to http.Header, length bool) {
|
|
// Loop over header names
|
|
outer:
|
|
for name, values := range from {
|
|
for _, header := range strip_headers {
|
|
if name == header {
|
|
continue outer
|
|
}
|
|
}
|
|
if (name != "Content-Length" || length) && !strings.HasPrefix(name, "Access-Control") {
|
|
// Loop over all values for the name.
|
|
for _, value := range values {
|
|
if strings.Contains(value, "jpeg") {
|
|
continue
|
|
}
|
|
to.Set(name, value)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func GetBestThumbnail(path string) (newpath string) {
|
|
|
|
formats := [4]string{"maxresdefault.jpg", "sddefault.jpg", "hqdefault.jpg", "mqdefault.jpg"}
|
|
|
|
for _, format := range formats {
|
|
newpath = strings.Replace(path, "maxres.jpg", format, 1)
|
|
url := "https://i.ytimg.com" + newpath
|
|
resp, _ := httpc.Client.Head(url)
|
|
if resp.StatusCode == 200 {
|
|
return newpath
|
|
}
|
|
}
|
|
|
|
return strings.Replace(path, "maxres.jpg", "mqdefault.jpg", 1)
|
|
}
|
|
|
|
func RelativeUrl(in string) (newurl string) {
|
|
segment_url, err := url.Parse(in)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
segment_query := segment_url.Query()
|
|
segment_query.Set("host", segment_url.Hostname())
|
|
segment_url.RawQuery = segment_query.Encode()
|
|
segment_url.Path = path_prefix + segment_url.Path
|
|
return segment_url.RequestURI()
|
|
}
|
|
|
|
func PanicHandler(w http.ResponseWriter) {
|
|
if r := recover(); r != nil {
|
|
log.Printf("Panic: %v", r)
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
}
|
|
}
|