Skip to content

Commit

Permalink
feat: custom hide error message by regexp (close #1468)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 8, 2022
1 parent d6437a3 commit 2b04cf4
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 20 deletions.
4 changes: 3 additions & 1 deletion internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func initSettings() {
log.Fatalf("failed get setting: %+v", err)
}
}
db.ResetTypeMap()
}

func isActive(key string) bool {
Expand Down Expand Up @@ -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},
Expand Down
2 changes: 1 addition & 1 deletion internal/bootstrap/data/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions internal/conf/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
)

const (
// site
VERSION = "version"
ApiUrl = "api_url"
BasePath = "base_path"
Expand All @@ -18,6 +19,7 @@ const (
Announcement = "announcement"
IconColor = "icon_color"

// preview
TextTypes = "text_types"
AudioTypes = "audio_types"
VideoTypes = "video_types"
Expand All @@ -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"
)

Expand Down
3 changes: 3 additions & 0 deletions internal/conf/var.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package conf

import "regexp"

var (
BuiltAt string
GoVersion string
Expand All @@ -14,3 +16,4 @@ var (
)

var TypesMap = make(map[string][]string)
var PrivacyReg []*regexp.Regexp
84 changes: 84 additions & 0 deletions internal/db/settinghooks.go
Original file line number Diff line number Diff line change
@@ -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
// }
45 changes: 29 additions & 16 deletions internal/db/settingitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions server/common/common.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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()
Expand All @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions server/dev.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
})
}

0 comments on commit 2b04cf4

Please sign in to comment.