Files
zurg/internal/handlers/router.go
2024-08-25 13:48:01 +02:00

472 lines
17 KiB
Go

package handlers
import (
"fmt"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/dav"
intHttp "github.com/debridmediamanager/zurg/internal/http"
"github.com/debridmediamanager/zurg/internal/torrent"
"github.com/debridmediamanager/zurg/internal/universal"
"github.com/debridmediamanager/zurg/internal/version"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/go-chi/chi/v5"
"github.com/panjf2000/ants/v2"
)
type Handlers struct {
downloader *universal.Downloader
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
rd *realdebrid.RealDebrid
workerPool *ants.Pool
hosts []string
log *logutil.Logger
}
func init() {
chi.RegisterMethod("PROPFIND")
chi.RegisterMethod("MKCOL")
chi.RegisterMethod("MOVE")
}
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,
rd: rd,
workerPool: workerPool,
hosts: hosts,
log: log,
}
if cfg.GetUsername() != "" {
router.Use(hs.basicAuth)
}
router.Use(hs.options)
router.NotFound(hs.handleNotFound)
router.Get("/", hs.handleHome)
router.Get("/stats", hs.handleHomeJson)
// debug
router.Post("/reboot-worker", hs.handleRebootWorkerPool)
router.Post("/reboot-refresh", hs.handleRebootRefreshWorker)
router.Post("/reboot-repair", hs.handleRebootRepairWorker)
router.Post("/downloads/remount", hs.handleRemountDownloads)
// utils
router.Post("/torrents/dump", hs.handleDumpTorrents)
router.Post("/torrents/analyze", hs.handleAnalyzeTorrents)
router.Post("/torrents/repair", hs.handleTriggerRepairAll)
router.Post("/torrents/reset-repair-state", hs.handleResetRepairState)
// version
router.Get("/{mountType}/version.txt", hs.handleVersionFile)
router.Head("/{mountType}/version.txt", hs.handleCheckVersionFile)
// download
router.Get("/{mountType}/__downloads__/{filename}", hs.handleDownloadLink)
router.Head("/{mountType}/__downloads__/{filename}", hs.handleCheckDownloadLink)
// file
router.Get("/{mountType}/{directory}/{torrent}/{filename}", hs.handleDownloadFile)
router.Head("/{mountType}/{directory}/{torrent}/{filename}", hs.handleCheckFile)
// http
router.Get("/http/", hs.handleHttpRootDirectory)
router.Get("/http/__downloads__/", hs.handleHttpDownloads)
router.Get("/http/{directory}/", hs.handleHttpGroupDirectory)
router.Get("/http/{directory}/{torrent}/", hs.handleHttpTorrentFiles)
// dav
router.Get("/dav/", hs.handleDavRootDirectory)
router.Get("/dav/__downloads__/", hs.handleDavDownloads)
router.Get("/dav/{directory}/", hs.handleDavGroupDirectory)
router.Get("/dav/{directory}/{torrent}/", hs.handleDavTorrentFiles)
router.MethodFunc("PROPFIND", "/dav/", hs.handleDavRootDirectory)
router.MethodFunc("PROPFIND", "/dav/__downloads__/", hs.handleDavDownloads)
router.MethodFunc("PROPFIND", "/dav/{directory}/", hs.handleDavGroupDirectory)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/", hs.handleDavTorrentFiles)
// infuse
router.Get("/infuse/", hs.handleInfuseRootDirectory)
router.Get("/infuse/__downloads__/", hs.handleInfuseDownloads)
router.Get("/infuse/{directory}/", hs.handleInfuseGroupDirectory)
router.Get("/infuse/{directory}/{torrent}/", hs.handleInfuseTorrentFiles)
router.MethodFunc("PROPFIND", "/infuse/", hs.handleInfuseRootDirectory)
router.MethodFunc("PROPFIND", "/infuse/__downloads__/", hs.handleInfuseDownloads)
router.MethodFunc("PROPFIND", "/infuse/{directory}/", hs.handleInfuseGroupDirectory)
router.MethodFunc("PROPFIND", "/infuse/{directory}/{torrent}/", hs.handleInfuseTorrentFiles)
// vidhub
router.Get("/vidhub/", hs.handleVidHubRootDirectory)
router.Get("/vidhub/__downloads__/", hs.handleVidHubDownloads)
router.Get("/vidhub/{directory}/", hs.handleVidHubGroupDirectory)
router.Get("/vidhub/{directory}/{torrent}/", hs.handleVidHubTorrentFiles)
router.MethodFunc("PROPFIND", "/vidhub/", hs.handleVidHubRootDirectory)
router.MethodFunc("PROPFIND", "/vidhub/__downloads__", hs.handleVidHubDownloads)
router.MethodFunc("PROPFIND", "/vidhub/{directory}", hs.handleVidHubGroupDirectory)
router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}", hs.handleVidHubTorrentFiles)
// note: reused handlers for dav and infuse
router.MethodFunc("PROPFIND", "/{mountType}/{directory}/{torrent}/{filename}", hs.checkSingleFileHandler)
router.Delete("/{mountType}/{directory}/{torrent}/", hs.deleteTorrentHandler)
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}/{filename}", hs.moveFileHandler)
// logs route
router.Get("/logs", hs.logsHandler)
router.Get("/logs/", hs.logsHandler)
router.Get("/logs/upload", hs.uploadLogsHandler)
router.Get("/logs/upload/", hs.uploadLogsHandler)
router.MethodNotAllowed(func(resp http.ResponseWriter, req *http.Request) {
hs.log.Debugf("Method not allowed: %s %s (error=%v)", req.Method, req.URL, req.Header)
http.Error(resp, "Method not allowed", http.StatusMethodNotAllowed)
})
}
// handle root request
func (hs *Handlers) innerRootDirectoryHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
out, err := handleFunc(hs.torMgr)
if err != nil {
hs.log.Errorf("Not found: %s %s (error=%v)", req.Method, req.URL, err)
http.NotFound(resp, req)
return
}
resp.Header().Set("Content-Type", contentType)
if strings.Contains(contentType, "xml") {
resp.WriteHeader(http.StatusMultiStatus)
} else {
resp.WriteHeader(http.StatusOK)
}
resp.Write(out)
}
func (hs *Handlers) handleHttpRootDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerRootDirectoryHandler(resp, req, intHttp.ServeRootDirectory, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavRootDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerRootDirectoryHandler(resp, req, dav.ServeRootDirectoryForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseRootDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerRootDirectoryHandler(resp, req, dav.ServeRootDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubRootDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerRootDirectoryHandler(resp, req, dav.ServeRootDirectoryForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle torrent list request
func (hs *Handlers) innerGroupDirectoryHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
out, err := handleFunc(directory, hs.torMgr)
if err != nil {
hs.log.Errorf("Not found: %s %s (error=%v)", req.Method, req.URL, err)
http.NotFound(resp, req)
return
}
resp.Header().Set("Content-Type", contentType)
if strings.Contains(contentType, "xml") {
resp.WriteHeader(http.StatusMultiStatus)
} else {
resp.WriteHeader(http.StatusOK)
}
resp.Write(out)
}
func (hs *Handlers) handleHttpGroupDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, intHttp.ServeGroupDirectory, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavGroupDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeGroupDirectoryForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseGroupDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeGroupDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubGroupDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeGroupDirectoryForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle files list request
func (hs *Handlers) innerTorrentFilesHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager, bool) ([]byte, error), contentType string) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
out, err := handleFunc(directory, torrentName, hs.torMgr, hs.cfg.ShouldHideBrokenTorrents())
if err != nil {
hs.log.Errorf("Not found: %s %s (error=%v)", req.Method, req.URL, err)
http.NotFound(resp, req)
return
}
resp.Header().Set("Content-Type", contentType)
if strings.Contains(contentType, "xml") {
resp.WriteHeader(http.StatusMultiStatus)
} else {
resp.WriteHeader(http.StatusOK)
}
resp.Write(out)
}
func (hs *Handlers) handleHttpTorrentFiles(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentFilesHandler(resp, req, intHttp.ServeTorrentFiles, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavTorrentFiles(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentFilesHandler(resp, req, dav.ServeTorrentFilesForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseTorrentFiles(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentFilesHandler(resp, req, dav.ServeTorrentFilesForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubTorrentFiles(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentFilesHandler(resp, req, dav.ServeTorrentFilesForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle downloads list request
func (hs *Handlers) handleHttpDownloads(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, intHttp.ServeDownloads, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavDownloads(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeDownloadsForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseDownloads(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeDownloadsForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubDownloads(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeDownloadsForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle delete request
func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil {
http.NotFound(resp, req)
return
}
resp.WriteHeader(http.StatusNoContent)
}
func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
if dav.HandleDeleteTorrent(directory, torrentName, hs.torMgr) != nil {
http.NotFound(resp, req)
return
}
resp.WriteHeader(http.StatusNoContent)
}
// other handlers
func (hs *Handlers) checkSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr)
if err != nil {
http.NotFound(resp, req)
return
}
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) {
resp.WriteHeader(http.StatusNoContent)
}
func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
if dav.HandleRenameFile(directory, torrentName, fileName, newName, hs.torMgr) != nil {
http.NotFound(resp, req)
return
}
resp.WriteHeader(http.StatusNoContent)
}
func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
if dav.HandleRenameTorrent(directory, torrentName, newName, hs.torMgr) != nil {
http.NotFound(resp, req)
return
}
resp.WriteHeader(http.StatusNoContent)
}
// universal handlers
func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
hs.downloader.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log)
}
func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) {
filename, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
filename = chi.URLParam(req, "filename")
}
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
hs.downloader.DownloadLink(download, resp, req, hs.cfg, hs.log)
} else {
http.NotFound(resp, req)
}
}
func (hs *Handlers) handleCheckFile(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
universal.CheckFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log)
}
func (hs *Handlers) handleCheckDownloadLink(resp http.ResponseWriter, req *http.Request) {
filename, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
filename = chi.URLParam(req, "filename")
}
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
universal.CheckDownloadLink(download, resp, req, hs.torMgr, 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)
}
func (hs *Handlers) handleCheckVersionFile(resp http.ResponseWriter, req *http.Request) {
universal.CheckVersionFile(resp, req, hs.torMgr, hs.log)
}
// logs handler
func (hs *Handlers) logsHandler(resp http.ResponseWriter, req *http.Request) {
logs, err := hs.log.GetLogsFromFile()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(resp, logs)
}
func (hs *Handlers) uploadLogsHandler(resp http.ResponseWriter, req *http.Request) {
url, err := hs.log.UploadLogFile()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(resp, req, url, http.StatusFound)
}
func (hs *Handlers) handleNotFound(resp http.ResponseWriter, req *http.Request) {
if hs.cfg.ShouldLogRequests() {
hs.log.Debugf("Not found: %s %s", req.Method, req.URL)
}
http.NotFound(resp, req)
}