feat: 修改服务初始化操作逻辑 (#4530)

This commit is contained in:
ssongliu 2024-04-16 14:44:07 +08:00 committed by GitHub
parent 4efe7240ad
commit 2ec4b2561c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 66 additions and 22 deletions

View File

@ -22,7 +22,7 @@ type System struct {
Entrance string `mapstructure:"entrance"` Entrance string `mapstructure:"entrance"`
IsDemo bool `mapstructure:"is_demo"` IsDemo bool `mapstructure:"is_demo"`
AppRepo string `mapstructure:"app_repo"` AppRepo string `mapstructure:"app_repo"`
ChangeUserInfo bool `mapstructure:"change_user_info"` ChangeUserInfo string `mapstructure:"change_user_info"`
OneDriveID string `mapstructure:"one_drive_id"` OneDriveID string `mapstructure:"one_drive_id"`
OneDriveSc string `mapstructure:"one_drive_sc"` OneDriveSc string `mapstructure:"one_drive_sc"`
} }

View File

@ -4,6 +4,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"os" "os"
"strings"
"github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/app/model"
"github.com/1Panel-dev/1Panel/backend/app/repo" "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) global.LOG.Fatalf("init service before start failed, err: %v", err)
} }
if global.CONF.System.ChangeUserInfo { handleUserInfo(global.CONF.System.ChangeUserInfo, settingRepo)
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)
}
handleCronjobStatus() handleCronjobStatus()
handleSnapStatus() handleSnapStatus()
@ -173,3 +160,41 @@ func loadLocalDir() {
} }
global.LOG.Errorf("error type dir: %T", varMap["dir"]) 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)
}

View File

@ -96,7 +96,7 @@ func Init() {
global.CONF.System.Username = username global.CONF.System.Username = username
global.CONF.System.Password = password global.CONF.System.Password = password
global.CONF.System.Entrance = entrance global.CONF.System.Entrance = entrance
global.CONF.System.ChangeUserInfo = loadChange() global.CONF.System.ChangeUserInfo = loadChangeInfo()
global.Viper = v global.Viper = v
} }
@ -112,10 +112,10 @@ func loadParams(param string) string {
return info return info
} }
func loadChange() bool { func loadChangeInfo() string {
stdout, err := cmd.Exec("grep '^CHANGE_USER_INFO=' /usr/bin/1pctl | cut -d'=' -f2") stdout, err := cmd.Exec("grep '^CHANGE_USER_INFO=' /usr/bin/1pctl | cut -d'=' -f2")
if err != nil { if err != nil {
return false return ""
} }
return stdout == "true\n" return strings.ReplaceAll(stdout, "\n", "")
} }

View File

@ -59,6 +59,14 @@ func getSettingByKey(db *gorm.DB, key string) string {
return setting.Value 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 { func setSettingByKey(db *gorm.DB, key, value string) error {
return db.Model(&setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error return db.Model(&setting{}).Where("key = ?", key).Updates(map[string]interface{}{"value": value}).Error
} }

View File

@ -3,6 +3,8 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -23,6 +25,15 @@ var userinfoCmd = &cobra.Command{
return fmt.Errorf("init my db conn failed, err: %v \n", err) return fmt.Errorf("init my db conn failed, err: %v \n", err)
} }
user := getSettingByKey(db, "UserName") 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") port := getSettingByKey(db, "ServerPort")
ssl := getSettingByKey(db, "SSL") ssl := getSettingByKey(db, "SSL")
entrance := getSettingByKey(db, "SecurityEntrance") entrance := getSettingByKey(db, "SecurityEntrance")
@ -38,7 +49,7 @@ var userinfoCmd = &cobra.Command{
fmt.Printf("面板地址: %s://%s:%s/%s \n", protocol, address, port, entrance) fmt.Printf("面板地址: %s://%s:%s/%s \n", protocol, address, port, entrance)
fmt.Println("面板用户: ", user) fmt.Println("面板用户: ", user)
fmt.Println("面板密码: ", "********") fmt.Println("面板密码: ", pass)
fmt.Println("提示修改密码可执行命令1pctl update password") fmt.Println("提示修改密码可执行命令1pctl update password")
return nil return nil
}, },

View File

@ -13,7 +13,7 @@ router.beforeEach((to, from, next) => {
if (to.name !== 'entrance' && !globalStore.isLogin) { if (to.name !== 'entrance' && !globalStore.isLogin) {
next({ next({
name: 'entrance', name: 'entrance',
params: { code: to.params?.code || globalStore.entrance }, params: to.params,
}); });
NProgress.done(); NProgress.done();
return; return;