From 99ca9d260258772e6c5451912b6ee87ddc59adab Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Mon, 1 Jul 2024 03:22:50 +0200 Subject: [PATCH] Proper token rotation --- internal/torrent/manager.go | 37 +++++++++++++++++++++---------------- pkg/realdebrid/api.go | 11 +++++++---- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index a61253f..7074ecb 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -383,24 +383,29 @@ func (t *TorrentManager) mountNewDownloads() { mountedCount := 0 for i := range downloads { downloads[i].Token = token - isRealDebrid := strings.HasPrefix(downloads[i].Link, "https://real-debrid.com/d/") - if !isRealDebrid { - filename := filepath.Base(downloads[i].Filename) - if strings.Contains(downloads[i].Type, "x") { - // extract extension from the filename - ext := filepath.Ext(filename) - trimmed := strings.TrimSuffix(filename, ext) - // it's a resolution so extract 2nd part and add it to the filename - parts := strings.Split(downloads[i].Type, "x") - if len(parts) > 1 { - filename = fmt.Sprintf("%s (%sp)%s", trimmed, parts[1], ext) - } - } - t.DownloadMap.Set(filename, &downloads[i]) - mountedCount++ - } else if token != "" { + + if strings.HasPrefix(downloads[i].Link, "https://real-debrid.com/d/") { + downloads[i].Link = downloads[i].Link[0:39] tokenMap.Set(downloads[i].Link, &downloads[i]) + continue } + + filename := filepath.Base(downloads[i].Filename) + + // account for resolution in the type + if strings.Contains(downloads[i].Type, "x") { + // extract extension from the filename + ext := filepath.Ext(filename) + trimmed := strings.TrimSuffix(filename, ext) + // it's a resolution so extract 2nd part and add it to the filename + parts := strings.Split(downloads[i].Type, "x") + if len(parts) > 1 { + filename = fmt.Sprintf("%s (%sp)%s", trimmed, parts[1], ext) + } + } + + t.DownloadMap.Set(filename, &downloads[i]) + mountedCount++ } if mountedCount > 0 { t.log.Infof("Mounted %d new downloads", mountedCount) diff --git a/pkg/realdebrid/api.go b/pkg/realdebrid/api.go index 2cdd1f6..987b9e6 100644 --- a/pkg/realdebrid/api.go +++ b/pkg/realdebrid/api.go @@ -56,6 +56,8 @@ func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPCli return rd } +const DOWNLOAD_LINK_EXPIRY = 60 * 60 * 24 + func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) { for { token, err := rd.TokenManager.GetCurrentToken() @@ -71,6 +73,8 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) { // check if the link is in the verified links cache if expiry, ok := rd.verifiedLinks.Get(download.ID); ok && expiry > time.Now().Unix() { return download, nil + } else if ok { + rd.verifiedLinks.Remove(download.ID) } err := rd.downloadClient.VerifyLink(download.Download) @@ -79,10 +83,9 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) { continue } if err == nil { - rd.verifiedLinks.Set(download.ID, time.Now().Unix()+60*60*24) + rd.verifiedLinks.Set(download.ID, time.Now().Unix()+DOWNLOAD_LINK_EXPIRY) return download, nil } - rd.verifiedLinks.Remove(download.ID) tokenMap.Remove(link) } @@ -103,7 +106,7 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) { return nil, err } - rd.verifiedLinks.Set(download.ID, time.Now().Unix()+60*60*24) + rd.verifiedLinks.Set(download.ID, time.Now().Unix()+DOWNLOAD_LINK_EXPIRY) return download, err } @@ -405,7 +408,7 @@ func (rd *RealDebrid) DownloadFile(req *http.Request) (*http.Response, error) { // MonitorExpiredTokens is a permanent job for monitoring expired tokens if they are still expired func (rd *RealDebrid) MonitorExpiredTokens() { - sleepPeriod := 5 * time.Minute + sleepPeriod := 1 * time.Minute rd.workerPool.Submit(func() { for { expiredTokens := rd.TokenManager.GetExpiredTokens()