diff --git a/internal/handlers/home.go b/internal/handlers/home.go index 1669320..7c88179 100644 --- a/internal/handlers/home.go +++ b/internal/handlers/home.go @@ -285,3 +285,7 @@ func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) { fmt.Fprint(resp, out) } + +func bToMb(b uint64) uint64 { + return b / 1024 / 1024 +} diff --git a/internal/handlers/router.go b/internal/handlers/router.go index 560c4b9..190382c 100644 --- a/internal/handlers/router.go +++ b/internal/handlers/router.go @@ -39,40 +39,44 @@ func AttachHandlers(router *chi.Mux, getfile *universal.GetFile, torMgr *torrent log: log, } - router.Use(globalOptionsHandler) + router.Use(optionsMiddleware) router.Get("/", hs.handleHome) router.Get("/{mountType}/version.txt", hs.handleVersionFile) + router.Get(fmt.Sprintf("/{mountType}/%s/{filename}", config.DOWNLOADS), hs.handleDownloadLink) + router.Get("/{mountType}/{directory}/{torrent}/{filename}", hs.handleDownloadFile) + router.Head("/{mountType}/{directory}/{torrent}/{filename}", hs.handleCheckCachedLink) router.Get("/http/", hs.handleHttpRoot) + router.Get(fmt.Sprintf("/http/%s/", config.DOWNLOADS), hs.handleHttpDownloadsList) router.Get("/http/{directory}/", hs.handleHttpTorrentsList) router.Get("/http/{directory}/{torrent}/", hs.handleHttpFilesList) - router.Get("/http/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler) - router.Head("/http/{directory}/{torrent}/{file}", hs.httpHeadHandler) router.Get("/dav/", hs.handleDavRoot) + router.Get(fmt.Sprintf("/dav/%s/", config.DOWNLOADS), hs.handleDavDownloadsList) router.Get("/dav/{directory}/", hs.handleDavTorrentsList) router.Get("/dav/{directory}/{torrent}/", hs.handleDavFilesList) - router.Get("/dav/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler) router.MethodFunc("PROPFIND", "/dav/", hs.handleDavRoot) + router.MethodFunc("PROPFIND", fmt.Sprintf("/dav/%s/", config.DOWNLOADS), hs.handleDavDownloadsList) router.MethodFunc("PROPFIND", "/dav/{directory}/", hs.handleDavTorrentsList) router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/", hs.handleDavFilesList) - router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/{file}", hs.davCheckSingleFileHandler) + router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/{filename}", hs.davCheckSingleFileHandler) router.Get("/infuse/", hs.handleInfuseRoot) + router.Get(fmt.Sprintf("/infuse/%s/", config.DOWNLOADS), hs.handleInfuseDownloadsList) router.Get("/infuse/{directory}/", hs.handleInfuseTorrentsList) router.Get("/infuse/{directory}/{torrent}/", hs.handleInfuseFilesList) - router.Get("/infuse/{directory}/{torrent}/{file}", hs.universalDownloadFileHandler) router.MethodFunc("PROPFIND", "/infuse/", hs.handleInfuseRoot) + router.MethodFunc("PROPFIND", fmt.Sprintf("/infuse/%s/", config.DOWNLOADS), hs.handleInfuseDownloadsList) router.MethodFunc("PROPFIND", "/infuse/{directory}/", hs.handleInfuseTorrentsList) router.MethodFunc("PROPFIND", "/infuse/{directory}/{torrent}/", hs.handleInfuseFilesList) // note: reused handlers for dav and infuse router.Delete("/{mountType}/{directory}/{torrent}/", hs.deleteTorrentHandler) - router.Delete("/{mountType}/{directory}/{torrent}/{file}", hs.deleteFileHandler) + router.Delete("/{mountType}/{directory}/{torrent}/{filename}", hs.deleteFileHandler) router.MethodFunc("MKCOL", "/{mountType}/{directory}/{torrent}/", hs.mkcolTorrentHandler) router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/", hs.moveTorrentHandler) - router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/{file}", hs.moveFileHandler) + router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/{filename}", hs.moveFileHandler) // logs route router.Get("/logs", hs.logsHandler) @@ -89,7 +93,7 @@ func AttachHandlers(router *chi.Mux, getfile *universal.GetFile, torMgr *torrent func (hs *Handlers) innerRootHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) { out, err := handleFunc(hs.torMgr) if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) + http.NotFound(resp, req) return } resp.Header().Set("Content-Type", contentType) @@ -115,7 +119,7 @@ func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http directory := chi.URLParam(req, "directory") out, err := handleFunc(directory, hs.torMgr) if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) + http.NotFound(resp, req) return } resp.Header().Set("Content-Type", contentType) @@ -163,7 +167,7 @@ func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Re torrentName := chi.URLParam(req, "torrent") out, err := handleFunc(directory, torrentName, hs.torMgr) if err != nil { - http.Error(resp, "Not Found", http.StatusNotFound) + http.NotFound(resp, req) return } resp.Header().Set("Content-Type", contentType) @@ -183,19 +187,37 @@ func (hs *Handlers) handleInfuseFilesList(resp http.ResponseWriter, req *http.Re hs.innerFilesListHandler(resp, req, dav.ServeFilesListForInfuse, "text/xml; charset=\"utf-8\"") } -func (hs *Handlers) handleVersionFile(resp http.ResponseWriter, req *http.Request) { - out, _ := version.GetFile() - resp.Header().Set("Content-Type", "text/plain; charset=\"utf-8\"") - resp.WriteHeader(http.StatusOK) - resp.Write(out) +// handle downloads list request + +func (hs *Handlers) handleHttpDownloadsList(resp http.ResponseWriter, req *http.Request) { + handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { + return intHttp.ServeDownloadsList(torMgr) + } + hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/html; charset=\"utf-8\"") } +func (hs *Handlers) handleDavDownloadsList(resp http.ResponseWriter, req *http.Request) { + handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { + return dav.ServeDownloadsList(torMgr) + } + hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"") +} + +func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *http.Request) { + handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) { + return dav.ServeDownloadsListForInfuse(torMgr) + } + hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"") +} + +// handle delete request + func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Request) { directory := chi.URLParam(req, "directory") torrentName := chi.URLParam(req, "torrent") - fileName := chi.URLParam(req, "file") + fileName := chi.URLParam(req, "filename") if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil { - http.Error(resp, "Not Found", http.StatusNotFound) + http.NotFound(resp, req) return } resp.WriteHeader(http.StatusNoContent) @@ -205,46 +227,42 @@ func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Req directory := chi.URLParam(req, "directory") torrentName := chi.URLParam(req, "torrent") if dav.HandleDeleteTorrent(directory, torrentName, hs.torMgr) != nil { - http.Error(resp, "Not Found", http.StatusNotFound) + http.NotFound(resp, req) return } resp.WriteHeader(http.StatusNoContent) } +// other handlers + func (hs *Handlers) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request) { directory := chi.URLParam(req, "directory") torrentName := chi.URLParam(req, "torrent") - fileName := chi.URLParam(req, "file") + fileName := chi.URLParam(req, "filename") out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr) if err != nil { - fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err) - http.Error(resp, "Not Found", http.StatusNotFound) + http.NotFound(resp, req) return } - fmt.Println(">>>>>>>>>>>>>>>>>>>. found yey") resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"") resp.WriteHeader(http.StatusOK) resp.Write(out) } func (hs *Handlers) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Request) { - fmt.Println(">>>>>>>>>>>>>>>>>>> mkcolTorrentHandler") resp.WriteHeader(http.StatusNoContent) } func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request) { directory := chi.URLParam(req, "directory") torrentName := chi.URLParam(req, "torrent") - fileName := chi.URLParam(req, "file") + fileName := chi.URLParam(req, "filename") newName := req.Header.Get("Destination") newName = filepath.Base(newName) - fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler", fileName, ">>>>>>>>", newName) if dav.HandleRenameFile(directory, torrentName, fileName, newName, hs.torMgr) != nil { - fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler not found") - http.Error(resp, "Not Found", http.StatusNotFound) + http.NotFound(resp, req) return } - fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler yay") resp.WriteHeader(http.StatusNoContent) } @@ -254,13 +272,13 @@ func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Reque newName := req.Header.Get("Destination") newName = filepath.Base(newName) if dav.HandleRenameTorrent(directory, torrentName, newName, hs.torMgr) != nil { - http.Error(resp, "Not Found", http.StatusNotFound) + http.NotFound(resp, req) return } resp.WriteHeader(http.StatusNoContent) } -func globalOptionsHandler(next http.Handler) http.Handler { +func optionsMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == "OPTIONS" { w.WriteHeader(http.StatusOK) @@ -270,20 +288,42 @@ func globalOptionsHandler(next http.Handler) http.Handler { }) } -func (hs *Handlers) universalDownloadFileHandler(resp http.ResponseWriter, req *http.Request) { +// universal handlers + +func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Request) { directory := chi.URLParam(req, "directory") torrentName := chi.URLParam(req, "torrent") - fileName := chi.URLParam(req, "file") - hs.getfile.ServeFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log) + fileName := chi.URLParam(req, "filename") + hs.getfile.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log) } -func (hs *Handlers) httpHeadHandler(resp http.ResponseWriter, req *http.Request) { +func (hs *Handlers) handleCheckCachedLink(resp http.ResponseWriter, req *http.Request) { directory := chi.URLParam(req, "directory") torrentName := chi.URLParam(req, "torrent") - fileName := chi.URLParam(req, "file") + fileName := chi.URLParam(req, "filename") universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log) } +func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) { + filename := chi.URLParam(req, "filename") + if download, ok := hs.torMgr.DownloadMap.Get(filename); ok { + hs.getfile.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log) + } else { + http.NotFound(resp, req) + } +} + +// handle version file request + +func (hs *Handlers) handleVersionFile(resp http.ResponseWriter, req *http.Request) { + out, _ := version.GetFile() + resp.Header().Set("Content-Type", "text/plain; charset=\"utf-8\"") + resp.WriteHeader(http.StatusOK) + resp.Write(out) +} + +// logs handler + func (hs *Handlers) logsHandler(resp http.ResponseWriter, req *http.Request) { logs, err := hs.log.GetLogsFromFile() if err != nil { @@ -292,7 +332,3 @@ func (hs *Handlers) logsHandler(resp http.ResponseWriter, req *http.Request) { } fmt.Fprint(resp, logs) } - -func bToMb(b uint64) uint64 { - return b / 1024 / 1024 -} diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index cd46eeb..7ab4fb2 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -26,6 +26,7 @@ type TorrentManager struct { Api *realdebrid.RealDebrid DirectoryMap cmap.ConcurrentMap[string, cmap.ConcurrentMap[string, *Torrent]] // directory -> accessKey -> Torrent DownloadCache cmap.ConcurrentMap[string, *realdebrid.Download] + DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download] allAccessKeys mapset.Set[string] latestState *LibraryState requiredVersion string @@ -59,6 +60,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p // Fetch downloads t.DownloadCache = cmap.New[*realdebrid.Download]() + t.DownloadMap = cmap.New[*realdebrid.Download]() if t.Config.EnableDownloadCache() { _ = t.workerPool.Submit(func() { page := 1 @@ -76,7 +78,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p downloads[i].Download = replaceHostInURL(downloads[i].Download, prefHost) } } - t.DownloadCache.Set(downloads[i].Link, &downloads[i]) + t.cacheDownload(&downloads[i]) } } offset += len(downloads) @@ -114,18 +116,21 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p // proxy func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download { + if !strings.HasPrefix(link, "http") { + return nil + } if download, exists := t.DownloadCache.Get(link); exists { return download } - ret := t.Api.UnrestrictUntilOk(link, t.Config.ShouldServeFromRclone()) - if ret != nil { + ret, _ := t.Api.UnrestrictLink(link, t.Config.ShouldServeFromRclone()) + if ret != nil && ret.Link != "" { if strings.Contains(ret.Download, "download.real-debrid.") { prefHost := t.Config.GetRandomPreferredHost() if prefHost != "" { ret.Download = replaceHostInURL(ret.Download, prefHost) } } - t.DownloadCache.Set(link, ret) + t.cacheDownload(ret) } return ret } @@ -251,3 +256,8 @@ func replaceHostInURL(inputURL string, newHost string) string { u.Host = newHost return u.String() } + +func (t *TorrentManager) cacheDownload(ret *realdebrid.Download) { + t.DownloadCache.Set(ret.Link, ret) + t.DownloadMap.Set(ret.Filename, ret) +} diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index cb33f8d..3843b02 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -152,7 +152,7 @@ func (t *TorrentManager) repair(torrent *Torrent) { unassignedDownloads := make([]*realdebrid.Download, 0) torrent.UnassignedLinks.Each(func(link string) bool { unrestrict := t.UnrestrictUntilOk(link) - if unrestrict != nil && unrestrict.Link != "" { + if unrestrict != nil { // assign to a selected file assigned := false torrent.SelectedFiles.IterCb(func(_ string, file *File) { diff --git a/internal/universal/get.go b/internal/universal/get.go index f0c8fc3..1e746e4 100644 --- a/internal/universal/get.go +++ b/internal/universal/get.go @@ -21,8 +21,8 @@ func NewGetFile(client *zurghttp.HTTPClient) *GetFile { return &GetFile{client: client} } -// ServeFile handles a GET request universally for both WebDAV and HTTP -func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { +// DownloadFile handles a GET request for files in torrents +func (gf *GetFile) DownloadFile(directory, torrentName, fileName string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { log.Warnf("Cannot find directory %s", directory) @@ -53,7 +53,7 @@ func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http. log.Debugf("Opening file %s from torrent %s (%s)", fileName, torrentName, link) unrestrict := torMgr.UnrestrictUntilOk(link) - if unrestrict == nil || unrestrict.Link == "" { + if unrestrict == nil { log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link) if cfg.EnableRepair() { file.Link = "repair" @@ -93,6 +93,51 @@ func (gf *GetFile) ServeFile(directory, torrentName, fileName string, resp http. } } +// DownloadLink handles a GET request for downloads +func (gf *GetFile) DownloadLink(fileName, link string, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { + if !strings.HasPrefix(link, "http") { + // This is a dead file, serve an alternate file + log.Warnf("File %s is not available", fileName) + http.Error(resp, "File is not available", http.StatusNotFound) + return + } + + log.Debugf("Opening download %s", fileName) + unrestrict := torMgr.UnrestrictUntilOk(link) + if unrestrict == nil { + log.Warnf("File %s cannot be unrestricted (link=%s)", fileName, link) + http.Error(resp, "File is not available", http.StatusNotFound) + return + } else { + if unrestrict.Filename != fileName { + // this is possible if there's only 1 streamable file in the torrent + // and then suddenly it's a rar file + actualExt := filepath.Ext(unrestrict.Filename) + expectedExt := filepath.Ext(fileName) + if actualExt != expectedExt && unrestrict.Streamable != 1 { + log.Warnf("File was changed and is not streamable: %s and %s (link=%s)", fileName, unrestrict.Filename, unrestrict.Link) + http.Error(resp, "File is not available", http.StatusNotFound) + return + } else { + log.Warnf("Filename mismatch: %s and %s", fileName, unrestrict.Filename) + } + } + if cfg.ShouldServeFromRclone() { + if cfg.ShouldVerifyDownloadLink() { + if !torMgr.Api.CanFetchFirstByte(unrestrict.Download) { + log.Warnf("File %s is not available", fileName) + http.Error(resp, "File is not available", http.StatusNotFound) + return + } + } + redirect(resp, req, unrestrict.Download, cfg) + } else { + gf.streamFileToResponse(nil, nil, unrestrict, resp, req, torMgr, cfg, log) + } + return + } +} + func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.File, unrestrict *realdebrid.Download, resp http.ResponseWriter, req *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *logutil.Logger) { // Create a new request for the file download. dlReq, err := http.NewRequest(http.MethodGet, unrestrict.Download, nil) @@ -113,7 +158,7 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi if err != nil { if file != nil && unrestrict.Streamable == 1 { log.Warnf("Cannot download file %s: %v", file.Path, err) - if cfg.EnableRepair() { + if cfg.EnableRepair() && torrent != nil { file.Link = "repair" torMgr.Repair(torrent) } else { @@ -128,7 +173,7 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi if download.StatusCode != http.StatusOK && download.StatusCode != http.StatusPartialContent { if file != nil && unrestrict.Streamable == 1 { log.Warnf("Received a %s status code for file %s", download.Status, file.Path) - if cfg.EnableRepair() { + if cfg.EnableRepair() && torrent != nil { file.Link = "repair" torMgr.Repair(torrent) } else { diff --git a/pkg/realdebrid/unrestrict.go b/pkg/realdebrid/unrestrict.go index f9e705b..7594bf0 100644 --- a/pkg/realdebrid/unrestrict.go +++ b/pkg/realdebrid/unrestrict.go @@ -2,20 +2,8 @@ package realdebrid import ( "net/http" - "strings" ) -func (rd *RealDebrid) UnrestrictUntilOk(link string, serveFromRclone bool) *Download { - if !strings.HasPrefix(link, "http") { - return nil - } - resp, err := rd.UnrestrictLink(link, serveFromRclone) - if err != nil { - return nil - } - return resp -} - func (rd *RealDebrid) CanFetchFirstByte(url string) bool { req, err := http.NewRequest("GET", url, nil) if err != nil {