proxy

This commit is contained in:
微凉
2021-10-29 00:02:02 +08:00
parent 0a50fbd080
commit f4969560d4
6 changed files with 49 additions and 0 deletions

View File

@ -1,8 +1,10 @@
package server
import (
"fmt"
"github.com/Xhofe/alist/utils"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/proxy"
log "github.com/sirupsen/logrus"
"net/url"
)
@ -28,3 +30,34 @@ func Down(ctx *fiber.Ctx) error {
return ctx.Redirect(link, 302)
}
}
func Proxy(ctx *fiber.Ctx) error {
rawPath, err:= url.QueryUnescape(ctx.Params("*"))
if err != nil {
return ErrorResp(ctx,err,500)
}
rawPath = utils.ParsePath(rawPath)
log.Debugf("down: %s",rawPath)
account, path, driver, err := ParsePath(rawPath)
if err != nil {
return ErrorResp(ctx, err, 500)
}
if !account.Proxy {
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)
}
if account.Type == "native" {
return ctx.SendFile(link)
} else {
driver.Proxy(ctx)
if err := proxy.Do(ctx, link); err != nil {
return ErrorResp(ctx,err,500)
}
// Remove Server header from response
ctx.Response().Header.Del(fiber.HeaderServer)
return nil
}
}

View File

@ -10,6 +10,8 @@ func InitApiRouter(app *fiber.App) {
// TODO from settings
app.Use(cors.New())
app.Get("/d/*", Down)
// TODO check allow proxy?
app.Get("/p/*", Proxy)
public := app.Group("/api/public")
{