From 769c4a6803f7a3e58a86e8e7abddc3958eff9a0e Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sun, 7 Jan 2024 16:17:31 +0100 Subject: [PATCH] Fix location of pref hosts injection --- internal/torrent/manager.go | 22 ++++++++++++++++++++++ internal/universal/get.go | 14 -------------- pkg/http/client.go | 7 ------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index faafe43..6b04bbf 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -2,6 +2,7 @@ package torrent import ( "io" + "net/url" "os" "path/filepath" "strings" @@ -71,6 +72,12 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p } for i := range downloads { 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]) } } @@ -114,6 +121,12 @@ func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download { } ret := t.Api.UnrestrictUntilOk(link, t.Config.ShouldServeFromRclone()) 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) } return ret @@ -231,3 +244,12 @@ func (t *TorrentManager) deleteTorrentFile(torrentID string) { 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() +} diff --git a/internal/universal/get.go b/internal/universal/get.go index 9a463cb..f0c8fc3 100644 --- a/internal/universal/get.go +++ b/internal/universal/get.go @@ -3,7 +3,6 @@ package universal import ( "io" "net/http" - "net/url" "path/filepath" "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) { - prefHost := cfg.GetRandomPreferredHost() - if prefHost != "" { - url = replaceHostInURL(url, prefHost) - } 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() -} diff --git a/pkg/http/client.go b/pkg/http/client.go index 873d52f..b2c6dd3 100644 --- a/pkg/http/client.go +++ b/pkg/http/client.go @@ -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) { - 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 != "" { req.Header.Set("Authorization", "Bearer "+r.bearerToken) }