feat(alias): support proxy and direct together

This commit is contained in:
Andy Hsu
2023-03-14 13:38:41 +08:00
parent c4108007cd
commit d9795ff22f
6 changed files with 68 additions and 20 deletions

View File

@ -5,6 +5,8 @@ import (
"regexp"
"strings"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
)
@ -49,3 +51,18 @@ func CanAccess(user *model.User, meta *model.Meta, reqPath string, password stri
// validate password
return meta.Password == password
}
// ShouldProxy TODO need optimize
// when should be proxy?
// 1. config.MustProxy()
// 2. storage.WebProxy
// 3. proxy_types
func ShouldProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
return true
}
if utils.SliceContains(conf.SlicesMap[conf.ProxyTypes], utils.Ext(filename)) {
return true
}
return false
}

View File

@ -2,6 +2,7 @@ package handles
import (
"fmt"
"io"
stdpath "path"
"strings"
@ -14,6 +15,7 @@ import (
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func Down(c *gin.Context) {
@ -24,11 +26,10 @@ func Down(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
if shouldProxy(storage, filename) {
if common.ShouldProxy(storage, filename) {
Proxy(c)
return
} else {
link, _, err := fs.Link(c, rawPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
@ -38,6 +39,14 @@ func Down(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
if link.Data != nil {
defer func(Data io.ReadCloser) {
err := Data.Close()
if err != nil {
log.Errorf("close data error: %s", err)
}
}(link.Data)
}
c.Header("Referrer-Policy", "no-referrer")
c.Header("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate")
if setting.GetBool(conf.ForwardDirectLinkParams) {
@ -102,21 +111,6 @@ func Proxy(c *gin.Context) {
}
}
// TODO need optimize
// when should be proxy?
// 1. config.MustProxy()
// 2. storage.WebProxy
// 3. proxy_types
func shouldProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
return true
}
if utils.SliceContains(conf.SlicesMap[conf.ProxyTypes], utils.Ext(filename)) {
return true
}
return false
}
// TODO need optimize
// when can be proxy?
// 1. text file

View File

@ -2,6 +2,7 @@ package handles
import (
"fmt"
"io"
stdpath "path"
"regexp"
@ -15,6 +16,7 @@ import (
"github.com/alist-org/alist/v3/server/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type MkdirOrLinkReq struct {
@ -379,6 +381,14 @@ func Link(c *gin.Context) {
common.ErrorResp(c, err, 500)
return
}
if link.Data != nil {
defer func(Data io.ReadCloser) {
err := Data.Close()
if err != nil {
log.Errorf("close link data error: %v", err)
}
}(link.Data)
}
common.SuccessResp(c, link)
return
}