Skip to content

Commit

Permalink
Fix configured URL is not used when an URL is read from a response body
Browse files Browse the repository at this point in the history
  • Loading branch information
doingodswork committed Apr 3, 2020
1 parent f07f0c3 commit cc09259
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/imdb2torrent/1337x.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ func (c leetxClient) check(ctx context.Context, imdbID string) ([]Result, error)
resultChan := make(chan Result, len(torrentPageURLs))

for _, torrentPageURL := range torrentPageURLs {
// Use configured base URL, which could be a proxy that we want to go through
torrentPageURL, err = replaceURL(torrentPageURL, c.baseURL)
if err != nil {
logger.WithError(err).Warn("Couldn't replace URL which was retrieved from an HTML link")
continue
}

go func(goTorrentPageURL string) {
doc, err = c.getDoc(ctx, goTorrentPageURL)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/imdb2torrent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package imdb2torrent
import (
"context"
"fmt"
"net/url"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -207,3 +208,13 @@ type Result struct {
InfoHash string
MagnetURL string
}

func replaceURL(origURL, newBaseURL string) (string, error) {
// Replace by configured URL, which could be a proxy that we want to go through
url, err := url.Parse(origURL)
if err != nil {
return "", fmt.Errorf("Couldn't parse URL. URL: %v; error: %v", origURL, err)
}
origBaseURL := url.Scheme + "://" + url.Host
return strings.Replace(origURL, origBaseURL, newBaseURL, 1), nil
}
7 changes: 7 additions & 0 deletions pkg/imdb2torrent/ibit.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ func (c ibitClient) check(ctx context.Context, imdbID string) ([]Result, error)
// Sleeping 100ms between requests still leads to some `429 Too Many Requests` responses
time.Sleep(150 * time.Millisecond)

// Use configured base URL, which could be a proxy that we want to go through
torrentPageURL, err = replaceURL(torrentPageURL, c.baseURL)
if err != nil {
logger.WithError(err).Warn("Couldn't replace URL which was retrieved from an HTML link")
continue
}

res, err := http.Get(torrentPageURL)
if err != nil {
continue
Expand Down
15 changes: 15 additions & 0 deletions pkg/realdebrid/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ func (c Client) GetStreamURL(ctx context.Context, magnetURL, apiToken string, re
// Check RealDebrid torrent info

logger.Debug("Checking torrent info...")
// Use configured base URL, which could be a proxy that we want to go through
rdTorrentURL, err = replaceURL(rdTorrentURL, c.rdBaseURL)
if err != nil {
return "", fmt.Errorf("Couldn't replace URL which was retrieved from an HTML link: %v", err)
}
resBytes, err = c.get(ctx, rdTorrentURL, apiToken)
if err != nil {
return "", fmt.Errorf("Couldn't get torrent info from real-debrid.com: %v", err)
Expand Down Expand Up @@ -385,3 +390,13 @@ func selectFileID(ctx context.Context, fileResults []gjson.Result) (string, erro

return strconv.FormatInt(fileID, 10), nil
}

func replaceURL(origURL, newBaseURL string) (string, error) {
// Replace by configured URL, which could be a proxy that we want to go through
url, err := url.Parse(origURL)
if err != nil {
return "", fmt.Errorf("Couldn't parse URL. URL: %v; error: %v", origURL, err)
}
origBaseURL := url.Scheme + "://" + url.Host
return strings.Replace(origURL, origBaseURL, newBaseURL, 1), nil
}

0 comments on commit cc09259

Please sign in to comment.