Files
OpenList/server/controllers/down.go

143 lines
3.2 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-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-07 15:56:43 +08:00
c.Redirect(302, link)
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-08 20:00:52 +08:00
// 只有三种情况允许中转:
// 1. 账号开启中转
// 2. driver只能中转
// 3. 是文本类型文件
if !account.Proxy && !driver.Config().OnlyProxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
return
}
// 中转时有中转机器使用中转机器
2021-12-08 10:33:26 +08:00
if account.ProxyUrl != "" {
name := utils.Base(rawPath)
link := fmt.Sprintf("%s%s?sign=%s", account.ProxyUrl, rawPath, utils.SignWithToken(name, conf.Token))
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-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" {
file, err := os.Open(link)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
defer func() {
_ = file.Close()
}()
fileStat, err := os.Stat(link)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
http.ServeContent(c.Writer, c.Request, utils.Base(rawPath), fileStat.ModTime(), file)
return
}
2021-11-13 15:53:26 +08:00
c.File(link)
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
target, err := url.Parse(link)
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://"
if strings.HasPrefix(link, "https://") {
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)
}
func Text(c *gin.Context, link string) {
res, err := client.R().Get(link)
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
}