From f4969560d4dd46333c27ad528a0e215e16c7716e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <927625802@qq.com> Date: Fri, 29 Oct 2021 00:02:02 +0800 Subject: [PATCH] :sparkles: proxy --- drivers/alidrive.go | 6 ++++++ drivers/driver.go | 2 ++ drivers/native.go | 5 +++++ model/account.go | 1 + server/down.go | 33 +++++++++++++++++++++++++++++++++ server/router.go | 2 ++ 6 files changed, 49 insertions(+) diff --git a/drivers/alidrive.go b/drivers/alidrive.go index 6681eb93..0deace6d 100644 --- a/drivers/alidrive.go +++ b/drivers/alidrive.go @@ -6,6 +6,7 @@ import ( "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" "github.com/go-resty/resty/v2" + "github.com/gofiber/fiber/v2" "github.com/robfig/cron/v3" log "github.com/sirupsen/logrus" "path/filepath" @@ -25,6 +26,11 @@ func init() { type AliDrive struct { } +func (a AliDrive) Proxy(ctx *fiber.Ctx) { + ctx.Request().Header.Del("Origin") + ctx.Request().Header.Set("Referer", "https://www.aliyundrive.com/") +} + type AliRespError struct { Code string `json:"code"` Message string `json:"message"` diff --git a/drivers/driver.go b/drivers/driver.go index e2acab39..9fae5a61 100644 --- a/drivers/driver.go +++ b/drivers/driver.go @@ -3,12 +3,14 @@ package drivers import ( "encoding/json" "github.com/Xhofe/alist/model" + "github.com/gofiber/fiber/v2" ) type Driver interface { Path(path string, account *model.Account) (*model.File, []*model.File, error) Link(path string, account *model.Account) (string,error) Save(account *model.Account, old *model.Account) error + Proxy(ctx *fiber.Ctx) } var driversMap = map[string]Driver{} diff --git a/drivers/native.go b/drivers/native.go index 2da32b07..ccb535a3 100644 --- a/drivers/native.go +++ b/drivers/native.go @@ -5,6 +5,7 @@ import ( "github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/model" "github.com/Xhofe/alist/utils" + "github.com/gofiber/fiber/v2" log "github.com/sirupsen/logrus" "io/ioutil" "os" @@ -16,6 +17,10 @@ type Native struct { } +func (n Native) Proxy(ctx *fiber.Ctx) { + panic("implement me") +} + func (n Native) Save(account *model.Account, old *model.Account) error { log.Debugf("save a account: [%s]",account.Name) return nil diff --git a/model/account.go b/model/account.go index c9a51efb..cfeaabd8 100644 --- a/model/account.go +++ b/model/account.go @@ -18,6 +18,7 @@ type Account struct { Limit int `json:"limit"` OrderBy string `json:"order_by"` OrderDirection string `json:"order_direction"` + Proxy bool `json:"proxy"` } var accountsMap = map[string]Account{} diff --git a/server/down.go b/server/down.go index b1004088..3467a2c1 100644 --- a/server/down.go +++ b/server/down.go @@ -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 + } +} \ No newline at end of file diff --git a/server/router.go b/server/router.go index b1a38f91..b24cb0ef 100644 --- a/server/router.go +++ b/server/router.go @@ -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") {