alist/server/handles/down.go

156 lines
3.7 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"
"net/url"
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"
)
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)
2022-07-10 06:45:39 +00:00
storage, err := fs.GetStorage(rawPath)
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
}
2022-07-10 06:45:39 +00:00
if shouldProxy(storage, filename) {
2022-06-28 13:58:46 +00:00
Proxy(c)
return
} else {
2022-06-28 13:58:46 +00:00
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
}
c.Header("Referrer-Policy", "no-referrer")
c.Header("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate")
if setting.GetBool(conf.ForwardDirectLinkParams) {
params := c.Request.URL.Query()
params.Del("sign")
u, err := url.Parse(link.URL)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
values := u.Query()
for k := range params {
for i := range params[k] {
values.Set(k, params[k][i])
}
}
u.RawQuery = values.Encode()
link.URL = u.String()
}
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)
2022-07-10 06:45:39 +00:00
storage, err := fs.GetStorage(rawPath)
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) {
params := c.Request.URL.Query()
params.Del("sign")
u, err := url.Parse(link.URL)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
values := u.Query()
for k := range params {
for i := range params[k] {
values.Set(k, params[k][i])
}
}
u.RawQuery = values.Encode()
link.URL = u.String()
}
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 should be proxy?
// 1. config.MustProxy()
2022-07-10 06:45:39 +00:00
// 2. storage.WebProxy
2022-06-28 13:58:46 +00:00
// 3. proxy_types
2022-07-10 06:45:39 +00:00
func shouldProxy(storage driver.Driver, filename string) bool {
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
2022-06-28 13:58:46 +00:00
return true
2022-06-28 10:00:11 +00:00
}
if utils.SliceContains(conf.SlicesMap[conf.ProxyTypes], utils.Ext(filename)) {
2022-06-28 13:58:46 +00:00
return true
2022-06-28 10:00:11 +00:00
}
2022-06-28 13:58:46 +00:00
return false
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
}