From 5f60b51cf8abd48265904b82cb0a7ff0f21a099f Mon Sep 17 00:00:00 2001 From: j2rong4cn <36783515+j2rong4cn@users.noreply.github.com> Date: Wed, 22 May 2024 23:31:42 +0800 Subject: [PATCH] feat: add `proxy_range` option for `139Yun` `Alias` `AList V3` (#6496) --- drivers/139/meta.go | 9 ++++++--- drivers/alias/meta.go | 11 ++++++----- drivers/alias/util.go | 8 ++++++-- drivers/alist_v3/meta.go | 9 +++++---- internal/driver/config.go | 1 + internal/model/storage.go | 1 + internal/op/driver.go | 11 +++++++++++ server/common/proxy.go | 20 ++++++++++++++++++++ server/handles/down.go | 3 +++ server/webdav/webdav.go | 3 +++ 10 files changed, 62 insertions(+), 14 deletions(-) diff --git a/drivers/139/meta.go b/drivers/139/meta.go index 416e63a796c..56a4c1df96b 100644 --- a/drivers/139/meta.go +++ b/drivers/139/meta.go @@ -14,12 +14,15 @@ type Addition struct { } var config = driver.Config{ - Name: "139Yun", - LocalSort: true, + Name: "139Yun", + LocalSort: true, + ProxyRangeOption: true, } func init() { op.RegisterDriver(func() driver.Driver { - return &Yun139{} + d := &Yun139{} + d.ProxyRange = true + return d }) } diff --git a/drivers/alias/meta.go b/drivers/alias/meta.go index 2bebe7b00a8..45b885753d0 100644 --- a/drivers/alias/meta.go +++ b/drivers/alias/meta.go @@ -14,11 +14,12 @@ type Addition struct { } var config = driver.Config{ - Name: "Alias", - LocalSort: true, - NoCache: true, - NoUpload: true, - DefaultRoot: "/", + Name: "Alias", + LocalSort: true, + NoCache: true, + NoUpload: true, + DefaultRoot: "/", + ProxyRangeOption: true, } func init() { diff --git a/drivers/alias/util.go b/drivers/alias/util.go index 803bd0736b9..ba1f7e72649 100644 --- a/drivers/alias/util.go +++ b/drivers/alias/util.go @@ -103,12 +103,16 @@ func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs) return nil, err } if common.ShouldProxy(storage, stdpath.Base(sub)) { - return &model.Link{ + link := &model.Link{ URL: fmt.Sprintf("%s/p%s?sign=%s", common.GetApiUrl(args.HttpReq), utils.EncodePath(reqPath, true), sign.Sign(reqPath)), - }, nil + } + if args.HttpReq != nil && d.ProxyRange { + link.RangeReadCloser = common.NoProxyRange + } + return link, nil } link, _, err := fs.Link(ctx, reqPath, args) return link, err diff --git a/drivers/alist_v3/meta.go b/drivers/alist_v3/meta.go index c04c737b149..cc5f2189395 100644 --- a/drivers/alist_v3/meta.go +++ b/drivers/alist_v3/meta.go @@ -16,10 +16,11 @@ type Addition struct { } var config = driver.Config{ - Name: "AList V3", - LocalSort: true, - DefaultRoot: "/", - CheckStatus: true, + Name: "AList V3", + LocalSort: true, + DefaultRoot: "/", + CheckStatus: true, + ProxyRangeOption: true, } func init() { diff --git a/internal/driver/config.go b/internal/driver/config.go index c9e3f949af0..6068143cb71 100644 --- a/internal/driver/config.go +++ b/internal/driver/config.go @@ -12,6 +12,7 @@ type Config struct { CheckStatus bool `json:"-"` Alert string `json:"alert"` //info,success,warning,danger NoOverwriteUpload bool `json:"-"` // whether to support overwrite upload + ProxyRangeOption bool `json:"-"` } func (c Config) MustProxy() bool { diff --git a/internal/model/storage.go b/internal/model/storage.go index 1045a00767b..14bcf45f6e2 100644 --- a/internal/model/storage.go +++ b/internal/model/storage.go @@ -27,6 +27,7 @@ type Sort struct { type Proxy struct { WebProxy bool `json:"web_proxy"` WebdavPolicy string `json:"webdav_policy"` + ProxyRange bool `json:"proxy_range"` DownProxyUrl string `json:"down_proxy_url"` } diff --git a/internal/op/driver.go b/internal/op/driver.go index 66d1ae3ce9c..4f10e8e23c6 100644 --- a/internal/op/driver.go +++ b/internal/op/driver.go @@ -93,6 +93,17 @@ func getMainItems(config driver.Config) []driver.Item { Required: true, }, }...) + if config.ProxyRangeOption { + item := driver.Item{ + Name: "proxy_range", + Type: conf.TypeBool, + Help: "Need to enable proxy", + } + if config.Name == "139Yun" { + item.Default = "true" + } + items = append(items, item) + } } else { items = append(items, driver.Item{ Name: "webdav_policy", diff --git a/server/common/proxy.go b/server/common/proxy.go index 4ca4ba7f6ab..10923613ede 100644 --- a/server/common/proxy.go +++ b/server/common/proxy.go @@ -9,8 +9,10 @@ import ( "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/net" + "github.com/alist-org/alist/v3/internal/stream" "github.com/alist-org/alist/v3/pkg/http_range" "github.com/alist-org/alist/v3/pkg/utils" + log "github.com/sirupsen/logrus" ) func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.Obj) error { @@ -82,3 +84,21 @@ func attachFileName(w http.ResponseWriter, file model.Obj) { w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"; filename*=UTF-8''%s`, fileName, url.PathEscape(fileName))) w.Header().Set("Content-Type", utils.GetMimeType(fileName)) } + +var NoProxyRange = &model.RangeReadCloser{} + +func ProxyRange(link *model.Link, size int64) { + if link.MFile != nil { + return + } + if link.RangeReadCloser == nil { + var rrc, err = stream.GetRangeReadCloserFromLink(size, link) + if err != nil { + log.Warnf("ProxyRange error: %s", err) + return + } + link.RangeReadCloser = rrc + } else if link.RangeReadCloser == NoProxyRange { + link.RangeReadCloser = nil + } +} diff --git a/server/handles/down.go b/server/handles/down.go index d3d41e85a2b..0020ed1453e 100644 --- a/server/handles/down.go +++ b/server/handles/down.go @@ -106,6 +106,9 @@ func Proxy(c *gin.Context) { return } } + if storage.GetStorage().ProxyRange { + common.ProxyRange(link, file.GetSize()) + } err = common.Proxy(c.Writer, c.Request, link, file) if err != nil { common.ErrorResp(c, err, 500, true) diff --git a/server/webdav/webdav.go b/server/webdav/webdav.go index 6054991a0c2..b84e65b06b7 100644 --- a/server/webdav/webdav.go +++ b/server/webdav/webdav.go @@ -247,6 +247,9 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (sta if err != nil { return http.StatusInternalServerError, err } + if storage.GetStorage().ProxyRange { + common.ProxyRange(link, fi.GetSize()) + } err = common.Proxy(w, r, link, fi) if err != nil { log.Errorf("webdav proxy error: %+v", err)