Redo router and use chi, implement anchor file

This commit is contained in:
Ben Sarmiento
2024-01-08 21:13:35 +01:00
parent 122eea2374
commit 96e41d6cbc
11 changed files with 352 additions and 295 deletions

1
go.mod
View File

@@ -17,6 +17,7 @@ require (
)
require (
github.com/go-chi/chi/v5 v5.0.11 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
)

2
go.sum
View File

@@ -4,6 +4,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=

View File

@@ -9,14 +9,14 @@ import (
// _ "net/http/pprof" // Register pprof
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/router"
"github.com/debridmediamanager/zurg/internal/handlers"
"github.com/debridmediamanager/zurg/internal/torrent"
"github.com/debridmediamanager/zurg/internal/universal"
"github.com/debridmediamanager/zurg/pkg/http"
"github.com/debridmediamanager/zurg/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/debridmediamanager/zurg/pkg/utils"
"github.com/julienschmidt/httprouter"
"github.com/go-chi/chi/v5"
"github.com/panjf2000/ants/v2"
)
@@ -51,10 +51,8 @@ func MainApp(configPath string) {
downloadClient := http.NewHTTPClient(config.GetToken(), config.GetRetriesUntilFailed(), 0, config, log.Named("dlclient"))
getfile := universal.NewGetFile(downloadClient)
handler := httprouter.New()
handler.RedirectTrailingSlash = false
handler.RedirectFixedPath = true
router.ApplyRouteTable(handler, getfile, torrentMgr, config, rd, log.Named("router"))
router := chi.NewRouter()
handlers.AttachHandlers(router, getfile, torrentMgr, config, rd, log.Named("router"))
// go func() {
// if err := netHttp.ListenAndServe(":6060", nil); err != nil && err != netHttp.ErrServerClosed {
@@ -65,7 +63,7 @@ func MainApp(configPath string) {
addr := fmt.Sprintf("%s:%s", config.GetHost(), config.GetPort())
zurglog.Infof("Starting server on %s", addr)
if err := netHttp.ListenAndServe(addr, handler); err != nil && err != netHttp.ErrServerClosed {
if err := netHttp.ListenAndServe(addr, router); err != nil && err != netHttp.ErrServerClosed {
zurglog.Errorf("Failed to start server: %v", err)
os.Exit(1)
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/torrent"
"github.com/debridmediamanager/zurg/internal/version"
"github.com/debridmediamanager/zurg/pkg/dav"
)
@@ -22,7 +23,11 @@ func ServeRootDirectoryForInfuse(torMgr *torrent.TorrentManager) ([]byte, error)
}
buf.WriteString(dav.BaseDirectory(directory, ""))
}
buf.WriteString(dav.BaseDirectory(config.DOWNLOADS, ""))
if torMgr.Config.GetConfig().UseDownloadCache {
buf.WriteString(dav.BaseDirectory(config.DOWNLOADS, ""))
}
_, size := version.GetFile()
buf.WriteString(dav.File(version.FILE, size, ""))
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/torrent"
"github.com/debridmediamanager/zurg/internal/version"
"github.com/debridmediamanager/zurg/pkg/dav"
)
@@ -24,7 +25,11 @@ func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) {
}
buf.WriteString(dav.Directory(directory, ""))
}
buf.WriteString(dav.Directory(config.DOWNLOADS, ""))
if torMgr.Config.GetConfig().UseDownloadCache {
buf.WriteString(dav.Directory(config.DOWNLOADS, ""))
}
_, size := version.GetFile()
buf.WriteString(dav.File(version.FILE, size, ""))
buf.WriteString("</d:multistatus>")
return buf.Bytes(), nil
}

View File

