-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
157 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package common | ||
|
||
import ( | ||
"github.com/alist-org/alist/v3/internal/model" | ||
"github.com/alist-org/alist/v3/internal/sign" | ||
) | ||
|
||
func Sign(obj model.Obj) string { | ||
if obj.IsDir() { | ||
return "" | ||
} | ||
return sign.Sign(obj.GetName()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,101 +1,114 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
"github.com/alist-org/alist/v3/internal/sign" | ||
stdpath "path" | ||
"strings" | ||
|
||
"github.com/alist-org/alist/v3/internal/db" | ||
"github.com/alist-org/alist/v3/internal/driver" | ||
"github.com/alist-org/alist/v3/internal/errs" | ||
"github.com/alist-org/alist/v3/internal/fs" | ||
"github.com/alist-org/alist/v3/internal/model" | ||
"github.com/alist-org/alist/v3/internal/setting" | ||
"github.com/alist-org/alist/v3/internal/sign" | ||
"github.com/alist-org/alist/v3/pkg/utils" | ||
"github.com/alist-org/alist/v3/server/common" | ||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func Down(c *gin.Context) { | ||
rawPath := parsePath(c.Param("path")) | ||
rawPath := c.MustGet("path").(string) | ||
filename := stdpath.Base(rawPath) | ||
meta, err := db.GetNearestMeta(rawPath) | ||
account, err := fs.GetAccount(rawPath) | ||
if err != nil { | ||
if !errors.Is(errors.Cause(err), errs.MetaNotFound) { | ||
common.ErrorResp(c, err, 500, true) | ||
return | ||
} | ||
common.ErrorResp(c, err, 500) | ||
return | ||
} | ||
// verify sign | ||
if needSign(meta, rawPath) { | ||
s := c.Param("sign") | ||
err = sign.Verify(filename, s) | ||
if shouldProxy(account, filename) { | ||
Proxy(c) | ||
return | ||
} else { | ||
link, _, err := fs.Link(c, rawPath, model.LinkArgs{ | ||
IP: c.ClientIP(), | ||
Header: c.Request.Header, | ||
}) | ||
if err != nil { | ||
common.ErrorResp(c, err, 401) | ||
common.ErrorResp(c, err, 500) | ||
return | ||
} | ||
c.Redirect(302, link.URL) | ||
} | ||
} | ||
|
||
func Proxy(c *gin.Context) { | ||
rawPath := c.MustGet("path").(string) | ||
filename := stdpath.Base(rawPath) | ||
account, err := fs.GetAccount(rawPath) | ||
if err != nil { | ||
common.ErrorResp(c, err, 500) | ||
return | ||
} | ||
if needProxy(account, filename) { | ||
link, err := fs.Link(c, rawPath, model.LinkArgs{ | ||
if canProxy(account, filename) { | ||
downProxyUrl := account.GetAccount().DownProxyUrl | ||
if downProxyUrl != "" { | ||
_, ok := c.GetQuery("d") | ||
if ok { | ||
URL := fmt.Sprintf("%s%s?sign=%s", strings.Split(downProxyUrl, "\n")[0], rawPath, sign.Sign(filename)) | ||
c.Redirect(302, URL) | ||
return | ||
} | ||
} | ||
link, file, err := fs.Link(c, rawPath, model.LinkArgs{ | ||
Header: c.Request.Header, | ||
}) | ||
if err != nil { | ||
common.ErrorResp(c, err, 500) | ||
return | ||
} | ||
obj, err := fs.Get(c, rawPath) | ||
if err != nil { | ||
common.ErrorResp(c, err, 500) | ||
return | ||
} | ||
err = common.Proxy(c.Writer, c.Request, link, obj) | ||
err = common.Proxy(c.Writer, c.Request, link, file) | ||
if err != nil { | ||
common.ErrorResp(c, err, 500, true) | ||
return | ||
} | ||
} else { | ||
link, err := fs.Link(c, rawPath, model.LinkArgs{ | ||
IP: c.ClientIP(), | ||
Header: c.Request.Header, | ||
}) | ||
if err != nil { | ||
common.ErrorResp(c, err, 500) | ||
return | ||
} | ||
c.Redirect(302, link.URL) | ||
common.ErrorStrResp(c, "proxy not allowed", 403) | ||
return | ||
} | ||
} | ||
|
||
// TODO: implement | ||
// path maybe contains # ? etc. | ||
func parsePath(path string) string { | ||
return utils.StandardizePath(path) | ||
} | ||
|
||
func needSign(meta *model.Meta, path string) bool { | ||
if meta == nil || meta.Password == "" { | ||
return false | ||
// TODO need optimize | ||
// when should be proxy? | ||
// 1. config.MustProxy() | ||
// 2. account.WebProxy | ||
// 3. proxy_types | ||
func shouldProxy(account driver.Driver, filename string) bool { | ||
if account.Config().MustProxy() || account.GetAccount().WebProxy { | ||
return true | ||
} | ||
if !meta.SubFolder && path != meta.Path { | ||
return false | ||
proxyTypes := setting.GetByKey("proxy_types") | ||
if strings.Contains(proxyTypes, utils.Ext(filename)) { | ||
return true | ||
} | ||
return true | ||
return false | ||
} | ||
|
||
func needProxy(account driver.Driver, filename string) bool { | ||
config := account.Config() | ||
if config.MustProxy() { | ||
// TODO need optimize | ||
// when can be proxy? | ||
// 1. text file | ||
// 2. config.MustProxy() | ||
// 3. account.WebProxy | ||
// 4. proxy_types | ||
// solution: text_file + shouldProxy() | ||
func canProxy(account driver.Driver, filename string) bool { | ||
if account.Config().MustProxy() || account.GetAccount().WebProxy { | ||
return true | ||
} | ||
proxyTypes := setting.GetByKey("proxy_types") | ||
if strings.Contains(proxyTypes, utils.Ext(filename)) { | ||
return true | ||
} | ||
textTypes := setting.GetByKey("text_types") | ||
if strings.Contains(textTypes, utils.Ext(filename)) { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package middlewares | ||
|
||
import ( | ||
"github.com/alist-org/alist/v3/internal/db" | ||
"github.com/alist-org/alist/v3/internal/errs" | ||
"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/server/common" | ||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
stdpath "path" | ||
) | ||
|
||
func Down(c *gin.Context) { | ||
rawPath := parsePath(c.Param("path")) | ||
c.Set("path", rawPath) | ||
filename := stdpath.Base(rawPath) | ||
meta, err := db.GetNearestMeta(rawPath) | ||
if err != nil { | ||
if !errors.Is(errors.Cause(err), errs.MetaNotFound) { | ||
common.ErrorResp(c, err, 500, true) | ||
return | ||
} | ||
} | ||
c.Set("meta", meta) | ||
// verify sign | ||
if needSign(meta, rawPath) { | ||
s := c.Param("sign") | ||
err = sign.Verify(filename, s) | ||
if err != nil { | ||
common.ErrorResp(c, err, 401) | ||
c.Abort() | ||
return | ||
} | ||
} | ||
c.Next() | ||
} | ||
|
||
// TODO: implement | ||
// path maybe contains # ? etc. | ||
func parsePath(path string) string { | ||
return utils.StandardizePath(path) | ||
} | ||
|
||
func needSign(meta *model.Meta, path string) bool { | ||
if meta == nil || meta.Password == "" { | ||
return false | ||
} | ||
if !meta.SubFolder && path != meta.Path { | ||
return false | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters