Proper token rotation
This commit is contained in:
@@ -383,24 +383,29 @@ 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/") {
|
||||||
filename := filepath.Base(downloads[i].Filename)
|
downloads[i].Link = downloads[i].Link[0:39]
|
||||||
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 != "" {
|
|
||||||
tokenMap.Set(downloads[i].Link, &downloads[i])
|
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 {
|
if mountedCount > 0 {
|
||||||
t.log.Infof("Mounted %d new downloads", mountedCount)
|
t.log.Infof("Mounted %d new downloads", mountedCount)
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user