alist/server/controllers/down.go

117 lines
2.5 KiB
Go
Raw Normal View History

2021-12-07 07:56:43 +00:00
package controllers
2021-10-26 14:28:37 +00:00
2021-10-28 14:50:09 +00:00
import (
2021-10-28 16:02:02 +00:00
"fmt"
2021-10-31 13:27:47 +00:00
"github.com/Xhofe/alist/conf"
2021-12-07 07:56:43 +00:00
"github.com/Xhofe/alist/server/common"
2021-10-28 14:50:09 +00:00
"github.com/Xhofe/alist/utils"
2021-11-13 07:53:26 +00:00
"github.com/gin-gonic/gin"
2021-11-15 11:06:10 +00:00
"github.com/go-resty/resty/v2"
2021-10-28 14:50:09 +00:00
log "github.com/sirupsen/logrus"
2021-11-13 08:02:19 +00:00
"net/http/httputil"
2021-10-28 14:50:09 +00:00
"net/url"
2021-10-31 13:27:47 +00:00
"path/filepath"
2021-11-13 08:02:19 +00:00
"strings"
2021-10-28 14:50:09 +00:00
)
2021-10-26 14:28:37 +00:00
2021-11-13 07:53:26 +00:00
func Down(c *gin.Context) {
rawPath := c.Param("path")
2021-10-28 14:50:09 +00:00
rawPath = utils.ParsePath(rawPath)
2021-11-09 08:03:04 +00:00
log.Debugf("down: %s", rawPath)
2021-12-07 07:56:43 +00:00
account, path, driver, err := common.ParsePath(rawPath)
2021-10-26 14:28:37 +00:00
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 07:53:26 +00:00
return
2021-10-26 14:28:37 +00:00
}
2021-12-08 02:33:26 +00:00
if driver.Config().OnlyProxy || account.Proxy {
2021-11-17 09:20:57 +00:00
Proxy(c)
return
}
2021-10-26 14:28:37 +00:00
link, err := driver.Link(path, account)
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 07:53:26 +00:00
return
2021-10-26 14:28:37 +00:00
}
2021-12-07 07:56:43 +00:00
c.Redirect(302, link)
return
2021-10-26 14:28:37 +00:00
}
2021-10-28 16:02:02 +00:00
2021-11-13 07:53:26 +00:00
func Proxy(c *gin.Context) {
rawPath := c.Param("path")
2021-10-28 16:02:02 +00:00
rawPath = utils.ParsePath(rawPath)
2021-11-09 08:03:04 +00:00
log.Debugf("proxy: %s", rawPath)
2021-12-07 07:56:43 +00:00
account, path, driver, err := common.ParsePath(rawPath)
2021-10-28 16:02:02 +00:00
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 07:53:26 +00:00
return
2021-10-28 16:02:02 +00:00
}
2021-12-08 02:33:26 +00: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-11-09 08:03:04 +00:00
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
2021-11-13 07:53:26 +00:00
return
2021-10-28 16:02:02 +00:00
}
link, err := driver.Link(path, account)
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 07:53:26 +00:00
return
2021-10-28 16:02:02 +00:00
}
2021-10-29 06:50:26 +00:00
if account.Type == "Native" {
2021-11-13 07:53:26 +00:00
c.File(link)
return
2021-10-28 16:02:02 +00:00
} else {
2021-11-15 11:06:10 +00:00
if utils.GetFileType(filepath.Ext(rawPath)) == conf.TEXT {
Text(c, link)
return
}
2021-11-17 14:19:11 +00:00
driver.Proxy(c, account)
2021-11-13 08:02:19 +00:00
r := c.Request
w := c.Writer
target, err := url.Parse(link)
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-13 08:02:19 +00: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-28 16:02:02 +00:00
}
2021-11-13 07:53:26 +00:00
}
2021-11-15 11:06:10 +00: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-15 16:03:49 +00:00
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-15 16:03:49 +00:00
return
}
2021-11-15 11:20:39 +00:00
text := res.String()
2021-11-15 16:03:49 +00:00
t := utils.GetStrCoding(res.Body())
log.Debugf("text type: %s", t)
if t != utils.UTF8 {
2021-11-15 11:20:39 +00:00
body, err := utils.GbkToUtf8(res.Body())
if err != nil {
2021-12-07 07:56:43 +00:00
common.ErrorResp(c, err, 500)
2021-11-15 11:20:39 +00:00
return
}
text = string(body)
}
2021-11-15 16:03:49 +00:00
c.String(200, text)
2021-11-15 11:06:10 +00:00
}