diff --git a/backend/configs/system.go b/backend/configs/system.go index 9e295ebff..e144c4675 100644 --- a/backend/configs/system.go +++ b/backend/configs/system.go @@ -1,22 +1,23 @@ package configs type System struct { - Port string `mapstructure:"port"` - SSL string `mapstructure:"ssl"` - DbFile string `mapstructure:"db_file"` - DbPath string `mapstructure:"db_path"` - LogPath string `mapstructure:"log_path"` - DataDir string `mapstructure:"data_dir"` - TmpDir string `mapstructure:"tmp_dir"` - Cache string `mapstructure:"cache"` - Backup string `mapstructure:"backup"` - EncryptKey string `mapstructure:"encrypt_key"` - BaseDir string `mapstructure:"base_dir"` - Mode string `mapstructure:"mode"` - RepoUrl string `mapstructure:"repo_url"` - Version string `mapstructure:"version"` - Username string `mapstructure:"username"` - Password string `mapstructure:"password"` - Entrance string `mapstructure:"entrance"` - IsDemo bool `mapstructure:"is_demo"` + Port string `mapstructure:"port"` + SSL string `mapstructure:"ssl"` + DbFile string `mapstructure:"db_file"` + DbPath string `mapstructure:"db_path"` + LogPath string `mapstructure:"log_path"` + DataDir string `mapstructure:"data_dir"` + TmpDir string `mapstructure:"tmp_dir"` + Cache string `mapstructure:"cache"` + Backup string `mapstructure:"backup"` + EncryptKey string `mapstructure:"encrypt_key"` + BaseDir string `mapstructure:"base_dir"` + Mode string `mapstructure:"mode"` + RepoUrl string `mapstructure:"repo_url"` + Version string `mapstructure:"version"` + Username string `mapstructure:"username"` + Password string `mapstructure:"password"` + Entrance string `mapstructure:"entrance"` + IsDemo bool `mapstructure:"is_demo"` + ChangeUserInfo bool `mapstructure:"change_user_info"` } diff --git a/backend/init/hook/hook.go b/backend/init/hook/hook.go index a2fdc989d..b18bc06b3 100644 --- a/backend/init/hook/hook.go +++ b/backend/init/hook/hook.go @@ -3,6 +3,9 @@ package hook import ( "github.com/1Panel-dev/1Panel/backend/app/repo" "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/cmd" + "github.com/1Panel-dev/1Panel/backend/utils/common" + "github.com/1Panel-dev/1Panel/backend/utils/encrypt" ) func Init() { @@ -27,6 +30,25 @@ func Init() { _ = settingRepo.Create("SystemStatus", "Free") } if err := settingRepo.Update("SystemStatus", "Free"); err != nil { - global.LOG.Fatalf("init service before start failed, err: %v", "Free") + 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) + } + + if cmd.HasNoPasswordSudo() { + _, _ = cmd.Exec("sudo sed -i '/CHANGE_USER_INFO=true/d' /usr/local/bin/1pctl") + } else { + _, _ = cmd.Exec("sed -i '/CHANGE_USER_INFO=true/d' /usr/local/bin/1pctl") + } } } diff --git a/backend/init/viper/viper.go b/backend/init/viper/viper.go index 3ad127f05..896bb957b 100644 --- a/backend/init/viper/viper.go +++ b/backend/init/viper/viper.go @@ -99,6 +99,7 @@ func Init() { global.CONF.System.Username = username global.CONF.System.Password = password global.CONF.System.Entrance = entrance + global.CONF.System.ChangeUserInfo = loadChange() global.Viper = v } @@ -113,3 +114,11 @@ func loadParams(param string) string { } return info } + +func loadChange() bool { + stdout, err := cmd.Exec("grep '^CHANGE_USER_INFO=' /usr/bin/1pctl | cut -d'=' -f2") + if err != nil { + return false + } + return stdout == "true\n" +} diff --git a/backend/utils/common/common.go b/backend/utils/common/common.go index c32c7ce2c..03ecdc2ec 100644 --- a/backend/utils/common/common.go +++ b/backend/utils/common/common.go @@ -83,6 +83,16 @@ func RandStr(n int) string { return string(b) } +func RandStrAndNum(n int) string { + mathRand.Seed(time.Now().UnixNano()) + const charset = "abcdefghijklmnopqrstuvwxyz0123456789" + b := make([]byte, 10) + for i := range b { + b[i] = charset[mathRand.Int63()%int64(len(charset))] + } + return (string(b)) +} + func ScanPort(port int) bool { ln, err := net.Listen("tcp", ":"+strconv.Itoa(port)) if err != nil {