Fix unrestrict issue

This commit is contained in:
Ben Sarmiento
2023-11-28 00:41:15 +01:00
parent c8334ecb3b
commit 3d380e468f
9 changed files with 131 additions and 68 deletions

View File

@@ -10,24 +10,23 @@ import (
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/internal/universal"
"github.com/hashicorp/golang-lru/v2/expirable"
"go.uber.org/zap"
)
// Router creates a WebDAV router
func Router(mux *http.ServeMux, getfile *universal.GetFile, c config.ConfigInterface, t *torrent.TorrentManager, cache *expirable.LRU[string, string], log *zap.SugaredLogger) {
func Router(mux *http.ServeMux, getfile *universal.GetFile, c config.ConfigInterface, t *torrent.TorrentManager, log *zap.SugaredLogger) {
mux.HandleFunc("/http/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
requestPath := path.Clean(r.URL.Path)
if countNonEmptySegments(strings.Split(requestPath, "/")) > 3 {
getfile.HandleGetRequest(w, r, t, c, cache, log)
getfile.HandleGetRequest(w, r, t, c, log)
} else {
intHttp.HandleDirectoryListing(w, r, t, log)
}
case http.MethodHead:
universal.HandleHeadRequest(w, r, t, cache, log)
universal.HandleHeadRequest(w, r, t, log)
default:
log.Errorf("Request %s %s not supported yet", r.Method, r.URL.Path)
@@ -44,7 +43,7 @@ func Router(mux *http.ServeMux, getfile *universal.GetFile, c config.ConfigInter
dav.HandleDeleteRequest(w, r, t, log)
case http.MethodGet:
getfile.HandleGetRequest(w, r, t, c, cache, log)
getfile.HandleGetRequest(w, r, t, c, log)
case http.MethodOptions:
w.WriteHeader(http.StatusOK)

View File

@@ -27,6 +27,7 @@ const (
type TorrentManager struct {
DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent
DownloadCache cmap.ConcurrentMap[string, *realdebrid.Download]
checksum string
latestAdded string
requiredVersion string
@@ -70,10 +71,35 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
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)
}
var initWait sync.WaitGroup
initWait.Add(2)
// Fetch downloads
go func() {
defer initWait.Done()
downloads, _, err := t.api.GetDownloads()
if err != nil {
t.log.Fatalf("Cannot get downloads: %v\n", err)
}
t.DownloadCache = cmap.New[*realdebrid.Download]()
for _, download := range downloads {
if !t.DownloadCache.Has(download.Link) {
t.DownloadCache.Set(download.Link, &download)
}
}
}()
// Fetch torrents
var newTorrents []realdebrid.Torrent
go func() {
defer initWait.Done()
newTorrents, _, err = t.api.GetTorrents(0)
if err != nil {
t.log.Fatalf("Cannot get torrents: %v\n", err)
}
}()
initWait.Wait()
torrentsChan := make(chan *Torrent, len(newTorrents))
var wg sync.WaitGroup
@@ -178,8 +204,8 @@ func (t *TorrentManager) mergeToMain(mainTorrent, torrentToMerge *Torrent) *Torr
}
// proxy
func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.UnrestrictResponse {
retChan := make(chan *realdebrid.UnrestrictResponse, 1)
func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download {
retChan := make(chan *realdebrid.Download, 1)
t.unrestrictPool.Submit(func() {
retChan <- t.api.UnrestrictUntilOk(link, t.cfg.ShouldServeFromRclone())
time.Sleep(1 * time.Second)
@@ -388,7 +414,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
var info *realdebrid.TorrentInfo
var err error
// file cache
torrentFromFile := t.readFromFile(rdTorrent.ID)
torrentFromFile := t.readTorrentFromFile(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
@@ -440,7 +466,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
}
if len(selectedFiles) > 0 && torrentFromFile == nil {
t.writeToFile(info) // only when there are selected files, else it's useless
t.writeTorrentToFile(info) // only when there are selected files, else it's useless
}
infoCache.Set(rdTorrent.ID, &torrent)
@@ -462,7 +488,7 @@ func (t *TorrentManager) getName(name, originalName string) string {
}
}
func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error {
func (t *TorrentManager) writeTorrentToFile(torrent *realdebrid.TorrentInfo) error {
filePath := DATA_DIR + "/" + torrent.ID + ".bin"
file, err := os.Create(filePath)
if err != nil {
@@ -481,7 +507,7 @@ func (t *TorrentManager) writeToFile(torrent *realdebrid.TorrentInfo) error {
return nil
}
func (t *TorrentManager) readFromFile(torrentID string) *realdebrid.TorrentInfo {
func (t *TorrentManager) readTorrentFromFile(torrentID string) *realdebrid.TorrentInfo {
filePath := DATA_DIR + "/" + torrentID + ".bin"
file, err := os.Open(filePath)
if err != nil {
@@ -506,7 +532,7 @@ func (t *TorrentManager) readFromFile(torrentID string) *realdebrid.TorrentInfo
func (t *TorrentManager) organizeChaos(links []string, selectedFiles []*File) ([]*File, bool) {
type Result struct {
Response *realdebrid.UnrestrictResponse
Response *realdebrid.Download
}
resultsChan := make(chan Result, len(links))

View File

@@ -13,7 +13,6 @@ import (
intHttp "github.com/debridmediamanager.com/zurg/internal/http"
intTor "github.com/debridmediamanager.com/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
"github.com/hashicorp/golang-lru/v2/expirable"
"go.uber.org/zap"
)
@@ -26,7 +25,7 @@ func NewGetFile(client *zurghttp.HTTPClient) *GetFile {
}
// HandleGetRequest handles a GET request universally for both WebDAV and HTTP
func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string], log *zap.SugaredLogger) {
func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) {
requestPath := path.Clean(r.URL.Path)
isDav := true
if strings.Contains(requestPath, "/http") {
@@ -71,15 +70,6 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
return
}
if url, exists := cache.Get(requestPath); exists {
if c.ShouldServeFromRclone() {
redirect(w, r, url, c)
} else {
gf.streamFileToResponse(file, url, w, r, t, c, log)
}
return
}
if !strings.HasPrefix(file.Link, "http") {
// This is a dead file, serve an alternate file
log.Warnf("File %s is not available", filename)
@@ -88,6 +78,15 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
}
link := file.Link
if download, exists := t.DownloadCache.Get(link); exists {
if c.ShouldServeFromRclone() {
redirect(w, r, download.Download, c)
} else {
gf.streamFileToResponse(file, download.Download, w, r, t, c, log)
}
return
}
resp := t.UnrestrictUntilOk(link)
if resp == nil {
// log.Warnf("File %s is no longer available, link %s", filepath.Base(file.Path), link)
@@ -112,7 +111,7 @@ func (gf *GetFile) HandleGetRequest(w http.ResponseWriter, r *http.Request, t *i
log.Warnf("Filename mismatch: %s and %s", filename, resp.Filename)
}
}
cache.Add(requestPath, resp.Download)
t.DownloadCache.Set(link, resp)
if c.ShouldServeFromRclone() {
redirect(w, r, resp.Download, c)
} else {

View File

@@ -8,7 +8,6 @@ import (
"strings"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/hashicorp/golang-lru/v2/expirable"
"go.uber.org/zap"
)
@@ -16,7 +15,7 @@ const (
SPLIT_TOKEN = "$"
)
func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, cache *expirable.LRU[string, string], log *zap.SugaredLogger) {
func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
requestPath := path.Clean(r.URL.Path)
requestPath = strings.Replace(requestPath, "/http", "", 1)
if requestPath == "/favicon.ico" {
@@ -31,18 +30,6 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren
return
}
if data, exists := cache.Get("head:" + requestPath); exists {
splits := strings.Split(data, SPLIT_TOKEN)
contentType := splits[0]
contentLength := splits[1]
lastModified := splits[2]
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", contentLength)
w.Header().Set("Last-Modified", lastModified)
w.WriteHeader(http.StatusOK)
return
}
baseDirectory := segments[len(segments)-3]
accessKey := segments[len(segments)-2]
filename := segments[len(segments)-1]
@@ -78,8 +65,6 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", contentLength)
w.Header().Set("Last-Modified", lastModified)
cacheVal := strings.Join([]string{contentType, contentLength, lastModified}, SPLIT_TOKEN)
cache.Add("head:"+requestPath, cacheVal)
w.WriteHeader(http.StatusOK)
}