diff --git a/backend/configs/system.go b/backend/configs/system.go index 11ff98033..2e1f1c458 100644 --- a/backend/configs/system.go +++ b/backend/configs/system.go @@ -22,7 +22,7 @@ type System struct { Entrance string `mapstructure:"entrance"` IsDemo bool `mapstructure:"is_demo"` AppRepo string `mapstructure:"app_repo"` - ChangeUserInfo bool `mapstructure:"change_user_info"` + ChangeUserInfo string `mapstructure:"change_user_info"` OneDriveID string `mapstructure:"one_drive_id"` OneDriveSc string `mapstructure:"one_drive_sc"` } diff --git a/backend/init/hook/hook.go b/backend/init/hook/hook.go index e3b720471..9c58644b8 100644 --- a/backend/init/hook/hook.go +++ b/backend/init/hook/hook.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "encoding/json" "os" + "strings" "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/repo" @@ -57,21 +58,7 @@ func Init() { global.LOG.Fatalf("init service before start failed, err: %v", err) } - if global.CONF.System.ChangeUserInfo { - if err := settingRepo.Update("UserName", common.RandStrAndNum(10)); err != nil { - global.LOG.Fatalf("init username before start failed, err: %v", err) - } - pass, _ := encrypt.StringEncrypt(common.RandStrAndNum(10)) - if err := settingRepo.Update("Password", pass); err != nil { - global.LOG.Fatalf("init password before start failed, err: %v", err) - } - if err := settingRepo.Update("SecurityEntrance", common.RandStrAndNum(10)); err != nil { - global.LOG.Fatalf("init entrance before start failed, err: %v", err) - } - - sudo := cmd.SudoHandleCmd() - _, _ = cmd.Execf("%s sed -i '/CHANGE_USER_INFO=true/d' /usr/local/bin/1pctl", sudo) - } + handleUserInfo(global.CONF.System.ChangeUserInfo, settingRepo) handleCronjobStatus() handleSnapStatus() @@ -173,3 +160,41 @@ func loadLocalDir() { } global.LOG.Errorf("error type dir: %T", varMap["dir"]) } + +func handleUserInfo(tags string, settingRepo repo.ISettingRepo) { + if len(tags) == 0 { + return + } + if tags == "all" { + if err := settingRepo.Update("UserName", common.RandStrAndNum(10)); err != nil { + global.LOG.Fatalf("init username before start failed, err: %v", err) + } + pass, _ := encrypt.StringEncrypt(common.RandStrAndNum(10)) + if err := settingRepo.Update("Password", pass); err != nil { + global.LOG.Fatalf("init password before start failed, err: %v", err) + } + if err := settingRepo.Update("SecurityEntrance", common.RandStrAndNum(10)); err != nil { + global.LOG.Fatalf("init entrance before start failed, err: %v", err) + } + return + } + if strings.Contains(global.CONF.System.ChangeUserInfo, "username") { + if err := settingRepo.Update("UserName", common.RandStrAndNum(10)); err != nil { + global.LOG.Fatalf("init username before start failed, err: %v", err) + } + } + if strings.Contains(global.CONF.System.ChangeUserInfo, "password") { + pass, _ := encrypt.StringEncrypt(common.RandStrAndNum(10)) + if err := settingRepo.Update("Password", pass); err != nil { + global.LOG.Fatalf("init password before start failed, err: %v", err) + } + } + if strings.Contains(global.CONF.System.ChangeUserInfo, "entrance") { + if err := settingRepo.Update("SecurityEntrance", common.RandStrAndNum(10)); err != nil { + global.LOG.Fatalf("init entrance before start failed, err: %v", err) + } + } + + sudo := cmd.SudoHandleCmd() + _, _ = cmd.Execf("%s sed -i '/CHANGE_USER_INFO=%v/d' /usr/local/bin/1pctl", sudo, global.CONF.System.ChangeUserInfo) +} diff --git a/backend/init/viper/viper.go b/backend/init/viper/viper.go index 1b2a5d381..1a631e88d 100644 --- a/backend/init/viper/viper.go +++ b/backend/init/viper/viper.go @@ -96,7 +96,7 @@ func Init() { global.CONF.System.Username = username global.CONF.System.Password = password global.CONF.System.Entrance = entrance - global.CONF.System.ChangeUserInfo = loadChange() + global.CONF.System.ChangeUserInfo = loadChangeInfo() global.Viper = v } @@ -112,10 +112,10 @@ func loadParams(param string) string { return info } -func loadChange() bool { +func loadChangeInfo() string { stdout, err := cmd.Exec("grep '^CHANGE_USER_INFO=' /usr/bin/1pctl | cut -d'=' -f2") if err != nil { - return false + return "" } - return stdout == "true\n" + return strings.ReplaceAll(stdout, "\n", "") } diff --git a/cmd/server/cmd/root.go b/cmd/server/cmd/root.go index 9c89a813f..2968f9e91 100644 --- a/cmd/server/cmd/root.go +++ b/cmd/server/cmd/root.go @@ -59,6 +59,14 @@ func getSettingByKey(db *gorm.DB, key string) string { return setting.Value } +type LoginLog struct{} + +func isDefault(db *gorm.DB) bool { + logCount := int64(0) + _ = db.Model(&LoginLog{}).Where("status = ?", "Success").Count(&logCount).Error + return logCount == 0 +} + func setSettingByKey(db *gorm.DB, key, value string) error { return db.Model(&setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error } diff --git a/cmd/server/cmd/user-info.go b/cmd/server/cmd/user-info.go index c7a3ec925..c13a8efa5 100644 --- a/cmd/server/cmd/user-info.go +++ b/cmd/server/cmd/user-info.go @@ -3,6 +3,8 @@ package cmd import ( "fmt" + "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/encrypt" "github.com/spf13/cobra" ) @@ -23,6 +25,15 @@ var userinfoCmd = &cobra.Command{ return fmt.Errorf("init my db conn failed, err: %v \n", err) } user := getSettingByKey(db, "UserName") + pass := "********" + if isDefault(db) { + encryptSetting := getSettingByKey(db, "EncryptKey") + pass = getSettingByKey(db, "Password") + if len(encryptSetting) == 16 { + global.CONF.System.EncryptKey = encryptSetting + pass, _ = encrypt.StringDecrypt(pass) + } + } port := getSettingByKey(db, "ServerPort") ssl := getSettingByKey(db, "SSL") entrance := getSettingByKey(db, "SecurityEntrance") @@ -38,7 +49,7 @@ var userinfoCmd = &cobra.Command{ fmt.Printf("面板地址: %s://%s:%s/%s \n", protocol, address, port, entrance) fmt.Println("面板用户: ", user) - fmt.Println("面板密码: ", "********") + fmt.Println("面板密码: ", pass) fmt.Println("提示:修改密码可执行命令:1pctl update password") return nil }, diff --git a/frontend/src/routers/index.ts b/frontend/src/routers/index.ts index aa6683783..6959fec84 100644 --- a/frontend/src/routers/index.ts +++ b/frontend/src/routers/index.ts @@ -13,7 +13,7 @@ router.beforeEach((to, from, next) => { if (to.name !== 'entrance' && !globalStore.isLogin) { next({ name: 'entrance', - params: { code: to.params?.code || globalStore.entrance }, + params: to.params, }); NProgress.done(); return;