alist/server/down.go

68 lines
1.7 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-07 15:05:50 +00:00
rawPath, err:= url.PathUnescape(ctx.Params("*"))
2021-10-28 14:50:09 +00:00
if err != nil {
return ErrorResp(ctx,err,500)
}
rawPath = utils.ParsePath(rawPath)
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-07 14:12:52 +00:00
rawPath, err:= url.PathUnescape(ctx.Params("*"))
2021-10-28 16:02:02 +00:00
if err != nil {
return ErrorResp(ctx,err,500)
}
rawPath = utils.ParsePath(rawPath)
2021-10-31 13:27:47 +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-10-31 13:27:47 +00:00
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath))!=conf.TEXT {
2021-10-28 16:02:02 +00:00
return ErrorResp(ctx,fmt.Errorf("[%s] not allowed proxy",account.Name),403)
}
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)
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
}
}