Set api client token statically, unrestrict client dynamically and download client nothing

This commit is contained in:
Ben Adrian Sarmiento
2024-06-29 05:17:35 +02:00
parent 21814baf31
commit 50caa2c088
4 changed files with 35 additions and 15 deletions

View File

@@ -61,8 +61,8 @@ func MainApp(configPath string) {
proxyURL = os.Getenv("PROXY") proxyURL = os.Getenv("PROXY")
} }
repoClient4 := http.NewHTTPClient(0, 1, false, []string{}, proxyURL, log.Named("network_test")) repoClient4 := http.NewHTTPClient("", 0, 1, false, []string{}, proxyURL, log.Named("network_test"))
repoClient6 := http.NewHTTPClient(0, 1, true, []string{}, proxyURL, log.Named("network_test")) repoClient6 := http.NewHTTPClient("", 0, 1, true, []string{}, proxyURL, log.Named("network_test"))
repo := http.NewIPRepository(repoClient4, repoClient6, "", log.Named("network_test")) repo := http.NewIPRepository(repoClient4, repoClient6, "", log.Named("network_test"))
var hosts []string var hosts []string
@@ -84,6 +84,7 @@ func MainApp(configPath string) {
} }
apiClient := http.NewHTTPClient( apiClient := http.NewHTTPClient(
config.GetToken(),
config.GetRetriesUntilFailed(), // default retries = 2 config.GetRetriesUntilFailed(), // default retries = 2
config.GetApiTimeoutSecs(), // default api timeout = 60 config.GetApiTimeoutSecs(), // default api timeout = 60
false, // no need for ipv6 support false, // no need for ipv6 support
@@ -93,6 +94,7 @@ func MainApp(configPath string) {
) )
unrestrictClient := http.NewHTTPClient( unrestrictClient := http.NewHTTPClient(
"",
config.GetRetriesUntilFailed(), // default retries = 2 config.GetRetriesUntilFailed(), // default retries = 2
config.GetDownloadTimeoutSecs(), // default download timeout = 10 config.GetDownloadTimeoutSecs(), // default download timeout = 10
false, // no need for ipv6 support false, // no need for ipv6 support
@@ -102,6 +104,7 @@ func MainApp(configPath string) {
) )
downloadClient := http.NewHTTPClient( downloadClient := http.NewHTTPClient(
"",
config.GetRetriesUntilFailed(), config.GetRetriesUntilFailed(),
config.GetDownloadTimeoutSecs(), config.GetDownloadTimeoutSecs(),
config.ShouldForceIPv6(), config.ShouldForceIPv6(),

View File

@@ -1,9 +1,11 @@
package torrent package torrent
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"io" "io"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -207,6 +209,25 @@ func (t *TorrentManager) writeTorrentToFile(torrent *Torrent) {
// t.log.Debugf("Saved torrent %s to file", t.GetKey(torrent)) // t.log.Debugf("Saved torrent %s to file", t.GetKey(torrent))
} }
func (t *TorrentManager) sendTorrentToAPI(torrent *Torrent) {
torrent.Version = t.requiredVersion
jsonData, err := json.Marshal(torrent)
if err != nil {
return
}
req, err := http.NewRequest(
"POST",
"https://zurgtorrent.debridmediamanager.com/api/torrents",
bytes.NewBuffer(jsonData),
)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
client.Do(req)
}
func (t *TorrentManager) applyMediaInfoDetails(torrent *Torrent) error { func (t *TorrentManager) applyMediaInfoDetails(torrent *Torrent) error {
changesApplied := false changesApplied := false
bwLimitReached := false bwLimitReached := false
@@ -241,6 +262,7 @@ func (t *TorrentManager) applyMediaInfoDetails(torrent *Torrent) error {
}) })
if changesApplied { if changesApplied {
t.writeTorrentToFile(torrent) t.writeTorrentToFile(torrent)
t.sendTorrentToAPI(torrent)
} }
if bwLimitReached { if bwLimitReached {
t.log.Warnf("Your account has reached the bandwidth limit, cannot apply media info details to the rest of the files") t.log.Warnf("Your account has reached the bandwidth limit, cannot apply media info details to the rest of the files")

View File

@@ -12,7 +12,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"sync/atomic"
"time" "time"
"github.com/debridmediamanager/zurg/pkg/logutil" "github.com/debridmediamanager/zurg/pkg/logutil"
@@ -23,12 +22,12 @@ import (
) )
type HTTPClient struct { type HTTPClient struct {
token string
client *http.Client client *http.Client
maxRetries int maxRetries int
timeoutSecs int timeoutSecs int
rateLimitSleepSecs int rateLimitSleepSecs int
backoff func(attempt int) time.Duration backoff func(attempt int) time.Duration
token atomic.Value
dnsCache cmap.ConcurrentMap[string, string] dnsCache cmap.ConcurrentMap[string, string]
hosts []string hosts []string
log *logutil.Logger log *logutil.Logger
@@ -53,6 +52,7 @@ func (e *DownloadErrorResponse) Error() string {
} }
func NewHTTPClient( func NewHTTPClient(
token string,
maxRetries int, maxRetries int,
timeoutSecs int, timeoutSecs int,
forceIPv6 bool, forceIPv6 bool,
@@ -61,6 +61,7 @@ func NewHTTPClient(
log *logutil.Logger, log *logutil.Logger,
) *HTTPClient { ) *HTTPClient {
client := HTTPClient{ client := HTTPClient{
token: token,
client: &http.Client{}, client: &http.Client{},
maxRetries: maxRetries, maxRetries: maxRetries,
timeoutSecs: timeoutSecs, timeoutSecs: timeoutSecs,
@@ -127,14 +128,9 @@ func NewHTTPClient(
return &client return &client
} }
func (r *HTTPClient) SetToken(token string) {
r.token.Store(token)
}
func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) { func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) {
token := r.token.Load() if r.token != "" {
if token != nil && token.(string) != "" { req.Header.Set("Authorization", "Bearer "+r.token)
req.Header.Set("Authorization", "Bearer "+token.(string))
} }
var resp *http.Response var resp *http.Response

View File

@@ -49,8 +49,6 @@ func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPCli
log: log, log: log,
} }
apiClient.SetToken(mainToken)
unrestrictClient.SetToken(mainToken)
for _, token := range downloadTokens { for _, token := range downloadTokens {
rd.UnrestrictMap.Set(token, cmap.New[*Download]()) rd.UnrestrictMap.Set(token, cmap.New[*Download]())
} }
@@ -88,7 +86,7 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) {
return download, nil return download, nil
} }
download, err := rd.UnrestrictLink(link) download, err := rd.UnrestrictLinkWithToken(link, token)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -111,7 +109,7 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) {
} }
} }
func (rd *RealDebrid) UnrestrictLink(link string) (*Download, error) { func (rd *RealDebrid) UnrestrictLinkWithToken(link, token string) (*Download, error) {
data := url.Values{} data := url.Values{}
if strings.HasPrefix(link, "https://real-debrid.com/d/") { if strings.HasPrefix(link, "https://real-debrid.com/d/") {
// set link to max 39 chars (26 + 13) // set link to max 39 chars (26 + 13)
@@ -126,6 +124,7 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*Download, error) {
return nil, err return nil, err
} }
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// at this point, any errors mean that the link has expired and we need to repair it // at this point, any errors mean that the link has expired and we need to repair it