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

@@ -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)
}