alist/server/handles/down.go

139 lines
3.5 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,
Type: c.Query("type"),
HttpReq: c.Request,
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.MFile != nil {
feat: Crypt driver, improve http/webdav handling (#4884) this PR has several enhancements, fixes, and features: - [x] Crypt: a transparent encryption driver. Anyone can easily, and safely store encrypted data on the remote storage provider. Consider your data is safely stored in the safe, and the storage provider can only see the safe, but not your data. - [x] Optional: compatible with [Rclone Crypt](https://rclone.org/crypt/). More ways to manipulate the encrypted data. - [x] directory and filename encryption - [x] server-side encryption mode (server encrypts & decrypts all data, all data flows thru the server) - [x] obfuscate sensitive information internally - [x] introduced a server memory-cached multi-thread downloader. - [x] Driver: **Quark** enabled this feature, faster load in any single thread scenario. e.g. media player directly playing from the link, now it's faster. - [x] general improvement on HTTP/WebDAV stream processing & header handling & response handling - [x] Driver: **Mega** driver support ranged http header - [x] Driver: **Quark** fix bug of not closing HTTP request to Quark server while user end has closed connection to alist ## Crypt, a transparent Encrypt/Decrypt Driver. (Rclone Crypt compatible) e.g. Crypt mount path -> /vault Crypt remote path -> /ali/encrypted Aliyun mount paht -> /ali when the user uploads a.jpg to /vault, the data will be encrypted and saved to /ali/encrypted/xxxxx. And when the user wants to access a.jpg, it's automatically decrypted, and the user can do anything with it. Since it's Rclone Crypt compatible, users can download /ali/encrypted/xxxxx and decrypt it with rclone crypt tool. Or the user can mount this folder using rclone, then mount the decrypted folder in Linux... NB. Some breaking changes is made to make it follow global standard, e.g. processing the HTTP header properly. close #4679 close #4827 Co-authored-by: Sean He <866155+seanhe26@users.noreply.github.com> Co-authored-by: Andy Hsu <i@nn.ci>
2023-08-02 06:40:36 +00:00
defer func(ReadSeekCloser io.ReadCloser) {
err := ReadSeekCloser.Close()
if err != nil {
log.Errorf("close data error: %s", err)
}
}(link.MFile)
}
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()
for _, v := range conf.SlicesMap[conf.IgnoreDirectLinkParams] {
query.Del(v)
}
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{
Header: c.Request.Header,
Type: c.Query("type"),
HttpReq: c.Request,
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()
for _, v := range conf.SlicesMap[conf.IgnoreDirectLinkParams] {
query.Del(v)
}
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
}