Add token management

This commit is contained in:
Ben Adrian Sarmiento
2024-06-28 13:19:09 +02:00
parent 962845fb81
commit c3aea427d0
9 changed files with 100 additions and 114 deletions

View File

@@ -121,7 +121,7 @@ func MainApp(configPath string) {
}
defer workerPool.Release()
api := realdebrid.NewRealDebrid(
rd := realdebrid.NewRealDebrid(
apiClient,
unrestrictClient,
downloadClient,
@@ -132,7 +132,7 @@ func MainApp(configPath string) {
premium.MonitorPremiumStatus(
workerPool,
api,
rd,
zurglog,
)
@@ -145,14 +145,14 @@ func MainApp(configPath string) {
torrentMgr := torrent.NewTorrentManager(
config,
api,
rd,
workerPool,
hasFFprobe,
log.Named("manager"),
log.Named("repair"),
)
downloader := universal.NewDownloader(downloadClient, workerPool)
downloader := universal.NewDownloader(rd, workerPool)
router := chi.NewRouter()
handlers.AttachHandlers(
@@ -160,7 +160,7 @@ func MainApp(configPath string) {
downloader,
torrentMgr,
config,
api,
rd,
workerPool,
hosts,
log.Named("router"),

View File

@@ -47,7 +47,7 @@ type RootResponse struct {
}
func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request) (*RootResponse, error) {
userInfo, err := zr.api.GetUserInformation()
userInfo, err := zr.rd.GetUserInformation()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return nil, err
@@ -77,7 +77,7 @@ func (zr *Handlers) generateResponse(resp http.ResponseWriter, req *http.Request
sortedIDs := zr.torMgr.OnceDoneBin.ToSlice()
sort.Strings(sortedIDs)
trafficDetails, err := zr.api.GetTrafficDetails()
trafficDetails, err := zr.rd.GetTrafficDetails()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return nil, err

View File

@@ -24,7 +24,7 @@ type Handlers struct {
downloader *universal.Downloader
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
rd *realdebrid.RealDebrid
workerPool *ants.Pool
hosts []string
trafficOnStartup atomic.Uint64
@@ -37,18 +37,18 @@ func init() {
chi.RegisterMethod("MOVE")
}
func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, workerPool *ants.Pool, hosts []string, log *logutil.Logger) {
func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, rd *realdebrid.RealDebrid, workerPool *ants.Pool, hosts []string, log *logutil.Logger) {
hs := &Handlers{
downloader: downloader,
torMgr: torMgr,
cfg: cfg,
api: api,
rd: rd,
workerPool: workerPool,
hosts: hosts,
log: log,
}
trafficDetails, err := api.GetTrafficDetails()
trafficDetails, err := rd.GetTrafficDetails()
if err != nil {
log.Errorf("Failed to get traffic details: %v", err)
trafficDetails = make(map[string]int64)

View File

@@ -12,7 +12,6 @@ import (
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/fs"
"github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/debridmediamanager/zurg/pkg/utils"
@@ -144,7 +143,7 @@ func (t *TorrentManager) UnrestrictFile(file *File) (*realdebrid.Download, error
} else if file.State.Is("broken_file") {
return nil, fmt.Errorf("file %s is broken", file.Path)
}
return t.rd.UnrestrictLink(file.Link)
return t.rd.UnrestrictAndVerify(file.Link)
}
func (t *TorrentManager) GetKey(torrent *Torrent) string {
@@ -220,7 +219,7 @@ func (t *TorrentManager) applyMediaInfoDetails(torrent *Torrent) error {
return
}
unrestrict, err := t.UnrestrictFile(file)
if dlErr, ok := err.(*http.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
if utils.IsBWLimitExceeded(err) {
bwLimitReached = true
return
}
@@ -332,11 +331,8 @@ func (t *TorrentManager) deleteInfoFile(torrentID string) {
/// end info functions
func (t *TorrentManager) mountNewDownloads() {
token, _ := t.rd.GetToken()
var tokenMap cmap.ConcurrentMap[string, *realdebrid.Download]
if token != "" {
tokenMap, _ = t.rd.UnrestrictMap.Get(token)
}
token := t.Config.GetToken()
tokenMap, _ := t.rd.UnrestrictMap.Get(token)
downloads := t.rd.GetDownloads()
mountedCount := 0

View File

@@ -9,7 +9,6 @@ import (
"time"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/debridmediamanager/zurg/pkg/utils"
mapset "github.com/deckarep/golang-set/v2"
@@ -197,7 +196,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
return
}
_, err := t.UnrestrictFile(file)
if dlErr, ok := err.(*http.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
if utils.IsBWLimitExceeded(err) {
bwLimitReached = true
return
}
@@ -253,7 +252,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
info, err := t.redownloadTorrent(torrent, []string{}) // reinsert the whole torrent, passing empty selection
if info != nil && info.Progress == 100 {
err = t.checkIfBroken(info, brokenFiles)
if dlErr, ok := err.(*http.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
if utils.IsBWLimitExceeded(err) {
t.repairLog.Warnf("Your account has reached the bandwidth limit, cannot continue repairing torrent %s", t.GetKey(torrent))
return
}
@@ -339,8 +338,8 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
bwLimitReached := false
torrent.UnassignedLinks.Clone().Each(func(link string) bool {
// unrestrict each unassigned link that was filled out during torrent init
unrestrict, err := t.rd.UnrestrictLink(link)
if dlErr, ok := err.(*http.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
unrestrict, err := t.rd.UnrestrictAndVerify(link)
if utils.IsBWLimitExceeded(err) {
bwLimitReached = true
return true
}

View File

@@ -13,22 +13,22 @@ import (
"github.com/debridmediamanager/zurg/internal/config"
intTor "github.com/debridmediamanager/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/debridmediamanager/zurg/pkg/utils"
"github.com/panjf2000/ants/v2"
)
type Downloader struct {
client *zurghttp.HTTPClient
rd *realdebrid.RealDebrid
workerPool *ants.Pool
RequestedBytes atomic.Uint64
TotalBytes atomic.Uint64
}
func NewDownloader(client *zurghttp.HTTPClient, workerPool *ants.Pool) *Downloader {
func NewDownloader(rd *realdebrid.RealDebrid, workerPool *ants.Pool) *Downloader {
dl := &Downloader{
client: client,
rd: rd,
workerPool: workerPool,
}
@@ -42,10 +42,12 @@ func NewDownloader(client *zurghttp.HTTPClient, workerPool *ants.Pool) *Download
nextMidnightInCET := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 0, 0, 0, 0, cetTZ)
duration := nextMidnightInCET.Sub(now)
timer := time.NewTimer(duration)
// permanent job for bandwidth reset
workerPool.Submit(func() {
<-timer.C
ticker := time.NewTicker(24 * time.Hour)
for {
rd.TokenManager.ResetAllTokens()
dl.RequestedBytes.Store(0)
dl.TotalBytes.Store(0)
<-ticker.C
@@ -88,7 +90,7 @@ func (dl *Downloader) DownloadFile(
}
unrestrict, err := torMgr.UnrestrictFile(file)
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
if utils.IsBWLimitExceeded(err) {
// log.Errorf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest)
return
@@ -161,8 +163,8 @@ func (dl *Downloader) streamFileToResponse(
dlReq.Header.Add("Range", req.Header.Get("Range"))
}
downloadResp, err := dl.client.Do(dlReq)
if dlErr, ok := err.(*zurghttp.DownloadErrorResponse); ok && dlErr.Message == "bytes_limit_reached" {
downloadResp, err := dl.rd.DownloadFile(dlReq)
if utils.IsBWLimitExceeded(err) {
// log.Errorf("Your account has reached the bandwidth limit, please try again after 12AM CET")
http.Error(resp, "File is not available (bandwidth limit reached)", http.StatusBadRequest)
return