package torrent import ( "bufio" "encoding/gob" "fmt" "hash/fnv" "math" "os" "path/filepath" "strings" "sync" "time" "github.com/debridmediamanager.com/zurg/internal/config" "github.com/debridmediamanager.com/zurg/pkg/logutil" "github.com/debridmediamanager.com/zurg/pkg/realdebrid" "github.com/debridmediamanager.com/zurg/pkg/utils" cmap "github.com/orcaman/concurrent-map/v2" "github.com/panjf2000/ants/v2" "go.uber.org/zap" ) const ( ALL_TORRENTS = "__all__" DATA_DIR = "data" ) type TorrentManager struct { DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent checksum string requiredVersion string cfg config.ConfigInterface api *realdebrid.RealDebrid antsPool *ants.Pool log *zap.SugaredLogger mu *sync.Mutex } // NewTorrentManager creates a new torrent manager // it will fetch all torrents and their info in the background // and store them in-memory and cached in files func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p *ants.Pool) *TorrentManager { t := &TorrentManager{ cfg: cfg, DirectoryMap: cmap.New[cmap.ConcurrentMap[string, *Torrent]](), requiredVersion: "18.11.2023", api: api, antsPool: p, log: logutil.NewLogger().Named("manager"), mu: &sync.Mutex{}, } ensureDir(DATA_DIR) // create special directory t.DirectoryMap.Set("__all__", cmap.New[*Torrent]()) // key is AccessKey // create directory maps for _, directory := range cfg.GetDirectories() { t.DirectoryMap.Set(directory, cmap.New[*Torrent]()) } newTorrents, _, err := t.api.GetTorrents(0) if err != nil { t.log.Fatalf("Cannot get torrents: %v\n", err) } torrentsChan := make(chan *Torrent, len(newTorrents)) var wg sync.WaitGroup for i := range newTorrents { wg.Add(1) go func(idx int) { _ = t.antsPool.Submit(func() { defer wg.Done() torrentsChan <- t.getMoreInfo(newTorrents[idx]) }) }(i) } wg.Wait() close(torrentsChan) t.log.Infof("Fetched info for %d torrents", len(newTorrents)) noInfoCount := 0 allCt := 0 allTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS) for info := range torrentsChan { allCt++ if info == nil { noInfoCount++ continue } if torrent, exists := allTorrents.Get(info.AccessKey); exists { mainTorrent := t.mergeToMain(torrent, info) allTorrents.Set(info.AccessKey, mainTorrent) } else { allTorrents.Set(info.AccessKey, info) } } allTorrents.IterCb(func(accessKey string, torrent *Torrent) { // get IDs var torrentIDs []string for _, instance := range torrent.Instances { torrentIDs = append(torrentIDs, instance.ID) } // get filenames filenames := torrent.SelectedFiles.Keys() // Map torrents to directories switch t.cfg.GetVersion() { case "v1": configV1 := t.cfg.(*config.ZurgConfigV1) for _, directories := range configV1.GetGroupMap() { for _, directory := range directories { if t.cfg.MeetsConditions(directory, torrent.AccessKey, torrentIDs, filenames) { torrents, _ := t.DirectoryMap.Get(directory) torrents.Set(accessKey, torrent) break } } } } }) t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount) t.SetChecksum(t.getChecksum()) if t.cfg.EnableRepair() { t.log.Info("Checking for torrents to repair") t.repairAll() t.log.Info("Finished checking for torrents to repair") } go t.startRefreshJob() return t } func (t *TorrentManager) mergeToMain(mainTorrent, torrentToMerge *Torrent) *Torrent { // Merge SelectedFiles - itercb accesses a different copy of the selectedfiles map torrentToMerge.SelectedFiles.IterCb(func(filepath string, fileToMerge *File) { // see if it already exists in the main torrent if mainFile, ok := mainTorrent.SelectedFiles.Get(filepath); !ok { // if it doesn't exist in the main torrent, add it mainTorrent.SelectedFiles.Set(filepath, fileToMerge) } else { // if it exists, compare the LatestAdded property and the link if mainTorrent.LatestAdded < torrentToMerge.LatestAdded && strings.HasPrefix(fileToMerge.Link, "http") { // if torrentToMerge is more recent and its file has a link, update the main torrent's file mainTorrent.SelectedFiles.Set(filepath, fileToMerge) } else if !strings.HasPrefix(mainFile.Link, "http") && strings.HasPrefix(fileToMerge.Link, "http") { // if the main torrent's file link is empty and t2's file has a link, even if it's older, update it mainTorrent.SelectedFiles.Set(filepath, fileToMerge) } // else do nothing, the main torrent's file is more recent or has a valid link } }) // Merge Instances mainTorrent.Instances = append(mainTorrent.Instances, torrentToMerge.Instances...) // LatestAdded if mainTorrent.LatestAdded < torrentToMerge.LatestAdded { mainTorrent.LatestAdded = torrentToMerge.LatestAdded } return mainTorrent } // proxy func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.UnrestrictResponse { ret := t.api.UnrestrictUntilOk(link) return ret } type torrentsResponse struct { torrents []realdebrid.Torrent totalCount int } func (t *TorrentManager) SetChecksum(checksum string) { t.mu.Lock() t.checksum = checksum t.mu.Unlock() } // generates a checksum based on the number of torrents, the first torrent id and the number of active torrents func (t *TorrentManager) getChecksum() string { torrentsChan := make(chan torrentsResponse, 1) countChan := make(chan int, 1) errChan := make(chan error, 2) // accommodate errors from both goroutines // GetTorrents request go func() { torrents, totalCount, err := t.api.GetTorrents(1) if err != nil { errChan <- err return } torrentsChan <- torrentsResponse{torrents: torrents, totalCount: totalCount} }() // GetActiveTorrentCount request go func() { count, err := t.api.GetActiveTorrentCount() if err != nil { errChan <- err return } countChan <- count.DownloadingCount }() var torrents []realdebrid.Torrent var totalCount, count int for i := 0; i < 2; i++ { select { case torrentsResp := <-torrentsChan: torrents = torrentsResp.torrents totalCount = torrentsResp.totalCount case count = <-countChan: case err := <-errChan: t.log.Warnf("Checksum API Error: %v\n", err) return "" } } if len(torrents) == 0 { t.log.Error("Huh, no torrents returned") return "" } checksum := fmt.Sprintf("%d%s%d", totalCount, torrents[0].ID, count) return checksum } // startRefreshJob periodically refreshes the torrents func (t *TorrentManager) startRefreshJob() { t.log.Info("Starting periodic refresh") for { <-time.After(time.Duration(t.cfg.GetRefreshEverySeconds()) * time.Second) checksum := t.getChecksum() if checksum == t.checksum { continue } newTorrents, _, err := t.api.GetTorrents(0) if err != nil { t.log.Warnf("Cannot get torrents: %v\n", err) continue } t.log.Infof("Detected changes! Refreshing %d torrents", len(newTorrents)) torrentsChan := make(chan *Torrent, len(newTorrents)) var wg sync.WaitGroup for i := range newTorrents { wg.Add(1) go func(idx int) { _ = t.antsPool.Submit(func() { defer wg.Done() torrentsChan <- t.getMoreInfo(newTorrents[idx]) }) }(i) } wg.Wait() close(torrentsChan) t.log.Infof("Fetched info for %d torrents", len(newTorrents)) noInfoCount := 0 allTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS) retain := make(map[string]bool) for info := range torrentsChan { if info == nil { noInfoCount++ continue } retain[info.AccessKey] = true if torrent, exists := allTorrents.Get(info.AccessKey); exists { mainTorrent := t.mergeToMain(torrent, info) allTorrents.Set(info.AccessKey, mainTorrent) } else { allTorrents.Set(info.AccessKey, info) } } allTorrents.IterCb(func(accessKey string, torrent *Torrent) { // get IDs var torrentIDs []string for _, instance := range torrent.Instances { torrentIDs = append(torrentIDs, instance.ID) } // get filenames filenames := torrent.SelectedFiles.Keys() // Map torrents to directories switch t.cfg.GetVersion() { case "v1": configV1 := t.cfg.(*config.ZurgConfigV1) for _, directories := range configV1.GetGroupMap() { for _, directory := range directories { if t.cfg.MeetsConditions(directory, torrent.AccessKey, torrentIDs, filenames) { torrents, _ := t.DirectoryMap.Get(directory) torrents.Set(accessKey, torrent) break } } } } }) // delete torrents that no longer exist accessKeys := allTorrents.Keys() for _, oldAccessKey := range accessKeys { if _, ok := retain[oldAccessKey]; !ok { t.DirectoryMap.IterCb(func(_ string, torrents cmap.ConcurrentMap[string, *Torrent]) { torrents.Remove(oldAccessKey) }) t.log.Infof("Deleted torrent: %s\n", oldAccessKey) } } t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount) t.SetChecksum(t.getChecksum()) if t.cfg.EnableRepair() { t.log.Info("Checking for torrents to repair") t.repairAll() t.log.Info("Finished checking for torrents to repair") } // TODO: pass the changed directories to the hook go OnLibraryUpdateHook([]string{}, t.cfg, t.log) } } // getMoreInfo gets original name, size and files for a torrent func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { var info *realdebrid.TorrentInfo var err error // file cache torrentFromFile := t.readFromFile(rdTorrent.ID) if torrentFromFile != nil && len(torrentFromFile.ID) > 0 && len(torrentFromFile.Links) > 0 && len(torrentFromFile.Links) == len(rdTorrent.Links) && torrentFromFile.Links[0] == rdTorrent.Links[0] { info = torrentFromFile info.Progress = rdTorrent.Progress } else { torrentFromFile = nil } if info == nil { info, err = t.api.GetTorrentInfo(rdTorrent.ID) if err != nil { t.log.Warnf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err) return nil } } // SelectedFiles is a subset of Files with only the selected ones // it also has a Link field, which can be empty // if it is empty, it means the file is no longer available // Files+Links together are the same as SelectedFiles var selectedFiles []*File // if some Links are empty, we need to repair it for _, file := range info.Files { if file.Selected == 0 { continue } selectedFiles = append(selectedFiles, &File{ File: file, Added: info.Added, Ended: info.Ended, Link: "", // no link yet ZurgFS: hashStringToFh(file.Path + info.Hash), }) } if len(selectedFiles) > len(info.Links) && info.Progress == 100 { t.log.Warnf("Torrent id=%s is partly expired, it has %d selected files but only %d links", info.ID, len(selectedFiles), len(info.Links)) } else if len(selectedFiles) == len(info.Links) { // all links are still intact! good! for i, file := range selectedFiles { file.Link = info.Links[i] i++ } } torrent := Torrent{ AccessKey: t.getName(info.Name, info.OriginalName), LatestAdded: info.Added, Instances: []realdebrid.TorrentInfo{*info}, } torrent.SelectedFiles = cmap.New[*File]() for _, file := range selectedFiles { torrent.SelectedFiles.Set(filepath.Base(file.Path), file) } if len(selectedFiles) > 0 && torrentFromFile == nil { t.writeToFile(info) // only when there are selected files, else it's useless } return &torrent } func hashStringToFh(s string) (fh uint64) { hasher := fnv.New64a() _, _ = hasher.Write([]byte(s)) // Write the string to the hasher; ignoring error as it never returns an error return hasher.Sum64() // Returns a 64-bit hash value } func (t *TorrentManager) getName(name, originalName string) string { if t.cfg.EnableRetainRDTorrentName() { return name } // drop the extension from the name if t.cfg.EnableRetainFolderNameExtension() && strings.Contains(name, originalName) { return name } else { ret := strings.TrimSuffix(originalName, ".mp4") ret = strings.TrimSuffix(ret, ".mkv") return ret } } func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error { filePath := DATA_DIR + "/" + torrent.ID + ".bin" file, err := os.Create(filePath) if err != nil { return fmt.Errorf("failed creating file: %w", err) } defer file.Close() w := bufio.NewWriter(file) defer w.Flush() torrent.Version = t.requiredVersion dataEncoder := gob.NewEncoder(w) if err := dataEncoder.Encode(torrent); err != nil { return fmt.Errorf("failed encoding torrent: %w", err) } return nil } func (t *TorrentManager) readFromFile(torrentID string) *realdebrid.TorrentInfo { filePath := DATA_DIR + "/" + torrentID + ".bin" file, err := os.Open(filePath) if err != nil { if os.IsNotExist(err) { return nil } return nil } defer file.Close() r := bufio.NewReader(file) var torrent realdebrid.TorrentInfo dataDecoder := gob.NewDecoder(r) if err := dataDecoder.Decode(&torrent); err != nil { return nil } if torrent.Version != t.requiredVersion { return nil } return &torrent } func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([]*File, bool) { type Result struct { Response *realdebrid.UnrestrictResponse } resultsChan := make(chan Result, len(links)) var wg sync.WaitGroup for _, link := range links { wg.Add(1) link := link // redeclare to avoid closure on loop variable // Use the existing worker pool to submit tasks err := t.antsPool.Submit(func() { defer wg.Done() resp := t.api.UnrestrictUntilOk(link) resultsChan <- Result{Response: resp} }) if err != nil { t.log.Errorf("Error submitting task to worker pool: %v", err) } } go func() { wg.Wait() close(resultsChan) }() isChaotic := false for result := range resultsChan { if result.Response == nil { continue } found := false for _, file := range selectedFiles { if strings.Contains(file.Path, result.Response.Filename) { file.Link = result.Response.Link found = true } } if !found { if result.Response.Streamable == 1 { now := time.Now().Format(time.RFC3339) selectedFiles = append(selectedFiles, &File{ File: realdebrid.File{ ID: math.MaxInt32, Path: result.Response.Filename, Bytes: result.Response.Filesize, Selected: 1, }, Added: now, Ended: now, Link: result.Response.Link, ZurgFS: hashStringToFh(result.Response.Filename), }) } else { isChaotic = true } } } return selectedFiles, isChaotic } func (t *TorrentManager) repairAll() { proceed := t.canCapacityHandle() // blocks for approx 45 minutes if active torrents are full if !proceed { t.log.Error("Reached the max number of active torrents, cannot start repair") return } allTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS) allTorrents.IterCb(func(_ string, torrent *Torrent) { if torrent.InProgress() { t.log.Debugf("Skipping %s for repairs because it is in progress", torrent.AccessKey) return } forRepair := false unselected := 0 torrent.SelectedFiles.IterCb(func(_ string, file *File) { if file.Link == "repair" { t.log.Debugf("Found a file to repair for torrent %s", torrent.AccessKey) forRepair = true } if file.Link == "unselect" { unselected++ } }) if forRepair { t.log.Infof("Repairing %s", torrent.AccessKey) t.Repair(torrent.AccessKey) } if unselected == torrent.SelectedFiles.Count() && unselected > 0 { t.log.Infof("Deleting %s", torrent.AccessKey) t.Delete(torrent.AccessKey) } }) } func (t *TorrentManager) Delete(accessKey string) { t.log.Infof("Deleting torrent %s", accessKey) allTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS) if torrent, ok := allTorrents.Get(accessKey); ok { for _, instance := range torrent.Instances { t.api.DeleteTorrent(instance.ID) } allTorrents.Remove(accessKey) } } func (t *TorrentManager) Repair(accessKey string) { if !t.cfg.EnableRepair() { t.log.Warn("Repair is disabled; if you do not have other zurg instances running, you should enable repair") return } allTorrents, _ := t.DirectoryMap.Get(ALL_TORRENTS) torrent, _ := allTorrents.Get(accessKey) if torrent == nil { t.log.Warnf("Cannot find torrent %s anymore so we are not repairing it", accessKey) return } if torrent.InProgress() { t.log.Infof("Torrent %s is in progress, cannot repair", torrent.AccessKey) return } // make the file messy t.log.Infof("Evaluating whole torrent to find the correct files for torrent: %s", torrent.AccessKey) var selectedFiles []*File var isChaotic bool var links []string streamableCount := 0 torrent.SelectedFiles.IterCb(func(_ string, file *File) { if utils.IsStreamable(file.Path) { streamableCount++ } fileCopy := &File{ File: file.File, Added: file.Added, Ended: file.Ended, Link: file.Link, ZurgFS: file.ZurgFS, } selectedFiles = append(selectedFiles, fileCopy) if strings.HasPrefix(fileCopy.Link, "http") { links = append(links, fileCopy.Link) } fileCopy.Link = "" // empty the links = chaos! }) selectedFiles, isChaotic = t.organizeChaos(links, selectedFiles) // chaotic file means RD will not output the desired file selection // // e.g. even if we select just a single mkv, it will output a rar // var isChaotic bool // selectedFiles, isChaotic = t.organizeChaos(info.Links, selectedFiles) if isChaotic { t.log.Warnf("Torrent %s is always returning an unplayable rar file (it will no longer show up in your directories, zurg suggests you delete it)", torrent.AccessKey) t.DirectoryMap.IterCb(func(_ string, torrents cmap.ConcurrentMap[string, *Torrent]) { torrents.Remove(torrent.AccessKey) }) // t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash) return } else if streamableCount == 1 { t.log.Warnf("Torrent %s only file has expired (it will no longer show up in your directories, zurg suggests you delete it)", torrent.AccessKey) t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", torrent.Instances[0].Hash) t.DirectoryMap.IterCb(func(_ string, torrents cmap.ConcurrentMap[string, *Torrent]) { torrents.Remove(torrent.AccessKey) }) return } // t.log.Debugf("Identified the expired files of torrent id=%s", info.ID) for _, newFile := range selectedFiles { if file, exists := torrent.SelectedFiles.Get(filepath.Base(newFile.Path)); exists { file.Link = newFile.Link } else { torrent.SelectedFiles.Set(filepath.Base(newFile.Path), newFile) } } proceed := t.canCapacityHandle() // blocks for approx 45 minutes if active torrents are full if !proceed { t.log.Error("Reached the max number of active torrents, cannot continue with the repair") return } // first solution: add the same selection, maybe it can be fixed by reinsertion? if t.reinsertTorrent(torrent, "") { t.log.Infof("Successfully downloaded torrent %s to repair it", torrent.AccessKey) return } // if all the selected files are missing but there are other streamable files var missingFiles []File torrent.SelectedFiles.IterCb(func(_ string, file *File) { if !strings.HasPrefix(file.Link, "http") { missingFiles = append(missingFiles, *file) } }) // if we download a single file, it will be named differently // so we need to download 1 extra file to preserve the name // this is only relevant if we enable retain_rd_torrent_name if t.cfg.EnableRetainRDTorrentName() && len(missingFiles) == 1 && streamableCount > 1 { // add the first file link encountered with a prefix of http for _, file := range torrent.SelectedFiles.Items() { if strings.HasPrefix(file.Link, "http") { missingFiles = append(missingFiles, *file) break } } } if len(missingFiles) > 0 { t.log.Infof("Redownloading in multiple batches the %d missing files for torrent %s", len(missingFiles), torrent.AccessKey) // if not, last resort: add only the missing files but do it in 2 batches half := len(missingFiles) / 2 missingFiles1 := strings.Join(getFileIDs(missingFiles[:half]), ",") missingFiles2 := strings.Join(getFileIDs(missingFiles[half:]), ",") if missingFiles1 != "" { t.reinsertTorrent(torrent, missingFiles1) } if missingFiles2 != "" { t.reinsertTorrent(torrent, missingFiles2) } } else { t.log.Warnf("Torrent %s has no missing files to repair", torrent.AccessKey) } } func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string) bool { // if missingFiles is not provided, missing files means missing links if missingFiles == "" { tmpSelection := "" torrent.SelectedFiles.IterCb(func(_ string, file *File) { if !strings.HasPrefix(file.Link, "http") { tmpSelection += fmt.Sprintf("%d,", file.ID) } }) if tmpSelection == "" { return false } if len(tmpSelection) > 0 { missingFiles = tmpSelection[:len(tmpSelection)-1] } } // redownload torrent resp, err := t.api.AddMagnetHash(torrent.Instances[0].Hash) if err != nil { t.log.Warnf("Cannot redownload torrent: %v", err) return false } time.Sleep(1 * time.Second) // select files newTorrentID := resp.ID err = t.api.SelectTorrentFiles(newTorrentID, missingFiles) if err != nil { t.log.Warnf("Cannot start redownloading: %v", err) t.api.DeleteTorrent(newTorrentID) return false } time.Sleep(10 * time.Second) // see if the torrent is ready info, err := t.api.GetTorrentInfo(newTorrentID) if err != nil { t.log.Warnf("Cannot get info on redownloaded torrent id=%s : %v", newTorrentID, err) t.api.DeleteTorrent(newTorrentID) return false } if info.Status == "magnet_error" || info.Status == "error" || info.Status == "virus" || info.Status == "dead" { t.log.Warnf("The redownloaded torrent id=%s is in error state: %s", newTorrentID, info.Status) t.api.DeleteTorrent(newTorrentID) return false } if info.Progress != 100 { t.log.Infof("Torrent id=%s is not cached anymore so we have to wait until completion (this should fix the issue already)", info.ID) return true } missingCount := len(strings.Split(missingFiles, ",")) if len(info.Links) != missingCount { t.log.Infof("It did not fix the issue for id=%s, only got %d files but we need %d, undoing", info.ID, len(info.Links), missingCount) t.api.DeleteTorrent(newTorrentID) return false } t.log.Infof("Repair successful id=%s", newTorrentID) return true } func (t *TorrentManager) canCapacityHandle() bool { // max waiting time is 45 minutes const maxRetries = 50 const baseDelay = 1 * time.Second const maxDelay = 60 * time.Second retryCount := 0 for { count, err := t.api.GetActiveTorrentCount() if err != nil { t.log.Warnf("Cannot get active downloads count: %v", err) if retryCount >= maxRetries { t.log.Error("Max retries reached. Exiting.") return false } delay := time.Duration(math.Pow(2, float64(retryCount))) * baseDelay if delay > maxDelay { delay = maxDelay } time.Sleep(delay) retryCount++ continue } if count.DownloadingCount < count.MaxNumberOfTorrents { // t.log.Infof("We can still add a new torrent, we have capacity for %d more", count.MaxNumberOfTorrents-count.DownloadingCount) return true } delay := time.Duration(math.Pow(2, float64(retryCount))) * baseDelay if delay > maxDelay { delay = maxDelay } t.log.Infof("We have reached the max number of active torrents, waiting for %s seconds before retrying", delay) if retryCount >= maxRetries { t.log.Error("Max retries reached, exiting") return false } time.Sleep(delay) retryCount++ } }