proxy

pull/548/head
微凉 2021-10-29 00:02:02 +08:00
parent 0a50fbd080
commit f4969560d4
6 changed files with 49 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
"github.com/gofiber/fiber/v2"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"path/filepath" "path/filepath"
@ -25,6 +26,11 @@ func init() {
type AliDrive struct { 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 { type AliRespError struct {
Code string `json:"code"` Code string `json:"code"`
Message string `json:"message"` Message string `json:"message"`

View File

@ -3,12 +3,14 @@ package drivers
import ( import (
"encoding/json" "encoding/json"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/gofiber/fiber/v2"
) )
type Driver interface { type Driver interface {
Path(path string, account *model.Account) (*model.File, []*model.File, error) Path(path string, account *model.Account) (*model.File, []*model.File, error)
Link(path string, account *model.Account) (string,error) Link(path string, account *model.Account) (string,error)
Save(account *model.Account, old *model.Account) error Save(account *model.Account, old *model.Account) error
Proxy(ctx *fiber.Ctx)
} }
var driversMap = map[string]Driver{} var driversMap = map[string]Driver{}

View File

@ -5,6 +5,7 @@ import (
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"io/ioutil" "io/ioutil"
"os" "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 { func (n Native) Save(account *model.Account, old *model.Account) error {
log.Debugf("save a account: [%s]",account.Name) log.Debugf("save a account: [%s]",account.Name)
return nil return nil

View File

@ -18,6 +18,7 @@ type Account struct {
Limit int `json:"limit"` Limit int `json:"limit"`
OrderBy string `json:"order_by"` OrderBy string `json:"order_by"`
OrderDirection string `json:"order_direction"` OrderDirection string `json:"order_direction"`
Proxy bool `json:"proxy"`
} }
var accountsMap = map[string]Account{} var accountsMap = map[string]Account{}

View File

@ -1,8 +1,10 @@
package server package server
import ( import (
"fmt"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/proxy"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/url" "net/url"
) )
@ -28,3 +30,34 @@ func Down(ctx *fiber.Ctx) error {
return ctx.Redirect(link, 302) 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 // TODO from settings
app.Use(cors.New()) app.Use(cors.New())
app.Get("/d/*", Down) app.Get("/d/*", Down)
// TODO check allow proxy?
app.Get("/p/*", Proxy)
public := app.Group("/api/public") public := app.Group("/api/public")
{ {