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

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

View File

@@ -49,8 +49,6 @@ func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPCli
log: log,
}
apiClient.SetToken(mainToken)
unrestrictClient.SetToken(mainToken)
for _, token := range downloadTokens {
rd.UnrestrictMap.Set(token, cmap.New[*Download]())
}
@@ -88,7 +86,7 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) {
return download, nil
}
download, err := rd.UnrestrictLink(link)
download, err := rd.UnrestrictLinkWithToken(link, token)
if err != nil {
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{}
if strings.HasPrefix(link, "https://real-debrid.com/d/") {
// set link to max 39 chars (26 + 13)
@@ -126,6 +124,7 @@ func (rd *RealDebrid) UnrestrictLink(link string) (*Download, error) {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+token)
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