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

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