Handle preferred hosts in redirects too

This commit is contained in:
Ben Sarmiento
2023-11-24 22:03:31 +01:00
parent 595040ad7e
commit cbd291400f
2 changed files with 23 additions and 6 deletions

View File

@@ -3,6 +3,7 @@ package universal
import ( import (
"io" "io"
"net/http" "net/http"
"net/url"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -75,7 +76,7 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
if url, exists := cache.Get(requestPath); exists { if url, exists := cache.Get(requestPath); exists {
if c.ShouldServeFromRclone() { if c.ShouldServeFromRclone() {
http.Redirect(w, r, url, http.StatusFound) redirect(w, r, url, c)
} else { } else {
gf.streamFileToResponse(file, url, w, r, t, c, log) gf.streamFileToResponse(file, url, w, r, t, c, log)
} }
@@ -112,7 +113,7 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
} }
cache.Add(requestPath, resp.Download) cache.Add(requestPath, resp.Download)
if c.ShouldServeFromRclone() { if c.ShouldServeFromRclone() {
http.Redirect(w, r, resp.Download, http.StatusFound) redirect(w, r, resp.Download, c)
} else { } else {
gf.streamFileToResponse(file, resp.Download, w, r, t, c, log) gf.streamFileToResponse(file, resp.Download, w, r, t, c, log)
} }
@@ -174,8 +175,24 @@ func (gf *GetFile) playErrorVideo(link string, w http.ResponseWriter, r *http.Re
return return
} }
if c.ShouldServeFromRclone() { if c.ShouldServeFromRclone() {
http.Redirect(w, r, resp.Download, http.StatusFound) redirect(w, r, resp.Download, c)
return
} }
gf.streamFileToResponse(nil, resp.Download, w, r, t, c, log) gf.streamFileToResponse(nil, resp.Download, w, r, t, c, log)
} }
func redirect(w http.ResponseWriter, r *http.Request, url string, c config.ConfigInterface) {
prefHost := c.GetRandomPreferredHost()
if prefHost != "" {
url = replaceHostInURL(url, prefHost)
}
http.Redirect(w, r, url, http.StatusFound)
}
func replaceHostInURL(inputURL string, newHost string) string {
u, err := url.Parse(inputURL)
if err != nil {
return ""
}
u.Host = newHost
return u.String()
}

View File

@@ -23,8 +23,8 @@ type HTTPClient struct {
func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) { func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
if r.config != nil && strings.Contains(req.Host, "download.real-debrid.") { if r.config != nil && strings.Contains(req.Host, "download.real-debrid.") {
prefHost := r.config.GetRandomPreferredHost() prefHost := r.config.GetRandomPreferredHost()
if host := prefHost; host != "" { if prefHost != "" {
req.Host = r.config.GetRandomPreferredHost() req.Host = prefHost
} }
} }
if r.BearerToken != "" { if r.BearerToken != "" {