@@ -1,4 +1,4 @@
package router
package handlers
import (
"fmt"
@@ -10,7 +10,6 @@ import (
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/version"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/julienschmidt/httprouter"
)
type SponsorResponse struct {
@@ -35,7 +34,7 @@ type RootResponse struct {
Config config.ZurgConfig `json:"config"`
}
func (zr *ZurgRouter) rootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
func (zr *Handlers) handleHome(resp http.ResponseWriter, req *http.Request) {
userInfo, err := zr.api.GetUserInformation()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)

298
internal/handlers/router.go Normal file
View File

@@ -0,0 +1,298 @@
package handlers
import (
"fmt"
"net/http"
"path/filepath"
"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"
)
type Handlers struct {
getfile *universal.GetFile
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
log *logutil.Logger
}
func init() {
chi.RegisterMethod("PROPFIND")
chi.RegisterMethod("MKCOL")
chi.RegisterMethod("MOVE")
}
func AttachHandlers(router *chi.Mux, getfile *universal.GetFile, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, log *logutil.Logger) {
hs := &Handlers{
getfile: getfile,
torMgr: torMgr,
cfg: cfg,
api: api,
log: log,
}
router.Use(globalOptionsHandler)
router.Get("/", hs.handleHome)
router.Get("/{mountType}/version.txt", hs.handleVersionFile)
router.Get("/http/", hs.handleHttpRoot)
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("/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", "/dav/{directory}/", hs.handleDavTorrentsList)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/", hs.handleDavFilesList)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/{file}", hs.davCheckSingleFileHandler)
router.Get("/infuse/", hs.handleInfuseRoot)
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", "/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.MethodFunc("MKCOL", "/{mountType}/{directory}/{torrent}/", hs.mkcolTorrentHandler)
router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/", hs.moveTorrentHandler)
router.MethodFunc("MOVE", "/{mountType}/{directory}/{torrent}/{file}", hs.moveFileHandler)
// logs route
router.Get("/logs", hs.logsHandler)
router.Get("/logs/", hs.logsHandler)
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 {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.Header().Set("Content-Type", contentType)
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.ServeRootDirectory, "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\"")
}
// handle torrent list request
func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory := chi.URLParam(req, "directory")
out, err := handleFunc(directory, hs.torMgr)
if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.Header().Set("Content-Type", contentType)
resp.WriteHeader(http.StatusOK)
resp.Write(out)
}
func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
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) {
directory := chi.URLParam(req, "directory")
handlerFunc := dav.ServeTorrentsList
if directory == config.DOWNLOADS {
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) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
handlerFunc := dav.ServeTorrentsListForInfuse
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsListForInfuse(torMgr)
}
}
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) ([]byte, error), contentType string) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
out, err := handleFunc(directory, torrentName, hs.torMgr)
if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.Header().Set("Content-Type", contentType)
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.ServeFilesList, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseFilesList(resp http.ResponseWriter, req *http.Request) {
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)
}
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")
if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.WriteHeader(http.StatusNoContent)
}
func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request) {
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)
return
}
resp.WriteHeader(http.StatusNoContent)
}
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")
out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr)
if err != nil {
fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err)
http.Error(resp, "Not Found", http.StatusNotFound)
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")
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)
return
}
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler yay")
resp.WriteHeader(http.StatusNoContent)
}
func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
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)
return
}
resp.WriteHeader(http.StatusNoContent)
}
func globalOptionsHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}
func (hs *Handlers) universalDownloadFileHandler(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)
}
func (hs *Handlers) httpHeadHandler(resp http.ResponseWriter, req *http.Request) {
directory := chi.URLParam(req, "directory")
torrentName := chi.URLParam(req, "torrent")
fileName := chi.URLParam(req, "file")
universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log)
}
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 bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/debridmediamanager/zurg/internal/config"
"github.com/debridmediamanager/zurg/internal/torrent"
"github.com/debridmediamanager/zurg/internal/version"
)
func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) {
@@ -27,6 +28,7 @@ func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) {
if torMgr.Config.GetConfig().UseDownloadCache {
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", config.DOWNLOADS, config.DOWNLOADS))
}
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s\">%s</a></li>", version.FILE, version.FILE))
return buf.Bytes(), nil
}

View File

