mirror of https://github.com/Xhofe/alist
feat(alias): support proxy and direct together
parent
c4108007cd
commit
d9795ff22f
|
@ -2,12 +2,15 @@ package alias
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
stdpath "path"
|
stdpath "path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/internal/fs"
|
"github.com/alist-org/alist/v3/internal/fs"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
|
"github.com/alist-org/alist/v3/internal/sign"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *Alias) listRoot() []model.Obj {
|
func (d *Alias) listRoot() []model.Obj {
|
||||||
|
@ -76,6 +79,18 @@ func (d *Alias) list(ctx context.Context, dst, sub string) ([]model.Obj, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs) (*model.Link, error) {
|
func (d *Alias) link(ctx context.Context, dst, sub string, args model.LinkArgs) (*model.Link, error) {
|
||||||
link, _, err := fs.Link(ctx, stdpath.Join(dst, sub), args)
|
reqPath := stdpath.Join(dst, sub)
|
||||||
|
storage, err := fs.GetStorage(reqPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if common.ShouldProxy(storage, stdpath.Base(sub)) {
|
||||||
|
return &model.Link{
|
||||||
|
URL: fmt.Sprintf("/p%s?sign=%s",
|
||||||
|
utils.EncodePath(reqPath, true),
|
||||||
|
sign.Sign(reqPath)),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
link, _, err := fs.Link(ctx, reqPath, args)
|
||||||
return link, err
|
return link, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,7 +144,7 @@ func (d *Local) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (
|
||||||
}
|
}
|
||||||
srcBuf = videoBuf
|
srcBuf = videoBuf
|
||||||
} else {
|
} else {
|
||||||
imgData, err := ioutil.ReadFile(fullPath)
|
imgData, err := os.ReadFile(fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,12 @@ package fs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/internal/op"
|
"github.com/alist-org/alist/v3/internal/op"
|
||||||
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,5 +16,14 @@ func link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, m
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, errors.WithMessage(err, "failed get storage")
|
return nil, nil, errors.WithMessage(err, "failed get storage")
|
||||||
}
|
}
|
||||||
return op.Link(ctx, storage, actualPath, args)
|
l, obj, err := op.Link(ctx, storage, actualPath, args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, errors.WithMessage(err, "failed link")
|
||||||
|
}
|
||||||
|
if l.URL != "" && !strings.HasPrefix(l.URL, "http://") && !strings.HasPrefix(l.URL, "https://") {
|
||||||
|
if c, ok := ctx.(*gin.Context); ok {
|
||||||
|
l.URL = common.GetApiUrl(c.Request) + l.URL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return l, obj, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,8 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alist-org/alist/v3/internal/conf"
|
||||||
|
"github.com/alist-org/alist/v3/internal/driver"
|
||||||
"github.com/alist-org/alist/v3/internal/model"
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
)
|
)
|
||||||
|
@ -49,3 +51,18 @@ func CanAccess(user *model.User, meta *model.Meta, reqPath string, password stri
|
||||||
// validate password
|
// validate password
|
||||||
return meta.Password == password
|
return meta.Password == password
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ShouldProxy TODO need optimize
|
||||||
|
// when should be proxy?
|
||||||
|
// 1. config.MustProxy()
|
||||||
|
// 2. storage.WebProxy
|
||||||
|
// 3. proxy_types
|
||||||
|
func ShouldProxy(storage driver.Driver, filename string) bool {
|
||||||
|
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if utils.SliceContains(conf.SlicesMap[conf.ProxyTypes], utils.Ext(filename)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package handles
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
stdpath "path"
|
stdpath "path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -14,6 +15,7 @@ import (
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
"github.com/alist-org/alist/v3/server/common"
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Down(c *gin.Context) {
|
func Down(c *gin.Context) {
|
||||||
|
@ -24,11 +26,10 @@ func Down(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if shouldProxy(storage, filename) {
|
if common.ShouldProxy(storage, filename) {
|
||||||
Proxy(c)
|
Proxy(c)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
link, _, err := fs.Link(c, rawPath, model.LinkArgs{
|
link, _, err := fs.Link(c, rawPath, model.LinkArgs{
|
||||||
IP: c.ClientIP(),
|
IP: c.ClientIP(),
|
||||||
Header: c.Request.Header,
|
Header: c.Request.Header,
|
||||||
|
@ -38,6 +39,14 @@ func Down(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if link.Data != nil {
|
||||||
|
defer func(Data io.ReadCloser) {
|
||||||
|
err := Data.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("close data error: %s", err)
|
||||||
|
}
|
||||||
|
}(link.Data)
|
||||||
|
}
|
||||||
c.Header("Referrer-Policy", "no-referrer")
|
c.Header("Referrer-Policy", "no-referrer")
|
||||||
c.Header("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate")
|
c.Header("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate")
|
||||||
if setting.GetBool(conf.ForwardDirectLinkParams) {
|
if setting.GetBool(conf.ForwardDirectLinkParams) {
|
||||||
|
@ -102,21 +111,6 @@ func Proxy(c *gin.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO need optimize
|
|
||||||
// when should be proxy?
|
|
||||||
// 1. config.MustProxy()
|
|
||||||
// 2. storage.WebProxy
|
|
||||||
// 3. proxy_types
|
|
||||||
func shouldProxy(storage driver.Driver, filename string) bool {
|
|
||||||
if storage.Config().MustProxy() || storage.GetStorage().WebProxy {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if utils.SliceContains(conf.SlicesMap[conf.ProxyTypes], utils.Ext(filename)) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO need optimize
|
// TODO need optimize
|
||||||
// when can be proxy?
|
// when can be proxy?
|
||||||
// 1. text file
|
// 1. text file
|
||||||
|
|
|
@ -2,6 +2,7 @@ package handles
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
stdpath "path"
|
stdpath "path"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
@ -15,6 +16,7 @@ import (
|
||||||
"github.com/alist-org/alist/v3/server/common"
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MkdirOrLinkReq struct {
|
type MkdirOrLinkReq struct {
|
||||||
|
@ -379,6 +381,14 @@ func Link(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if link.Data != nil {
|
||||||
|
defer func(Data io.ReadCloser) {
|
||||||
|
err := Data.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("close link data error: %v", err)
|
||||||
|
}
|
||||||
|
}(link.Data)
|
||||||
|
}
|
||||||
common.SuccessResp(c, link)
|
common.SuccessResp(c, link)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue