Browse Source

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

pull/4532/head
ssongliu 7 months ago committed by GitHub
parent
commit
2ec4b2561c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      backend/configs/system.go
  2. 55
      backend/init/hook/hook.go
  3. 8
      backend/init/viper/viper.go
  4. 8
      cmd/server/cmd/root.go
  5. 13
      cmd/server/cmd/user-info.go
  6. 2
      frontend/src/routers/index.ts

2
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"`
}

55
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)
}

8
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", "")
}

8
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
}

13
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
},

2
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;

Loading…
Cancel
Save