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,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)

View File

@@ -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()