From 2b04cf4ac3e1d5b78528eb18e0640703b9343de8 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Mon, 8 Aug 2022 12:52:54 +0800 Subject: [PATCH] feat: custom hide error message by regexp (close #1468) --- internal/bootstrap/data/setting.go | 4 +- internal/bootstrap/data/user.go | 2 +- internal/conf/const.go | 6 +++ internal/conf/var.go | 3 ++ internal/db/settinghooks.go | 84 ++++++++++++++++++++++++++++++ internal/db/settingitem.go | 45 ++++++++++------ server/common/common.go | 16 +++++- server/dev.go | 4 ++ 8 files changed, 144 insertions(+), 20 deletions(-) create mode 100644 internal/db/settinghooks.go diff --git a/internal/bootstrap/data/setting.go b/internal/bootstrap/data/setting.go index 406691c65de..6f78f0e49a6 100644 --- a/internal/bootstrap/data/setting.go +++ b/internal/bootstrap/data/setting.go @@ -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}, diff --git a/internal/bootstrap/data/user.go b/internal/bootstrap/data/user.go index 2e63c5c7441..20ce6a20218 100644 --- a/internal/bootstrap/data/user.go +++ b/internal/bootstrap/data/user.go @@ -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) diff --git a/internal/conf/const.go b/internal/conf/const.go index 324b59b5115..eb5d7a13b4d 100644 --- a/internal/conf/const.go +++ b/internal/conf/const.go @@ -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" ) diff --git a/internal/conf/var.go b/internal/conf/var.go index 93637157deb..1fbc15109f5 100644 --- a/internal/conf/var.go +++ b/internal/conf/var.go @@ -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 diff --git a/internal/db/settinghooks.go b/internal/db/settinghooks.go new file mode 100644 index 00000000000..6d51c6e855b --- /dev/null +++ b/internal/db/settinghooks.go @@ -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 +// } diff --git a/internal/db/settingitem.go b/internal/db/settingitem.go index 93f63255751..9b8d4d7c5ca 100644 --- a/internal/db/settingitem.go +++ b/internal/db/settingitem.go @@ -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 { diff --git a/server/common/common.go b/server/common/common.go index 9032513938f..b78466fad72 100644 --- a/server/common/common.go +++ b/server/common/common.go @@ -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() diff --git a/server/dev.go b/server/dev.go index ea923af2f86..d39ef7a0a51 100644 --- a/server/dev.go +++ b/server/dev.go @@ -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) + }) }