Files
zurg/internal/handlers/router.go
Ben Adrian Sarmiento 396a8781aa Add VidHub endpoints
2024-07-11 15:53:20 +02:00

505 lines
19 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(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleVersionFile)
router.Head(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleCheckVersionFile)
// download
router.Get(fmt.Sprintf("/{mountType}/%s/{filename}", config.DOWNLOADS), hs.handleDownloadLink)
router.Head(fmt.Sprintf("/{mountType}/%s/{filename}", config.DOWNLOADS), hs.handleCheckDownloadLink)
// file
router.Get("/{mountType}/{directory}/{torrent}/{filename}", hs.handleDownloadFile)
router.Head("/{mountType}/{directory}/{torrent}/{filename}", hs.handleCheckFile)
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("/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.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.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.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)
router.Get("/vidhub/", hs.handleVidHubRoot)
router.Get(fmt.Sprintf("/vidhub/%s/", config.DOWNLOADS), hs.handleVidHubDownloadsList)
router.Get("/vidhub/{directory}/", hs.handleVidHubTorrentsList)
router.Get("/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList)
router.MethodFunc("PROPFIND", "/vidhub/", hs.handleVidHubRoot)
// router.MethodFunc("PROPFIND", fmt.Sprintf("/vidhub/%s/", config.DOWNLOADS), hs.handleVidHubDownloadsList)
// router.MethodFunc("PROPFIND", "/vidhub/{directory}/", hs.handleVidHubTorrentsList)
// router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList)
// router.MethodFunc("PROPFIND", "/vidhub", hs.handleVidHubRoot)
router.MethodFunc("PROPFIND", fmt.Sprintf("/vidhub/%s", config.DOWNLOADS), hs.handleVidHubDownloadsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}", hs.handleVidHubTorrentsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}", hs.handleVidHubFilesList)
router.MethodFunc("PROPFIND", "/{mountType}/{directory}/{torrent}/{filename}", hs.checkSingleFileHandler)
// note: reused handlers for dav and infuse
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 %v", req.Method, req.URL, req.Header)
http.Error(resp, "Method not allowed", http.StatusMethodNotAllowed)
})
}
// handle root request
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 && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
} else if err != nil {
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) handleHttpRoot(resp http.ResponseWriter, req *http.Request) {
hs.innerRootHandler(resp, req, intHttp.ServeRootDirectory, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavRoot(resp http.ResponseWriter, req *http.Request) {
hs.innerRootHandler(resp, req, dav.ServeRootDirectoryForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseRoot(resp http.ResponseWriter, req *http.Request) {
hs.innerRootHandler(resp, req, dav.ServeRootDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubRoot(resp http.ResponseWriter, req *http.Request) {
hs.innerRootHandler(resp, req, dav.ServeRootDirectoryForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle torrent list request
func (hs *Handlers) innerTorrentsListHandler(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 && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
} else if err != nil {
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) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
handlerFunc := intHttp.ServeTorrentsList
if directory == config.DOWNLOADS {
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) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeGroupDirectoryForDav
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeGroupDirectoryForInfuse
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubTorrentsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := dav.ServeGroupDirectoryForVidHub
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
// handle files list request
func (hs *Handlers) innerFilesListHandler(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 && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
} else if err != nil {
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) handleHttpFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, intHttp.ServeFilesList, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForVidHub, "text/xml; charset=\"utf-8\"")
}
// 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.ServeDownloadsForDav(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.ServeDownloadsForInfuse(torMgr)
}
hs.innerTorrentsListHandler(resp, req, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubDownloadsList(resp http.ResponseWriter, req *http.Request) {
handlerFunc := func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsForVidHub(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, 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.torMgr, 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) {
hs.log.Debugf("Not found: %s %s", req.Method, req.URL)
http.NotFound(resp, req)
}