alist/server/handles/down.go

133 lines
3.3 KiB
Go
Raw Normal View History

2022-07-11 09:12:50 +00:00
package handles
2022-06-28 10:00:11 +00:00
import (
2022-06-28 13:58:46 +00:00
"fmt"
"io"
2022-06-28 10:12:53 +00:00
stdpath "path"
"strings"
2022-08-03 06:26:59 +00:00
"github.com/alist-org/alist/v3/internal/conf"
2022-06-28 10:00:11 +00:00
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/fs"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/setting"
2022-08-03 06:26:59 +00:00
"github.com/alist-org/alist/v3/internal/sign"
2022-06-28 10:00:11 +00:00
"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"
2022-06-28 10:00:11 +00:00
)
func Down(c *gin.Context) {
2022-06-28 13:58:46 +00:00
rawPath := c.MustGet("path").(string)
2022-06-28 10:00:11 +00:00
filename := stdpath.Base(rawPath)
storage, err := fs.GetStorage(rawPath, &fs.GetStoragesArgs{})
2022-06-28 10:00:11 +00:00
if err != nil {
2022-06-28 13:58:46 +00:00
common.ErrorResp(c, err, 500)
return
2022-06-28 10:00:11 +00:00
}
if common.ShouldProxy(storage, filename) {
2022-06-28 13:58:46 +00:00
Proxy(c)
return
} else {
link, _, err := fs.Link(c, rawPath, model.LinkArgs{
IP: c.ClientIP(),
Header: c.Request.Header,
2022-08-11 12:32:17 +00:00
Type: c.Query("type"),
2022-06-28 13:58:46 +00:00
})
2022-06-28 10:00:11 +00:00
if err != nil {
2022-06-28 13:58:46 +00:00
common.ErrorResp(c, err, 500)
2022-06-28 10:00:11 +00:00
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) {
query := c.Request.URL.Query()
query.Del("sign")
link.URL, err = utils.InjectQuery(link.URL, query)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
2022-06-28 13:58:46 +00:00
c.Redirect(302, link.URL)
2022-06-28 10:00:11 +00:00
}
2022-06-28 13:58:46 +00:00
}
func Proxy(c *gin.Context) {
rawPath := c.MustGet("path").(string)
filename := stdpath.Base(rawPath)
storage, err := fs.GetStorage(rawPath, &fs.GetStoragesArgs{})
2022-06-28 10:00:11 +00:00
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500)
2022-06-28 10:00:11 +00:00
return
}
2022-07-10 06:45:39 +00:00
if canProxy(storage, filename) {
downProxyUrl := storage.GetStorage().DownProxyUrl
2022-06-28 13:58:46 +00:00
if downProxyUrl != "" {
_, ok := c.GetQuery("d")
2022-09-09 12:54:11 +00:00
if !ok {
2022-08-11 12:32:17 +00:00
URL := fmt.Sprintf("%s%s?sign=%s",
strings.Split(downProxyUrl, "\n")[0],
2022-08-12 06:51:23 +00:00
utils.EncodePath(rawPath, true),
sign.Sign(rawPath))
2022-06-28 13:58:46 +00:00
c.Redirect(302, URL)
return
}
}
link, file, err := fs.Link(c, rawPath, model.LinkArgs{
2022-06-28 10:00:11 +00:00
Header: c.Request.Header,
2022-08-11 12:32:17 +00:00
Type: c.Query("type"),
2022-06-28 10:00:11 +00:00
})
if err != nil {
2022-06-28 10:12:53 +00:00
common.ErrorResp(c, err, 500)
2022-06-28 10:00:11 +00:00
return
}
if link.URL != "" && setting.GetBool(conf.ForwardDirectLinkParams) {
query := c.Request.URL.Query()
query.Del("sign")
link.URL, err = utils.InjectQuery(link.URL, query)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
}
2022-06-28 13:58:46 +00:00
err = common.Proxy(c.Writer, c.Request, link, file)
2022-06-28 10:00:11 +00:00
if err != nil {
common.ErrorResp(c, err, 500, true)
return
}
} else {
2022-06-28 13:58:46 +00:00
common.ErrorStrResp(c, "proxy not allowed", 403)
return
2022-06-28 10:00:11 +00:00
}
}
2022-06-28 13:58:46 +00:00
// TODO need optimize
// when can be proxy?
// 1. text file
// 2. config.MustProxy()
2022-07-10 06:45:39 +00:00
// 3. storage.WebProxy
2022-06-28 13:58:46 +00:00
// 4. proxy_types
// solution: text_file + shouldProxy()
2022-07-10 06:45:39 +00:00
func canProxy(storage driver.Driver, filename string) bool {
2022-09-09 12:54:11 +00:00
if storage.Config().MustProxy() || storage.GetStorage().WebProxy || storage.GetStorage().WebdavProxy() {
2022-06-28 10:00:11 +00:00
return true
}
if utils.SliceContains(conf.SlicesMap[conf.ProxyTypes], utils.Ext(filename)) {
2022-06-28 10:00:11 +00:00
return true
}
if utils.SliceContains(conf.SlicesMap[conf.TextTypes], utils.Ext(filename)) {
2022-06-28 13:58:46 +00:00
return true
}
2022-06-28 10:00:11 +00:00
return false
}