mirror of https://github.com/Xhofe/alist
feat: custom hide error message by regexp (close #1468)
parent
d6437a337f
commit
2b04cf4ac3
|
@ -47,7 +47,6 @@ func initSettings() {
|
|||
log.Fatalf("failed get setting: %+v", err)
|
||||
}
|
||||
}
|
||||
db.ResetTypeMap()
|
||||
}
|
||||
|
||||
func isActive(key string) bool {
|
||||
|
@ -93,6 +92,9 @@ func initialSettings() {
|
|||
{Key: conf.CustomizeHead, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
|
||||
{Key: conf.CustomizeBody, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
|
||||
{Key: conf.LinkExpiration, Value: "0", Type: conf.TypeNumber, Group: model.GLOBAL, Flag: model.PRIVATE},
|
||||
{Key: conf.PrivacyRegs, Value: `(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
|
||||
([[:xdigit:]]{1,4}(?::[[:xdigit:]]{1,4}){7}|::|:(?::[[:xdigit:]]{1,4}){1,6}|[[:xdigit:]]{1,4}:(?::[[:xdigit:]]{1,4}){1,5}|(?:[[:xdigit:]]{1,4}:){2}(?::[[:xdigit:]]{1,4}){1,4}|(?:[[:xdigit:]]{1,4}:){3}(?::[[:xdigit:]]{1,4}){1,3}|(?:[[:xdigit:]]{1,4}:){4}(?::[[:xdigit:]]{1,4}){1,2}|(?:[[:xdigit:]]{1,4}:){5}:[[:xdigit:]]{1,4}|(?:[[:xdigit:]]{1,4}:){1,6}:)`,
|
||||
Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
|
||||
// aria2 settings
|
||||
{Key: conf.Aria2Uri, Value: "http://localhost:6800/jsonrpc", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
|
||||
{Key: conf.Aria2Secret, Value: "", Type: conf.TypeString, Group: model.ARIA2, Flag: model.PRIVATE},
|
||||
|
|
|
@ -27,7 +27,7 @@ func initUser() {
|
|||
if err := db.CreateUser(admin); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
log.Infof("Successfully created the administrator user and the initial password is: %s", admin.Password)
|
||||
log.Infof("Successfully created the admin user and the initial password is: %s", admin.Password)
|
||||
}
|
||||
} else {
|
||||
panic(err)
|
||||
|
|
|
@ -9,6 +9,7 @@ const (
|
|||
)
|
||||
|
||||
const (
|
||||
// site
|
||||
VERSION = "version"
|
||||
ApiUrl = "api_url"
|
||||
BasePath = "base_path"
|
||||
|
@ -18,6 +19,7 @@ const (
|
|||
Announcement = "announcement"
|
||||
IconColor = "icon_color"
|
||||
|
||||
// preview
|
||||
TextTypes = "text_types"
|
||||
AudioTypes = "audio_types"
|
||||
VideoTypes = "video_types"
|
||||
|
@ -28,15 +30,19 @@ const (
|
|||
AudioAutoplay = "audio_autoplay"
|
||||
VideoAutoplay = "video_autoplay"
|
||||
|
||||
// global
|
||||
HideFiles = "hide_files"
|
||||
GlobalReadme = "global_readme"
|
||||
CustomizeHead = "customize_head"
|
||||
CustomizeBody = "customize_body"
|
||||
LinkExpiration = "link_expiration"
|
||||
PrivacyRegs = "privacy_regs"
|
||||
|
||||
// aria2
|
||||
Aria2Uri = "aria2_uri"
|
||||
Aria2Secret = "aria2_secret"
|
||||
|
||||
// single
|
||||
Token = "token"
|
||||
)
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package conf
|
||||
|
||||
import "regexp"
|
||||
|
||||
var (
|
||||
BuiltAt string
|
||||
GoVersion string
|
||||
|
@ -14,3 +16,4 @@ var (
|
|||
)
|
||||
|
||||
var TypesMap = make(map[string][]string)
|
||||
var PrivacyReg []*regexp.Regexp
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type SettingItemHook struct {
|
||||
Hook func(item *model.SettingItem) error
|
||||
}
|
||||
|
||||
var SettingItemHooks = map[string]SettingItemHook{
|
||||
conf.VideoTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.VideoTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.AudioTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.AudioTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.ImageTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.ImageTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.TextTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.TextTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.OfficeTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.OfficeTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.ProxyTypes: {
|
||||
func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.ProxyTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.PrivacyRegs: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
regStrs := strings.Split(item.Value, "\n")
|
||||
regs := make([]*regexp.Regexp, 0, len(regStrs))
|
||||
for _, regStr := range regStrs {
|
||||
reg, err := regexp.Compile(regStr)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
regs = append(regs, reg)
|
||||
}
|
||||
conf.PrivacyReg = regs
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func HandleSettingItem(item *model.SettingItem) (bool, error) {
|
||||
if hook, ok := SettingItemHooks[item.Key]; ok {
|
||||
return true, hook.Hook(item)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// func HandleSettingItems(items []model.SettingItem) error {
|
||||
// for i := range items {
|
||||
// if err := HandleSettingItem(&items[i]); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
|
@ -2,9 +2,7 @@ package db
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -13,19 +11,9 @@ import (
|
|||
var settingsMap map[string]string
|
||||
var publicSettingsMap map[string]string
|
||||
|
||||
func ResetTypeMap() {
|
||||
settingsMap := GetSettingsMap()
|
||||
conf.TypesMap[conf.AudioTypes] = strings.Split(settingsMap[conf.AudioTypes], ",")
|
||||
conf.TypesMap[conf.VideoTypes] = strings.Split(settingsMap[conf.VideoTypes], ",")
|
||||
conf.TypesMap[conf.ImageTypes] = strings.Split(settingsMap[conf.ImageTypes], ",")
|
||||
conf.TypesMap[conf.TextTypes] = strings.Split(settingsMap[conf.TextTypes], ",")
|
||||
conf.TypesMap[conf.OfficeTypes] = strings.Split(settingsMap[conf.OfficeTypes], ",")
|
||||
}
|
||||
|
||||
func settingsUpdate() {
|
||||
settingsMap = nil
|
||||
publicSettingsMap = nil
|
||||
ResetTypeMap()
|
||||
}
|
||||
|
||||
func GetPublicSettingsMap() map[string]string {
|
||||
|
@ -105,13 +93,38 @@ func GetSettingItemsInGroups(groups []int) ([]model.SettingItem, error) {
|
|||
}
|
||||
|
||||
func SaveSettingItems(items []model.SettingItem) error {
|
||||
settingsUpdate()
|
||||
return errors.WithStack(db.Save(items).Error)
|
||||
others := make([]model.SettingItem, 0)
|
||||
for i := range items {
|
||||
if ok, err := HandleSettingItem(&items[i]); ok {
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
err = db.Save(items[i]).Error
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
others = append(others, items[i])
|
||||
}
|
||||
}
|
||||
err := db.Save(others).Error
|
||||
if err == nil {
|
||||
settingsUpdate()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func SaveSettingItem(item model.SettingItem) error {
|
||||
settingsUpdate()
|
||||
return errors.WithStack(db.Save(item).Error)
|
||||
_, err := HandleSettingItem(&item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.Save(item).Error
|
||||
if err == nil {
|
||||
settingsUpdate()
|
||||
}
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
func DeleteSettingItemByKey(key string) error {
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue