package realdebrid import ( "fmt" "io" "net/http" "net/url" "strings" "time" "github.com/debridmediamanager/zurg/internal/config" zurghttp "github.com/debridmediamanager/zurg/pkg/http" "github.com/debridmediamanager/zurg/pkg/logutil" "github.com/debridmediamanager/zurg/pkg/utils" cmap "github.com/orcaman/concurrent-map/v2" "github.com/panjf2000/ants/v2" ) type RealDebrid struct { UnrestrictMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Download]] TokenManager *DownloadTokenManager torrentsCache []Torrent verifiedLinks cmap.ConcurrentMap[string, int64] apiClient *zurghttp.HTTPClient unrestrictClient *zurghttp.HTTPClient downloadClient *zurghttp.HTTPClient workerPool *ants.Pool cfg config.ConfigInterface log *logutil.Logger } func NewRealDebrid(apiClient, unrestrictClient, downloadClient *zurghttp.HTTPClient, workerPool *ants.Pool, cfg config.ConfigInterface, log *logutil.Logger) *RealDebrid { mainToken := cfg.GetToken() downloadTokens := cfg.GetDownloadTokens() if !strings.Contains(strings.Join(downloadTokens, ","), mainToken) { downloadTokens = append([]string{mainToken}, downloadTokens...) } rd := &RealDebrid{ UnrestrictMap: cmap.New[cmap.ConcurrentMap[string, *Download]](), TokenManager: NewDownloadTokenManager(downloadTokens, log), torrentsCache: []Torrent{}, verifiedLinks: cmap.New[int64](), apiClient: apiClient, unrestrictClient: unrestrictClient, downloadClient: downloadClient, workerPool: workerPool, cfg: cfg, log: log, } for _, token := range downloadTokens { rd.UnrestrictMap.Set(token, cmap.New[*Download]()) } return rd } const DOWNLOAD_LINK_EXPIRY = 60 * 60 * 24 func (rd *RealDebrid) UnrestrictAndVerify(link string) (*Download, error) { for { token, err := rd.TokenManager.GetCurrentToken() if err != nil { // when all tokens are expired return nil, err } // check if the link is already unrestricted tokenMap, _ := rd.UnrestrictMap.Get(token) if tokenMap.Has(link) { download, _ := tokenMap.Get(link) if !rd.cfg.ShouldServeFromRclone() { return download, nil } // 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) if utils.IsBytesLimitReached(err) { rd.TokenManager.SetTokenAsExpired(token, "bandwidth limit exceeded") continue } else if utils.IsInvalidDownloadCode(err) { continue } else if err == nil { rd.verifiedLinks.Set(download.ID, time.Now().Unix()+DOWNLOAD_LINK_EXPIRY) return download, nil } tokenMap.Remove(link) } download, err := rd.UnrestrictLinkWithToken(link, token) if err != nil { return nil, err } download.Token = token tokenMap.Set(link, download) if rd.cfg.ShouldServeFromRclone() { err = rd.downloadClient.VerifyLink(download.Download) if utils.IsBytesLimitReached(err) { rd.TokenManager.SetTokenAsExpired(token, "bandwidth limit exceeded") continue } else if utils.IsInvalidDownloadCode(err) { continue } else if err != nil { return nil, err } rd.verifiedLinks.Set(download.ID, time.Now().Unix()+DOWNLOAD_LINK_EXPIRY) } return download, err } } func (rd *RealDebrid) UnrestrictLinkWithToken(link, token string) (*Download, error) { data := url.Values{} if strings.HasPrefix(link, "https://real-debrid.com/d/") { // set link to max 39 chars (26 + 13) link = link[0:39] } data.Set("link", link) requestBody := strings.NewReader(data.Encode()) req, err := http.NewRequest(http.MethodPost, "https://api.real-debrid.com/rest/1.0/unrestrict/link", requestBody) if err != nil { // rd.log.Errorf("Error when creating a unrestrict link request: %v", err) return nil, err } req.Header.Set("Authorization", "Bearer "+token) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") // at this point, any errors mean that the link has expired and we need to repair it resp, err := rd.unrestrictClient.Do(req) if err != nil { // rd.log.Errorf("Error when executing the unrestrict link request: %v", err) return nil, fmt.Errorf("unrestrict link request failed: %v", err) } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { // rd.log.Errorf("Error when reading the body of unrestrict link response: %v", err) return nil, fmt.Errorf("unreadable body: %v", err) } var response Download err = json.Unmarshal(body, &response) if err != nil { // rd.log.Errorf("Error when decoding unrestrict link JSON: %v", err) return nil, fmt.Errorf("undecodable response: %v", err) } // rd.log.Debugf("Unrestricted link %s into %s", link, response.Download) return &response, nil } func (rd *RealDebrid) GetTorrentInfo(id string) (*TorrentInfo, error) { url := "https://api.real-debrid.com/rest/1.0/torrents/info/" + id req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { rd.log.Errorf("Error when creating a get info request: %v", err) return nil, err } resp, err := rd.apiClient.Do(req) if err != nil { rd.log.Errorf("Error when executing the get info request: %v", err) return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { rd.log.Errorf("Error when reading the body of get info response: %v", err) return nil, err } var response TorrentInfo err = json.Unmarshal(body, &response) if err != nil { rd.log.Errorf("Error when : %v", err) return nil, err } // rd.log.Debugf("Got info for torrent %s (progress=%d%%)", id, response.Progress) return &response, nil } // SelectTorrentFiles selects files of a torrent to start it. func (rd *RealDebrid) SelectTorrentFiles(id string, files string) error { data := url.Values{} data.Set("files", files) requestBody := strings.NewReader(data.Encode()) reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/selectFiles/%s", id) req, err := http.NewRequest(http.MethodPost, reqURL, requestBody) if err != nil { rd.log.Errorf("Error when creating a select files request: %v", err) return err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, err := rd.apiClient.Do(req) if err != nil { rd.log.Errorf("Error when executing the select files request: %v", err) return err } defer resp.Body.Close() rd.log.Debugf("Selected %d files and started the download for torrent id=%s (status code: %d)", len(strings.Split(files, ",")), id, resp.StatusCode) return nil } // DeleteTorrent deletes a torrent from the torrents list. func (rd *RealDebrid) DeleteTorrent(id string) error { // Construct request URL reqURL := fmt.Sprintf("https://api.real-debrid.com/rest/1.0/torrents/delete/%s", id) req, err := http.NewRequest(http.MethodDelete, reqURL, nil) if err != nil { rd.log.Errorf("Error when creating a delete torrent request: %v", err) return err } // Send the request resp, err := rd.apiClient.Do(req) if err != nil { rd.log.Errorf("Error when executing the delete torrent request: %v", err) return err } defer resp.Body.Close() rd.log.Debugf("Deleted torrent with id=%s", id) return nil } // AddMagnetHash adds a magnet link to download. func (rd *RealDebrid) AddMagnetHash(magnet string) (*MagnetResponse, error) { // Prepare request data data := url.Values{} data.Set("magnet", fmt.Sprintf("magnet:?xt=urn:btih:%s", magnet)) requestBody := strings.NewReader(data.Encode()) // Construct request URL reqURL := "https://api.real-debrid.com/rest/1.0/torrents/addMagnet" req, err := http.NewRequest(http.MethodPost, reqURL, requestBody) if err != nil { rd.log.Errorf("Error when creating an add magnet request: %v", err) return nil, err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") // Send the request resp, err := rd.apiClient.Do(req) if err != nil { rd.log.Errorf("Error when executing the add magnet request: %v", err) return nil, err } defer resp.Body.Close() var response MagnetResponse err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { rd.log.Errorf("Error when decoding add magnet JSON: %v", err) return nil, err } rd.log.Debugf("Added magnet %s with id=%s", magnet, response.ID) return &response, nil } // GetActiveTorrentCount gets the number of currently active torrents and the current maximum limit. func (rd *RealDebrid) GetActiveTorrentCount() (*ActiveTorrentCountResponse, error) { // Construct request URL reqURL := "https://api.real-debrid.com/rest/1.0/torrents/activeCount" req, err := http.NewRequest(http.MethodGet, reqURL, nil) if err != nil { rd.log.Errorf("Error when creating a active torrents request: %v", err) return nil, err } // Send the request resp, err := rd.apiClient.Do(req) if err != nil { rd.log.Errorf("Error when executing the active torrents request: %v", err) return nil, err } defer resp.Body.Close() var response ActiveTorrentCountResponse err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { rd.log.Errorf("Error when decoding active torrents JSON: %v", err) return nil, err } return &response, nil } // GetUserInformation gets the current user information. func (rd *RealDebrid) GetUserInformation() (*User, error) { // Construct request URL reqURL := "https://api.real-debrid.com/rest/1.0/user" req, err := http.NewRequest(http.MethodGet, reqURL, nil) if err != nil { rd.log.Errorf("Error when creating a user information request: %v", err) return nil, err } // Send the request resp, err := rd.apiClient.Do(req) if err != nil { rd.log.Errorf("Error when executing the user information request: %v", err) return nil, err } defer resp.Body.Close() // Decode the JSON response into the User struct var user User err = json.NewDecoder(resp.Body).Decode(&user) if err != nil { rd.log.Errorf("Error when decoding user information JSON: %v", err) return nil, err } return &user, nil } // TrafficDetails represents the structure of the traffic details response type TrafficDetails map[string]struct { Host map[string]uint64 `json:"host"` Bytes int64 `json:"bytes"` } // GetTrafficDetails gets the traffic details from the Real-Debrid API func (rd *RealDebrid) GetTrafficDetails(token string) (map[string]uint64, error) { // Construct request URL reqURL := "https://api.real-debrid.com/rest/1.0/traffic/details" req, err := http.NewRequest(http.MethodGet, reqURL, nil) if err != nil { rd.log.Errorf("Error when creating a traffic details request: %v", err) return nil, err } req.Header.Set("Authorization", "Bearer "+token) // Send the request resp, err := rd.apiClient.Do(req) if err != nil { rd.log.Errorf("Error when executing the traffic details request: %v", err) return nil, err } defer resp.Body.Close() // Decode the JSON response into the TrafficDetails struct var trafficDetails TrafficDetails err = json.NewDecoder(resp.Body).Decode(&trafficDetails) if err != nil { // rd.log.Errorf("Error when decoding traffic details JSON: %v", err) return nil, err } // Find the latest date in the traffic details var latestDate string for date := range trafficDetails { if latestDate == "" || date > latestDate { latestDate = date } } // Get the traffic details for the latest date latestTraffic := trafficDetails[latestDate].Host return latestTraffic, nil } // AvailabilityCheck checks the instant availability of torrents func (rd *RealDebrid) AvailabilityCheck(hashes []string) (AvailabilityResponse, error) { if len(hashes) == 0 { return nil, fmt.Errorf("no hashes provided") } baseURL := "https://api.real-debrid.com/rest/1.0" url := fmt.Sprintf("%s/torrents/instantAvailability/%s", baseURL, strings.Join(hashes, "/")) req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { return nil, err } resp, err := rd.apiClient.Do(req) if err != nil { return nil, err } defer resp.Body.Close() var response AvailabilityResponse err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { return nil, err } return response, nil } func (rd *RealDebrid) DownloadFile(req *http.Request) (*http.Response, error) { return rd.downloadClient.Do(req) } // MonitorExpiredTokens is a permanent job for monitoring expired tokens if they are still expired func (rd *RealDebrid) MonitorExpiredTokens() { sleepPeriod := 1 * time.Minute rd.workerPool.Submit(func() { for { expiredTokens := rd.TokenManager.GetExpiredTokens() for _, token := range expiredTokens { tokenMap, _ := rd.UnrestrictMap.Get(token) stillExpired := true skipAll := false tokenMap.IterCb(func(key string, download *Download) { if skipAll { return } err := rd.downloadClient.VerifyLink(download.Download) if err != nil { skipAll = utils.IsBytesLimitReached(err) return } stillExpired = false skipAll = true rd.verifiedLinks.Set(download.ID, time.Now().Unix()+60*60*24) }) if !stillExpired { rd.TokenManager.SetTokenAsUnexpired(token) } } time.Sleep(sleepPeriod) } }) }