@@ -1,282 +0,0 @@
package router
import (
"fmt"
"net/http"
"path/filepath"
"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/pkg/logutil"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
"github.com/julienschmidt/httprouter"
)
type ZurgRouter struct {
getfile *universal.GetFile
torMgr *torrent.TorrentManager
cfg config.ConfigInterface
api *realdebrid.RealDebrid
log *logutil.Logger
}
func ApplyRouteTable(router *httprouter.Router, getfile *universal.GetFile, torMgr *torrent.TorrentManager, cfg config.ConfigInterface, api *realdebrid.RealDebrid, log *logutil.Logger) {
zr := &ZurgRouter{
getfile: getfile,
torMgr: torMgr,
cfg: cfg,
api: api,
log: log,
}
// file download handler
router.GET("/http/:directory/:torrent/:file", zr.universalDownloadFileHandler)
router.GET("/dav/:directory/:torrent/:file", zr.universalDownloadFileHandler)
router.GET("/infuse/:directory/:torrent/:file", zr.universalDownloadFileHandler)
// HEAD route
router.HEAD("/http/:directory/:torrent/:file", zr.httpHeadHandler)
// HTTP router
router.GET("/http/", zr.httpRootHandler)
router.GET("/http/:directory/", zr.httpTorrentsListHandler)
router.GET("/http/:directory/:torrent/", zr.httpFilesListHandler)
// INFUSE DAV routes
router.Handle("PROPFIND", "/infuse/", zr.infuseDavRootHandler)
router.Handle("PROPFIND", "/infuse/:directory/", zr.infuseDavTorrentsListHandler)
router.Handle("PROPFIND", "/infuse/:directory/:torrent/", zr.infuseDavFilesListHandler)
// WEBDAV router
router.Handle("PROPFIND", "/dav/", zr.davRootHandler)
router.Handle("PROPFIND", "/dav/:directory/", zr.davTorrentsListHandler)
router.Handle("PROPFIND", "/dav/:directory/:torrent/", zr.davFilesListHandler)
// EXTRA: for browser handling
router.GET("/dav/", zr.davRootHandler)
router.GET("/dav/:directory/", zr.davTorrentsListHandler)
router.GET("/dav/:directory/:torrent/", zr.davFilesListHandler)
// EXTRA: for browser handling of infuse
router.GET("/infuse/", zr.infuseDavRootHandler)
router.GET("/infuse/:directory/", zr.infuseDavTorrentsListHandler)
router.GET("/infuse/:directory/:torrent/", zr.infuseDavFilesListHandler)
// DELETE routes
router.DELETE("/dav/:directory/:torrent/", zr.deleteTorrentHandler)
router.DELETE("/dav/:directory/:torrent/:file", zr.deleteFileHandler)
// RENAME sequence
router.Handle("PROPFIND", "/dav/:directory/:torrent/:file", zr.davCheckSingleFileHandler)
router.Handle("MKCOL", "/dav/:directory/:torrent/", zr.mkcolTorrentHandler)
router.Handle("MOVE", "/dav/:directory/:torrent/:file", zr.moveFileHandler)
router.Handle("MOVE", "/dav/:directory/:torrent/", zr.moveTorrentHandler)
// Global OPTIONS route
router.GlobalOPTIONS = http.HandlerFunc(zr.globalOptionsHandler)
// root route
router.GET("/", zr.rootHandler)
// logs route
router.GET("/logs", zr.logsHandler)
router.GET("/logs/", zr.logsHandler)
router.MethodNotAllowed = http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
zr.log.Debugf("Method not allowed: %s %s %v", req.Method, req.URL, req.Header)
http.Error(resp, "Method not allowed", http.StatusMethodNotAllowed)
})
}
func (zr *ZurgRouter) handleRootRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
out, err := handleFunc(zr.torMgr)
if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.Header().Set("Content-Type", contentType)
resp.WriteHeader(http.StatusOK)
resp.Write(out)
}
func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
zr.handleRootRequest(resp, req, params, intHttp.ServeRootDirectory, "text/html; charset=\"utf-8\"")
}
func (zr *ZurgRouter) davRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
zr.handleRootRequest(resp, req, params, dav.ServeRootDirectory, "text/xml; charset=\"utf-8\"")
}
func (zr *ZurgRouter) infuseDavRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
zr.handleRootRequest(resp, req, params, dav.ServeRootDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
}
func (zr *ZurgRouter) handleTorrentsListRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory := params.ByName("directory")
out, err := handleFunc(directory, zr.torMgr)
if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.Header().Set("Content-Type", contentType)
resp.WriteHeader(http.StatusOK)
resp.Write(out)
}
func (zr *ZurgRouter) httpTorrentsListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
handlerFunc := intHttp.ServeTorrentsList
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return intHttp.ServeDownloadsList(torMgr)
}
}
zr.handleTorrentsListRequest(resp, req, params, handlerFunc, "text/html; charset=\"utf-8\"")
}
func (zr *ZurgRouter) davTorrentsListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
handlerFunc := dav.ServeTorrentsList
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsList(torMgr)
}
}
zr.handleTorrentsListRequest(resp, req, params, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (zr *ZurgRouter) infuseDavTorrentsListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
handlerFunc := dav.ServeTorrentsListForInfuse
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
return dav.ServeDownloadsListForInfuse(torMgr)
}
}
zr.handleTorrentsListRequest(resp, req, params, handlerFunc, "text/xml; charset=\"utf-8\"")
}
func (zr *ZurgRouter) handleFilesListRequest(resp http.ResponseWriter, req *http.Request, params httprouter.Params, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
out, err := handleFunc(directory, torrentName, zr.torMgr)
if err != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.Header().Set("Content-Type", contentType)
resp.WriteHeader(http.StatusOK)
resp.Write(out)
}
func (zr *ZurgRouter) httpFilesListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
zr.handleFilesListRequest(resp, req, params, intHttp.ServeFilesList, "text/html; charset=\"utf-8\"")
}
func (zr *ZurgRouter) davFilesListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
zr.handleFilesListRequest(resp, req, params, dav.ServeFilesList, "text/xml; charset=\"utf-8\"")
}
func (zr *ZurgRouter) infuseDavFilesListHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
fmt.Println(">>>>>>>>>>>>>>>>>>> infuseDavFilesListHandler", params.ByName("directory"), params.ByName("torrent"))
zr.handleFilesListRequest(resp, req, params, dav.ServeFilesListForInfuse, "text/xml; charset=\"utf-8\"")
}
func (zr *ZurgRouter) deleteFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
fileName := params.ByName("file")
if dav.HandleDeleteFile(directory, torrentName, fileName, zr.torMgr) != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.WriteHeader(http.StatusNoContent)
}
func (zr *ZurgRouter) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
if dav.HandleDeleteTorrent(directory, torrentName, zr.torMgr) != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.WriteHeader(http.StatusNoContent)
}
func (zr *ZurgRouter) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
fileName := params.ByName("file")
out, err := dav.HandleSingleFile(directory, torrentName, fileName, zr.torMgr)
if err != nil {
fmt.Println(">>>>>>>>>>>>>>>>>>>. not found", err)
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
fmt.Println(">>>>>>>>>>>>>>>>>>>. found yey")
resp.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK)
resp.Write(out)
}
func (zr *ZurgRouter) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
fmt.Println(">>>>>>>>>>>>>>>>>>> mkcolTorrentHandler")
resp.WriteHeader(http.StatusNoContent)
}
func (zr *ZurgRouter) moveFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
fileName := params.ByName("file")
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler", fileName, ">>>>>>>>", newName)
if dav.HandleRenameFile(directory, torrentName, fileName, newName, zr.torMgr) != nil {
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler not found")
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
fmt.Println(">>>>>>>>>>>>>>>>>>> moveFileHandler yay")
resp.WriteHeader(http.StatusNoContent)
}
func (zr *ZurgRouter) moveTorrentHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
if dav.HandleRenameTorrent(directory, torrentName, newName, zr.torMgr) != nil {
http.Error(resp, "Not Found", http.StatusNotFound)
return
}
resp.WriteHeader(http.StatusNoContent)
}
func (zr *ZurgRouter) globalOptionsHandler(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(http.StatusOK)
}
func (zr *ZurgRouter) universalDownloadFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
fileName := params.ByName("file")
zr.getfile.ServeFile(directory, torrentName, fileName, resp, req, zr.torMgr, zr.cfg, zr.log)
}
func (zr *ZurgRouter) httpHeadHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
directory := params.ByName("directory")
torrentName := params.ByName("torrent")
fileName := params.ByName("file")
universal.HandleHeadRequest(directory, torrentName, fileName, resp, req, zr.torMgr, zr.log)
}
func (zr *ZurgRouter) logsHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
logs, err := zr.log.GetLogsFromFile()
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(resp, logs)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}

