Proper token rotation

This commit is contained in:
Ben Adrian Sarmiento
2024-07-01 03:22:50 +02:00
parent 9fdda47639
commit 99ca9d2602
2 changed files with 28 additions and 20 deletions

View File

@@ -383,9 +383,16 @@ func (t *TorrentManager) mountNewDownloads() {
mountedCount := 0 mountedCount := 0
for i := range downloads { for i := range downloads {
downloads[i].Token = token downloads[i].Token = token
isRealDebrid := strings.HasPrefix(downloads[i].Link, "https://real-debrid.com/d/")
if !isRealDebrid { 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) filename := filepath.Base(downloads[i].Filename)
// account for resolution in the type
if strings.Contains(downloads[i].Type, "x") { if strings.Contains(downloads[i].Type, "x") {
// extract extension from the filename // extract extension from the filename
ext := filepath.Ext(filename) ext := filepath.Ext(filename)
@@ -396,11 +403,9 @@ func (t *TorrentManager) mountNewDownloads() {
filename = fmt.Sprintf("%s (%sp)%s", trimmed, parts[1], ext) filename = fmt.Sprintf("%s (%sp)%s", trimmed, parts[1], ext)
} }
} }
t.DownloadMap.Set(filename, &downloads[i]) t.DownloadMap.Set(filename, &downloads[i])
mountedCount++ mountedCount++
} else if token != "" {
tokenMap.Set(downloads[i].Link, &downloads[i])
}
} }
if mountedCount > 0 { if mountedCount > 0 {
t.log.Infof("Mounted %d new downloads", mountedCount) t.log.Infof("Mounted %d new downloads", mountedCount)

View File

@@ -56,6 +56,8 @@ func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPCli
return rd return rd
} }
const DOWNLOAD_LINK_EXPIRY = 60 * 60 * 24
func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) { func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) {
for { for {
token, err := rd.TokenManager.GetCurrentToken() 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 // check if the link is in the verified links cache
if expiry, ok := rd.verifiedLinks.Get(download.ID); ok && expiry > time.Now().Unix() { if expiry, ok := rd.verifiedLinks.Get(download.ID); ok && expiry > time.Now().Unix() {
return download, nil return download, nil
} else if ok {
rd.verifiedLinks.Remove(download.ID)
} }
err := rd.downloadClient.VerifyLink(download.Download) err := rd.downloadClient.VerifyLink(download.Download)
@@ -79,10 +83,9 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) {
continue continue
} }
if err == nil { 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 return download, nil
} }
rd.verifiedLinks.Remove(download.ID)
tokenMap.Remove(link) tokenMap.Remove(link)
} }
@@ -103,7 +106,7 @@ func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) {
return nil, err 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 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 // MonitorExpiredTokens is a permanent job for monitoring expired tokens if they are still expired
func (rd *RealDebrid) MonitorExpiredTokens() { func (rd *RealDebrid) MonitorExpiredTokens() {
sleepPeriod := 5 * time.Minute sleepPeriod := 1 * time.Minute
rd.workerPool.Submit(func() { rd.workerPool.Submit(func() {
for { for {
expiredTokens := rd.TokenManager.GetExpiredTokens() expiredTokens := rd.TokenManager.GetExpiredTokens()