feat: change how headers are passed to the client and move constant variables

This commit is contained in:
Fijxu 2025-03-06 22:13:39 -03:00
parent f7e75ce5e7
commit e8d7b14d82
Signed by: Fijxu
GPG key ID: 32C1DDF333EDA6A4
4 changed files with 46 additions and 40 deletions

View file

@ -20,21 +20,6 @@ var allowed_hosts = []string{
"googleusercontent.com", "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{ var videoplayback_headers = http.Header{
"Accept": {"*/*"}, "Accept": {"*/*"},
"Accept-Encoding": {"gzip, deflate, br, zstd"}, "Accept-Encoding": {"gzip, deflate, br, zstd"},
@ -42,3 +27,6 @@ var videoplayback_headers = http.Header{
"Origin": {"https://www.youtube.com"}, "Origin": {"https://www.youtube.com"},
"Referer": {"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

View file

@ -123,10 +123,7 @@ func Videoplayback(w http.ResponseWriter, req *http.Request) {
proxyURL.RawQuery = q.Encode() proxyURL.RawQuery = q.Encode()
// https://github.com/FreeTubeApp/FreeTube/blob/5a4cd981cdf2c2a20ab68b001746658fd0c6484e/src/renderer/components/ft-shaka-video-player/ft-shaka-video-player.js#L1097 postRequest, err := http.NewRequest("POST", proxyURL.String(), bytes.NewReader(protobuf_body))
body := []byte{0x78, 0} // protobuf body
postRequest, err := http.NewRequest("POST", proxyURL.String(), bytes.NewReader(body))
if err != nil { if err != nil {
log.Panic("Failed to create postRequest:", err) log.Panic("Failed to create postRequest:", err)
} }
@ -184,8 +181,7 @@ func Videoplayback(w http.ResponseWriter, req *http.Request) {
defer resp.Body.Close() defer resp.Body.Close()
NoRewrite := strings.HasPrefix(resp.Header.Get("Content-Type"), "audio") || strings.HasPrefix(resp.Header.Get("Content-Type"), "video") utils.CopyHeadersNew(resp.Header, w.Header())
utils.CopyHeaders(resp.Header, w.Header(), NoRewrite)
w.WriteHeader(resp.StatusCode) w.WriteHeader(resp.StatusCode)

28
internal/utils/consts.go Normal file
View file

@ -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",
}

View file

@ -1,6 +1,9 @@
package utils package utils
import ( import (
"crypto/aes"
"encoding/hex"
"fmt"
"log" "log"
"net/http" "net/http"
"net/url" "net/url"
@ -9,25 +12,6 @@ import (
"git.nadeko.net/Fijxu/http3-ytproxy/internal/httpc" "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) { func CopyHeaders(from http.Header, to http.Header, length bool) {
// Loop over header names // Loop over header names
outer: outer:
@ -49,6 +33,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) { func GetBestThumbnail(path string) (newpath string) {
formats := [4]string{"maxresdefault.jpg", "sddefault.jpg", "hqdefault.jpg", "mqdefault.jpg"} formats := [4]string{"maxresdefault.jpg", "sddefault.jpg", "hqdefault.jpg", "mqdefault.jpg"}