feat: custom hide error message by regexp (close #1468)

This commit is contained in:
Noah Hsu
2022-08-08 12:52:54 +08:00
parent d6437a337f
commit 2b04cf4ac3
8 changed files with 144 additions and 20 deletions

View File

@ -1,11 +1,23 @@
package common
import (
"strings"
"github.com/alist-org/alist/v3/cmd/flags"
"github.com/alist-org/alist/v3/internal/conf"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func hidePrivacy(msg string) string {
for _, r := range conf.PrivacyReg {
msg = r.ReplaceAllStringFunc(msg, func(s string) string {
return strings.Repeat("*", len(s))
})
}
return msg
}
// ErrorResp is used to return error response
// @param l: if true, log error
func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
@ -18,7 +30,7 @@ func ErrorResp(c *gin.Context, err error, code int, l ...bool) {
}
c.JSON(200, Resp{
Code: code,
Message: err.Error(),
Message: hidePrivacy(err.Error()),
Data: nil,
})
c.Abort()
@ -30,7 +42,7 @@ func ErrorStrResp(c *gin.Context, str string, code int, l ...bool) {
}
c.JSON(200, Resp{
Code: code,
Message: str,
Message: hidePrivacy(str),
Data: nil,
})
c.Abort()

View File

@ -1,6 +1,7 @@
package server
import (
"github.com/alist-org/alist/v3/server/common"
"github.com/alist-org/alist/v3/server/middlewares"
"github.com/gin-gonic/gin"
)
@ -12,4 +13,7 @@ func dev(g *gin.RouterGroup) {
"path": rawPath,
})
})
g.GET("/hide_privacy", func(ctx *gin.Context) {
common.ErrorStrResp(ctx, "This is ip: 1.1.1.1", 400)
})
}