235 lines
5.8 KiB
Go
235 lines
5.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"sync/atomic"
|
|
)
|
|
|
|
type ResponseWriterWrapper struct {
|
|
http.ResponseWriter
|
|
size int
|
|
}
|
|
|
|
func (w *ResponseWriterWrapper) Size() int {
|
|
return w.size
|
|
}
|
|
|
|
// This overrides the http.ResponseWriter Write() function. Do not remove!
|
|
func (w *ResponseWriterWrapper) Write(b []byte) (int, error) {
|
|
n, err := w.ResponseWriter.Write(b)
|
|
w.size += n
|
|
return n, err
|
|
}
|
|
|
|
func doRequest(request *http.Request) *http.Response {
|
|
resp, err := client.Do(request)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
// Use `resp.ContentLength` for now, copying the whole body into memory uses a lot of memory lol!
|
|
// size, _ := httputil.DumpResponse(resp, true)
|
|
// atomic.AddInt64(&totalBandwidth.TotalDownload, int64(len(size)))
|
|
atomic.AddInt64(&totalBandwidth.TotalDownload, resp.ContentLength)
|
|
|
|
return resp
|
|
}
|
|
|
|
func videoplayback(w http.ResponseWriter, req *http.Request) {
|
|
w_ := &ResponseWriterWrapper{ResponseWriter: w}
|
|
q := req.URL.Query()
|
|
host := q.Get("host")
|
|
q.Del("host")
|
|
|
|
if len(host) <= 0 {
|
|
mvi := q.Get("mvi")
|
|
mn := strings.Split(q.Get("mn"), ",")
|
|
|
|
if len(mvi) <= 0 {
|
|
io.WriteString(w, "No `mvi` in query parameters")
|
|
return
|
|
}
|
|
|
|
if len(mn) <= 0 {
|
|
io.WriteString(w, "No `mn` in query parameters")
|
|
return
|
|
}
|
|
|
|
host = "rr" + mvi + "---" + mn[0] + ".googlevideo.com"
|
|
}
|
|
|
|
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()
|
|
|
|
proxyURL, err := url.Parse("https://" + host + path)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
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
|
|
|
|
request, err := http.NewRequest("POST", proxyURL.String(), bytes.NewReader(body))
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
copyHeaders(req.Header, request.Header, false)
|
|
request.Header.Set("User-Agent", ua)
|
|
|
|
resp := doRequest(request)
|
|
|
|
if resp.StatusCode == 403 {
|
|
atomic.AddInt64(&stats_.RequestsForbidden.Videoplayback, 1)
|
|
io.WriteString(w, "Forbidden 403\n")
|
|
io.WriteString(w, "Maybe Youtube blocked the IP of this proxy?\n")
|
|
return
|
|
}
|
|
|
|
NoRewrite := strings.HasPrefix(resp.Header.Get("Content-Type"), "audio") || strings.HasPrefix(resp.Header.Get("Content-Type"), "video")
|
|
copyHeaders(resp.Header, w.Header(), NoRewrite)
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
if req.Method == "GET" && (resp.Header.Get("Content-Type") == "application/x-mpegurl" || resp.Header.Get("Content-Type") == "application/vnd.apple.mpegurl") {
|
|
bytes, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
lines := strings.Split(string(bytes), "\n")
|
|
reqUrl := resp.Request.URL
|
|
for i := 0; i < len(lines); i++ {
|
|
line := lines[i]
|
|
if !strings.HasPrefix(line, "https://") && (strings.HasSuffix(line, ".m3u8") || strings.HasSuffix(line, ".ts")) {
|
|
path := reqUrl.EscapedPath()
|
|
path = path[0 : strings.LastIndex(path, "/")+1]
|
|
line = "https://" + reqUrl.Hostname() + path + line
|
|
}
|
|
if strings.HasPrefix(line, "https://") {
|
|
lines[i] = RelativeUrl(line)
|
|
}
|
|
|
|
if manifest_re.MatchString(line) {
|
|
url := manifest_re.FindStringSubmatch(line)[1]
|
|
lines[i] = strings.Replace(line, url, RelativeUrl(url), 1)
|
|
}
|
|
}
|
|
|
|
io.WriteString(w, strings.Join(lines, "\n"))
|
|
} else {
|
|
io.Copy(w_, resp.Body)
|
|
atomic.AddInt64(&totalBandwidth.TotalUpload, int64(w_.Size()))
|
|
}
|
|
}
|
|
|
|
func vi(w http.ResponseWriter, req *http.Request) {
|
|
const host string = "i.ytimg.com"
|
|
q := req.URL.Query()
|
|
|
|
path := req.URL.EscapedPath()
|
|
|
|
proxyURL, err := url.Parse("https://" + host + path)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
if strings.HasSuffix(proxyURL.EscapedPath(), "maxres.jpg") {
|
|
proxyURL.Path = getBestThumbnail(proxyURL.EscapedPath())
|
|
}
|
|
|
|
/*
|
|
Required for /sb/ endpoints
|
|
You can't access https://i.ytimg.com/sb/<VIDEOID>/storyboard3_L2/M3.jpg
|
|
without it's parameters `sqp` and `sigh`
|
|
*/
|
|
proxyURL.RawQuery = q.Encode()
|
|
|
|
request, err := http.NewRequest(req.Method, proxyURL.String(), nil)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
copyHeaders(req.Header, request.Header, false)
|
|
request.Header.Set("User-Agent", ua)
|
|
|
|
resp := doRequest(request)
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
if resp.StatusCode == 403 {
|
|
atomic.AddInt64(&stats_.RequestsForbidden.Vi, 1)
|
|
io.WriteString(w, "Forbidden 403")
|
|
return
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
NoRewrite := strings.HasPrefix(resp.Header.Get("Content-Type"), "audio") || strings.HasPrefix(resp.Header.Get("Content-Type"), "video")
|
|
copyHeaders(resp.Header, w.Header(), NoRewrite)
|
|
|
|
io.Copy(w, resp.Body)
|
|
}
|
|
|
|
func ggpht(w http.ResponseWriter, req *http.Request) {
|
|
const host string = "yt3.ggpht.com"
|
|
|
|
path := req.URL.EscapedPath()
|
|
path = strings.Replace(path, "/ggpht", "", 1)
|
|
path = strings.Replace(path, "/i/", "/", 1)
|
|
|
|
proxyURL, err := url.Parse("https://" + host + path)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
request, err := http.NewRequest(req.Method, proxyURL.String(), nil)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
copyHeaders(req.Header, request.Header, false)
|
|
request.Header.Set("User-Agent", ua)
|
|
|
|
resp := doRequest(request)
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
if resp.StatusCode == 403 {
|
|
atomic.AddInt64(&stats_.RequestsForbidden.Ggpht, 1)
|
|
io.WriteString(w, "Forbidden 403")
|
|
return
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
NoRewrite := strings.HasPrefix(resp.Header.Get("Content-Type"), "audio") || strings.HasPrefix(resp.Header.Get("Content-Type"), "video")
|
|
copyHeaders(resp.Header, w.Header(), NoRewrite)
|
|
|
|
io.Copy(w, resp.Body)
|
|
}
|