feat:新增管理员重置密码功能

This commit is contained in:
samwaf
2024-08-13 08:59:27 +08:00
parent 67321ccb92
commit d65c1d1bf1
3 changed files with 75 additions and 4 deletions
+15 -4
View File
@@ -553,12 +553,23 @@ func main() {
//doWork(ctx)
// 以服务方式运行
if len(os.Args) > 1 {
command := os.Args[1]
err := service.Control(s, command)
if err != nil {
log.Fatal(err)
switch command {
case "install", "start", "stop", "uninstall": // 以服务方式运行
err := service.Control(s, command)
if err != nil {
log.Fatal(err)
}
break
case "resetpwd": //重制密码
//加载配置
wafconfig.LoadAndInitConfig()
wafdb.InitCoreDb("")
wafdb.ResetAdminPwd()
default:
fmt.Printf("Command '%s' is not recognized.\n", command)
}
return
}
+26
View File
@@ -2,8 +2,12 @@ package utils
import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"math/big"
"strings"
)
func Md5String(str string) string {
@@ -12,3 +16,25 @@ func Md5String(str string) string {
sum := h.Sum(nil)
return hex.EncodeToString(sum[:])
}
// 生成指定长度的密码
func GenerateRandomPassword(length int) (string, error) {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:',.<>?/`~"
if length <= 0 {
return "", fmt.Errorf("密码长度必须大于0")
}
var password strings.Builder
charsetLen := big.NewInt(int64(len(charset)))
for i := 0; i < length; i++ {
num, err := rand.Int(rand.Reader, charsetLen)
if err != nil {
return "", err
}
password.WriteByte(charset[num.Int64()])
}
return password.String(), nil
}
+34
View File
@@ -0,0 +1,34 @@
package wafdb
import (
"SamWaf/customtype"
"SamWaf/global"
"SamWaf/model"
"SamWaf/utils"
"fmt"
"time"
)
/*
*
重置密码
*/
func ResetAdminPwd() {
defaultAcount := "admin"
randomPassword, _ := utils.GenerateRandomPassword(12)
beanMap := map[string]interface{}{
"LoginPassword": utils.Md5String(randomPassword + global.GWAF_DEFAULT_ACCOUNT_SALT),
"UPDATE_TIME": customtype.JsonTime(time.Now()),
}
err := global.GWAF_LOCAL_DB.Model(model.Account{}).Where(" login_account = ?", defaultAcount).Updates(beanMap).Error
if err != nil {
} else {
err = global.GWAF_LOCAL_DB.Where("login_account = ? ", defaultAcount).Delete(model.TokenInfo{}).Error
fmt.Println("Reset password is success,the new password is :")
fmt.Println(randomPassword)
fmt.Println("Please keep it safe.")
}
}