Skip to content

Commit

Permalink
feat: 修改服务初始化操作逻辑 (1Panel-dev#4530)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu authored Apr 16, 2024
1 parent 738e082 commit 3c22096
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion backend/configs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 40 additions & 15 deletions backend/init/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 changes: 4 additions & 4 deletions backend/init/viper/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 changes: 8 additions & 0 deletions cmd/server/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 12 additions & 1 deletion cmd/server/cmd/user-info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
Expand All @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/routers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3c22096

Please sign in to comment.