alist/server/down.go

82 lines
2.2 KiB
Go
Raw Normal View History

2021-10-26 14:28:37 +00:00
package server
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-10-28 14:50:09 +00:00
"github.com/Xhofe/alist/utils"
"github.com/gofiber/fiber/v2"
2021-10-28 16:02:02 +00:00
"github.com/gofiber/fiber/v2/middleware/proxy"
2021-10-28 14:50:09 +00:00
log "github.com/sirupsen/logrus"
"net/url"
2021-10-31 13:27:47 +00:00
"path/filepath"
2021-10-28 14:50:09 +00:00
)
2021-10-26 14:28:37 +00:00
func Down(ctx *fiber.Ctx) error {
2021-11-09 08:03:04 +00:00
rawPath, err := url.PathUnescape(ctx.Params("*"))
2021-10-28 14:50:09 +00:00
if err != nil {
2021-11-09 08:03:04 +00:00
return ErrorResp(ctx, err, 500)
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-10-26 14:28:37 +00:00
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
link, err := driver.Link(path, account)
if err != nil {
return ErrorResp(ctx, err, 500)
}
2021-10-29 06:50:26 +00:00
if account.Type == "Native" {
2021-10-26 14:28:37 +00:00
return ctx.SendFile(link)
} else {
return ctx.Redirect(link, 302)
}
}
2021-10-28 16:02:02 +00:00
func Proxy(ctx *fiber.Ctx) error {
2021-11-09 08:03:04 +00:00
rawPath, err := url.PathUnescape(ctx.Params("*"))
2021-10-28 16:02:02 +00:00
if err != nil {
2021-11-09 08:03:04 +00:00
return ErrorResp(ctx, err, 500)
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-10-28 16:02:02 +00:00
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
2021-11-09 08:03:04 +00:00
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
return ErrorResp(ctx, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
2021-10-28 16:02:02 +00:00
}
link, err := driver.Link(path, account)
if err != nil {
return ErrorResp(ctx, err, 500)
}
2021-10-29 06:50:26 +00:00
if account.Type == "Native" {
2021-10-28 16:02:02 +00:00
return ctx.SendFile(link)
} else {
driver.Proxy(ctx)
2021-11-09 08:03:04 +00:00
//ctx.Response().ImmediateHeaderFlush = true
//var ProxyNetHttp = fasthttpadaptor.NewFastHTTPHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// target, _ := url.Parse(link)
// protocol := "https://"
// if strings.HasPrefix(link, "http://") {
// protocol = "http://"
// }
// targetHost, _ := url.Parse(fmt.Sprintf("%s%s", protocol, target.Host))
// proxy := httputil.NewSingleHostReverseProxy(targetHost)
// r.URL = target
// r.Host = target.Host
// proxy.ServeHTTP(w, r)
//})
//ProxyNetHttp(ctx.Context())
2021-10-28 16:02:02 +00:00
if err := proxy.Do(ctx, link); err != nil {
2021-10-31 13:27:47 +00:00
log.Errorf("proxy error: %s", err)
2021-10-28 16:02:02 +00:00
return ErrorResp(ctx,err,500)
}
// Remove Server header from response
ctx.Response().Header.Del(fiber.HeaderServer)
2021-10-31 13:27:47 +00:00
ctx.Set("Access-Control-Allow-Origin","*")
log.Debugf("proxy hedaer: %+v", ctx.Response().Header.String())
2021-10-28 16:02:02 +00:00
return nil
}
}