Fix location of pref hosts injection

This commit is contained in:
Ben Sarmiento
2024-01-07 16:17:31 +01:00
parent 120df54f35
commit 769c4a6803
3 changed files with 22 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ package torrent
import ( import (
"io" "io"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -71,6 +72,12 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
} }
for i := range downloads { for i := range downloads {
if !t.DownloadCache.Has(downloads[i].Link) { if !t.DownloadCache.Has(downloads[i].Link) {
if strings.Contains(downloads[i].Download, "download.real-debrid.") {
prefHost := t.Config.GetRandomPreferredHost()
if prefHost != "" {
downloads[i].Download = replaceHostInURL(downloads[i].Download, prefHost)
}
}
t.DownloadCache.Set(downloads[i].Link, &downloads[i]) t.DownloadCache.Set(downloads[i].Link, &downloads[i])
} }
} }
@@ -114,6 +121,12 @@ func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download {
} }
ret := t.Api.UnrestrictUntilOk(link, t.Config.ShouldServeFromRclone()) ret := t.Api.UnrestrictUntilOk(link, t.Config.ShouldServeFromRclone())
if ret != nil { if ret != nil {
if strings.Contains(ret.Download, "download.real-debrid.") {
prefHost := t.Config.GetRandomPreferredHost()
if prefHost != "" {
ret.Download = replaceHostInURL(ret.Download, prefHost)
}
}
t.DownloadCache.Set(link, ret) t.DownloadCache.Set(link, ret)
} }
return ret return ret
@@ -231,3 +244,12 @@ func (t *TorrentManager) deleteTorrentFile(torrentID string) {
t.log.Warnf("Cannot delete file %s: %v", filePath, err) t.log.Warnf("Cannot delete file %s: %v", filePath, err)
} }
} }
func replaceHostInURL(inputURL string, newHost string) string {
u, err := url.Parse(inputURL)
if err != nil {
return ""
}
u.Host = newHost
return u.String()
}

View File

@@ -3,7 +3,6 @@ package universal
import ( import (
"io" "io"
"net/http" "net/http"
"net/url"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -151,18 +150,5 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi
} }
func redirect(resp http.ResponseWriter, req *http.Request, url string, cfg config.ConfigInterface) { func redirect(resp http.ResponseWriter, req *http.Request, url string, cfg config.ConfigInterface) {
prefHost := cfg.GetRandomPreferredHost()
if prefHost != "" {
url = replaceHostInURL(url, prefHost)
}
http.Redirect(resp, req, url, http.StatusFound) http.Redirect(resp, req, 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

@@ -105,13 +105,6 @@ func NewHTTPClient(token string, maxRetries int, timeoutSecs int, cfg config.Con
} }
func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) { func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
if r.cfg != nil && strings.Contains(req.Host, "download.real-debrid.") {
prefHost := r.cfg.GetRandomPreferredHost()
if prefHost != "" {
req.Host = prefHost
req.URL.Host = prefHost
}
}
if r.bearerToken != "" { if r.bearerToken != "" {
req.Header.Set("Authorization", "Bearer "+r.bearerToken) req.Header.Set("Authorization", "Bearer "+r.bearerToken)
} }