Files
OpenList/server/controllers/down.go

157 lines
3.7 KiB
Go
Raw Normal View History

2021-12-07 15:56:43 +08:00
package controllers
2021-10-26 22:28:37 +08:00
2021-10-28 22:50:09 +08:00
import (
2021-10-29 00:02:02 +08:00
"fmt"
2021-10-31 21:27:47 +08:00
"github.com/Xhofe/alist/conf"
2021-12-09 19:24:34 +08:00
"github.com/Xhofe/alist/drivers/base"
2021-12-07 15:56:43 +08:00
"github.com/Xhofe/alist/server/common"
2021-10-28 22:50:09 +08:00
"github.com/Xhofe/alist/utils"
2021-11-13 15:53:26 +08:00
"github.com/gin-gonic/gin"
2021-11-15 19:06:10 +08:00
"github.com/go-resty/resty/v2"
2021-10-28 22:50:09 +08:00
log "github.com/sirupsen/logrus"
2021-12-08 10:35:18 +08:00
"net/http"
2021-11-13 16:02:19 +08:00
"net/http/httputil"
2021-10-28 22:50:09 +08:00
"net/url"
2021-12-08 10:35:18 +08:00
"os"
2021-10-31 21:27:47 +08:00
"path/filepath"
2021-11-13 16:02:19 +08:00
"strings"
2021-10-28 22:50:09 +08:00
)
2021-10-26 22:28:37 +08:00
2021-11-13 15:53:26 +08:00
func Down(c *gin.Context) {
rawPath := c.Param("path")
2021-10-28 22:50:09 +08:00
rawPath = utils.ParsePath(rawPath)
2021-11-09 16:03:04 +08:00
log.Debugf("down: %s", rawPath)
2021-12-07 15:56:43 +08:00
account, path, driver, err := common.ParsePath(rawPath)
2021-10-26 22:28:37 +08:00
if err != nil {
2021-12-07 15:56:43 +08:00
common.ErrorResp(c, err, 500)
2021-11-13 15:53:26 +08:00
return
2021-10-26 22:28:37 +08:00
}
2021-12-08 10:33:26 +08:00
if driver.Config().OnlyProxy || account.Proxy {
2021-11-17 17:20:57 +08:00
Proxy(c)
return
}
2021-10-26 22:28:37 +08:00
link, err := driver.Link(path, account)
if err != nil {
2021-12-07 15:56:43 +08:00
common.ErrorResp(c, err, 500)
2021-11-13 15:53:26 +08:00
return
2021-10-26 22:28:37 +08:00
}
2021-12-09 19:24:34 +08:00
c.Redirect(302, link.Url)
2021-12-07 15:56:43 +08:00
return
2021-10-26 22:28:37 +08:00
}
2021-10-29 00:02:02 +08:00
2021-11-13 15:53:26 +08:00
func Proxy(c *gin.Context) {
rawPath := c.Param("path")
2021-10-29 00:02:02 +08:00
rawPath = utils.ParsePath(rawPath)
2021-11-09 16:03:04 +08:00
log.Debugf("proxy: %s", rawPath)
2021-12-07 15:56:43 +08:00
account, path, driver, err := common.ParsePath(rawPath)
2021-10-29 00:02:02 +08:00
if err != nil {
2021-12-07 15:56:43 +08:00
common.ErrorResp(c, err, 500)
2021-11-13 15:53:26 +08:00
return
2021-10-29 00:02:02 +08:00
}
2021-12-19 17:10:20 +08:00
// 只有以下几种情况允许中转:
2021-12-08 20:00:52 +08:00
// 1. 账号开启中转
// 2. driver只能中转
// 3. 是文本类型文件
2021-12-19 17:10:20 +08:00
// 4. 开启webdav中转需要验证sign
2021-12-08 20:00:52 +08:00
if !account.Proxy && !driver.Config().OnlyProxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
2021-12-19 17:10:20 +08:00
// 只开启了webdav中转验证sign
ok := false
if account.WebdavProxy {
_, ok = c.Get("sign")
}
if !ok {
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
return
}
2021-12-08 20:00:52 +08:00
}
2021-12-10 15:55:21 +08:00
// 中转时有中转机器使用中转机器,若携带标志位则表明不能再走中转机器了
2021-12-19 17:10:20 +08:00
if account.DownProxyUrl != "" && c.Param("d") != "1" {
2021-12-08 10:33:26 +08:00
name := utils.Base(rawPath)
2021-12-19 17:10:20 +08:00
link := fmt.Sprintf("%s%s?sign=%s", account.DownProxyUrl, rawPath, utils.SignWithToken(name, conf.Token))
2021-12-08 10:33:26 +08:00
c.Redirect(302, link)
return
}
2021-10-29 00:02:02 +08:00
link, err := driver.Link(path, account)
if err != nil {
2021-12-07 15:56:43 +08:00
common.ErrorResp(c, err, 500)
2021-11-13 15:53:26 +08:00
return
2021-10-29 00:02:02 +08:00
}
2021-12-16 18:03:58 +08:00
// 本机读取数据
if account.Type == "FTP" {
c.Data(http.StatusOK, "application/octet-stream", link.Data)
2021-12-17 12:14:27 +08:00
return
2021-12-16 18:03:58 +08:00
}
2021-12-08 20:00:52 +08:00
// 本机文件直接返回文件
2021-10-29 14:50:26 +08:00
if account.Type == "Native" {
2021-12-08 20:00:52 +08:00
// 对于名称为index.html的文件需要特殊处理
2021-12-08 10:35:18 +08:00
if utils.Base(rawPath) == "index.html" {
2021-12-09 19:24:34 +08:00
file, err := os.Open(link.Url)
2021-12-08 10:35:18 +08:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
defer func() {
_ = file.Close()
}()
2021-12-09 19:24:34 +08:00
fileStat, err := os.Stat(link.Url)
2021-12-08 10:35:18 +08:00
if err != nil {
common.ErrorResp(c, err, 500)
return
}
http.ServeContent(c.Writer, c.Request, utils.Base(rawPath), fileStat.ModTime(), file)
return
}
2021-12-09 19:24:34 +08:00
c.File(link.Url)
2021-11-13 15:53:26 +08:00
return
2021-10-29 00:02:02 +08:00
} else {
2021-11-15 19:06:10 +08:00
if utils.GetFileType(filepath.Ext(rawPath)) == conf.TEXT {
Text(c, link)
return
}
2021-11-17 22:19:11 +08:00
driver.Proxy(c, account)
2021-11-13 16:02:19 +08:00
r := c.Request
w := c.Writer
2021-12-09 19:24:34 +08:00
target, err := url.Parse(link.Url)
2021-11-13 16:02:19 +08:00
if err != nil {
2021-12-07 15:56:43 +08:00
common.ErrorResp(c, err, 500)
2021-11-13 16:02:19 +08:00
return
}
protocol := "http://"
2021-12-09 19:24:34 +08:00
if strings.HasPrefix(link.Url, "https://") {
2021-11-13 16:02:19 +08:00
protocol = "https://"
}
targetHost, err := url.Parse(fmt.Sprintf("%s%s", protocol, target.Host))
proxy := httputil.NewSingleHostReverseProxy(targetHost)
r.URL = target
r.Host = target.Host
proxy.ServeHTTP(w, r)
2021-10-29 00:02:02 +08:00
}
2021-11-13 15:53:26 +08:00
}
2021-11-15 19:06:10 +08:00
var client *resty.Client
func init() {
client = resty.New()
client.SetRetryCount(3)
}
2021-12-09 19:24:34 +08:00
func Text(c *gin.Context, link *base.Link) {
res, err := client.R().Get(link.Url)
2021-11-16 00:03:49 +08:00
if err != nil {
2021-12-07 15:56:43 +08:00
common.ErrorResp(c, err, 500)
2021-11-16 00:03:49 +08:00
return
}
2021-11-15 19:20:39 +08:00
text := res.String()
2021-11-16 00:03:49 +08:00
t := utils.GetStrCoding(res.Body())
log.Debugf("text type: %s", t)
if t != utils.UTF8 {
2021-11-15 19:20:39 +08:00
body, err := utils.GbkToUtf8(res.Body())
if err != nil {
2021-12-07 15:56:43 +08:00
common.ErrorResp(c, err, 500)
2021-11-15 19:20:39 +08:00
return
}
text = string(body)
}
2021-11-16 00:03:49 +08:00
c.String(200, text)
2021-11-15 19:06:10 +08:00
}