29
internal/version/file.go Normal file
View File

@@ -0,0 +1,29 @@
package version
import (
"fmt"
"strings"
)
const FILE = "version.txt"
func GetFile() ([]byte, int64) {
versionStr := fmt.Sprintf(" version: %s", GetVersion())
out := "\n"
out += " █████████ █████ ████ ████████ ███████\n"
out += "░█░░░░███ ░░███ ░███ ░░███░░███ ███░░███\n"
out += "░ ███░ ░███ ░███ ░███ ░░░ ░███ ░███\n"
out += " ███░ █ ░███ ░███ ░███ ░███ ░███\n"
out += " █████████ ░░████████ █████ ░░███████\n"
out += "░░░░░░░░░ ░░░░░░░░ ░░░░░ ░░░░░███\n"
out += " ███ ░███\n"
out += versionStr + strings.Repeat(" ", 31-len(versionStr))
out += "░░██████ \n"
out += " ░░░░░░ \n"
out += "\n⟡ support zurg today ⟡\n\n"
out += "Patreon: https://www.patreon.com/debridmediamanager\n"
out += "Github: https://github.com/sponsors/debridmediamanager\n"
out += "Paypal: https://paypal.me/yowmamasita\n"
return []byte(out), int64(len(out))
}

View File

@@ -66,7 +66,7 @@ func NewHTTPClient(token string, maxRetries int, timeoutSecs int, cfg config.Con
if resp.StatusCode == 429 {
return 1
}
if resp.StatusCode != http.StatusPartialContent && hasRangeHeader {
if resp.StatusCode == http.StatusOK && hasRangeHeader {
return 1
}
return 0 // don't retry