From a425392a2b02d393ada1ba77c3cc5d7f95f224e9 Mon Sep 17 00:00:00 2001 From: Andy Hsu Date: Sun, 6 Aug 2023 22:34:02 +0800 Subject: [PATCH] feat(cmd): set or random new password for admin --- cmd/admin.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- cmd/user.go | 4 ++-- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/cmd/admin.go b/cmd/admin.go index 94412613..123e4030 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -4,8 +4,10 @@ Copyright © 2022 NAME HERE package cmd import ( + "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/op" "github.com/alist-org/alist/v3/pkg/utils" + "github.com/alist-org/alist/v3/pkg/utils/random" "github.com/spf13/cobra" ) @@ -20,14 +22,57 @@ var PasswordCmd = &cobra.Command{ if err != nil { utils.Log.Errorf("failed get admin user: %+v", err) } else { - utils.Log.Infof("admin user's info: \nusername: %s\npassword: %s", admin.Username, admin.Password) + utils.Log.Infof("Admin user's username: %s", admin.Username) + utils.Log.Infof("The password can only be output at the first startup, and then stored as a hash value, which cannot be reversed") + utils.Log.Infof("You can reset the password by running 'alist password random' to generate a random password") + utils.Log.Infof("You can also set the password by running 'alist password set NEW_PASSWORD' to set a new password") } }, } +var RandomPasswordCmd = &cobra.Command{ + Use: "random", + Short: "Reset admin user's password to a random string", + Run: func(cmd *cobra.Command, args []string) { + newPwd := random.String(8) + setAdminPassword(newPwd) + }, +} + +var SetPasswordCmd = &cobra.Command{ + Use: "set", + Short: "Set admin user's password", + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + utils.Log.Errorf("Please enter the new password") + return + } + setAdminPassword(args[0]) + }, +} + +func setAdminPassword(pwd string) { + Init() + admin, err := op.GetAdmin() + if err != nil { + utils.Log.Errorf("failed get admin user: %+v", err) + return + } + admin.PwdHash = model.HashPwd(pwd) + if err := op.UpdateUser(admin); err != nil { + utils.Log.Errorf("failed update admin user: %+v", err) + return + } + utils.Log.Infof("admin user has been updated:") + utils.Log.Infof("username: %s", admin.Username) + utils.Log.Infof("password: %s", pwd) + DelAdminCacheOnline() +} + func init() { RootCmd.AddCommand(PasswordCmd) - + PasswordCmd.AddCommand(RandomPasswordCmd) + PasswordCmd.AddCommand(SetPasswordCmd) // Here you will define your flags and configuration settings. // Cobra supports Persistent Flags which will work for this command diff --git a/cmd/user.go b/cmd/user.go index 1e8504ae..6f7b6edb 100644 --- a/cmd/user.go +++ b/cmd/user.go @@ -28,7 +28,7 @@ func DelUserCacheOnline(username string) { u := fmt.Sprintf("http://localhost:%d/api/admin/user/del_cache", port) if port == -1 { if conf.Conf.Scheme.HttpsPort == -1 { - utils.Log.Infof("[del_user_cache] no open port") + utils.Log.Warnf("[del_user_cache] no open port") return } u = fmt.Sprintf("https://localhost:%d/api/admin/user/del_cache", conf.Conf.Scheme.HttpsPort) @@ -48,5 +48,5 @@ func DelUserCacheOnline(username string) { utils.Log.Errorf("[del_user_cache] del cache error: %s", msg) return } - utils.Log.Infof("[del_user_cache] del user [%s] cache success", username) + utils.Log.Debugf("[del_user_cache] del user [%s] cache success", username) }