diff --git a/internal/paths/consts.go b/internal/paths/consts.go index eb72271..70501c8 100644 --- a/internal/paths/consts.go +++ b/internal/paths/consts.go @@ -20,21 +20,6 @@ var allowed_hosts = []string{ "googleusercontent.com", } -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", -} - var videoplayback_headers = http.Header{ "Accept": {"*/*"}, "Accept-Encoding": {"gzip, deflate, br, zstd"}, @@ -42,3 +27,6 @@ var videoplayback_headers = http.Header{ "Origin": {"https://www.youtube.com"}, "Referer": {"https://www.youtube.com/"}, } + +// https://github.com/FreeTubeApp/FreeTube/blob/5a4cd981cdf2c2a20ab68b001746658fd0c6484e/src/renderer/components/ft-shaka-video-player/ft-shaka-video-player.js#L1097 +var protobuf_body = []byte{0x78, 0} // protobuf body diff --git a/internal/paths/videoplayback.go b/internal/paths/videoplayback.go index 315a8f3..712afba 100644 --- a/internal/paths/videoplayback.go +++ b/internal/paths/videoplayback.go @@ -123,10 +123,7 @@ func Videoplayback(w http.ResponseWriter, req *http.Request) { proxyURL.RawQuery = q.Encode() - // https://github.com/FreeTubeApp/FreeTube/blob/5a4cd981cdf2c2a20ab68b001746658fd0c6484e/src/renderer/components/ft-shaka-video-player/ft-shaka-video-player.js#L1097 - body := []byte{0x78, 0} // protobuf body - - postRequest, err := http.NewRequest("POST", proxyURL.String(), bytes.NewReader(body)) + postRequest, err := http.NewRequest("POST", proxyURL.String(), bytes.NewReader(protobuf_body)) if err != nil { log.Panic("Failed to create postRequest:", err) } @@ -184,8 +181,7 @@ func Videoplayback(w http.ResponseWriter, req *http.Request) { defer resp.Body.Close() - NoRewrite := strings.HasPrefix(resp.Header.Get("Content-Type"), "audio") || strings.HasPrefix(resp.Header.Get("Content-Type"), "video") - utils.CopyHeaders(resp.Header, w.Header(), NoRewrite) + utils.CopyHeadersNew(resp.Header, w.Header()) w.WriteHeader(resp.StatusCode) diff --git a/internal/utils/consts.go b/internal/utils/consts.go new file mode 100644 index 0000000..37015ee --- /dev/null +++ b/internal/utils/consts.go @@ -0,0 +1,28 @@ +package utils + +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", +} + +var headers_for_response = []string{ + "Content-Length", + "Accept-Ranges", + "Content-Type", + "Expires", + "Last-Modified", +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 6718101..5c05672 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -9,25 +9,6 @@ import ( "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: @@ -49,6 +30,16 @@ outer: } } +func CopyHeadersNew(from http.Header, to http.Header) { + for from_header, value := range from { + for _, header := range headers_for_response { + if from_header == header { + to.Add(header, value[0]) + } + } + } +} + func GetBestThumbnail(path string) (newpath string) { formats := [4]string{"maxresdefault.jpg", "sddefault.jpg", "hqdefault.jpg", "mqdefault.jpg"}