fix: 增加初始化逻辑 (#1026)

pull/1030/head
ssongliu 2 years ago committed by GitHub
parent 52a8331c78
commit 574a504ec3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,22 +1,23 @@
package configs package configs
type System struct { type System struct {
Port string `mapstructure:"port"` Port string `mapstructure:"port"`
SSL string `mapstructure:"ssl"` SSL string `mapstructure:"ssl"`
DbFile string `mapstructure:"db_file"` DbFile string `mapstructure:"db_file"`
DbPath string `mapstructure:"db_path"` DbPath string `mapstructure:"db_path"`
LogPath string `mapstructure:"log_path"` LogPath string `mapstructure:"log_path"`
DataDir string `mapstructure:"data_dir"` DataDir string `mapstructure:"data_dir"`
TmpDir string `mapstructure:"tmp_dir"` TmpDir string `mapstructure:"tmp_dir"`
Cache string `mapstructure:"cache"` Cache string `mapstructure:"cache"`
Backup string `mapstructure:"backup"` Backup string `mapstructure:"backup"`
EncryptKey string `mapstructure:"encrypt_key"` EncryptKey string `mapstructure:"encrypt_key"`
BaseDir string `mapstructure:"base_dir"` BaseDir string `mapstructure:"base_dir"`
Mode string `mapstructure:"mode"` Mode string `mapstructure:"mode"`
RepoUrl string `mapstructure:"repo_url"` RepoUrl string `mapstructure:"repo_url"`
Version string `mapstructure:"version"` Version string `mapstructure:"version"`
Username string `mapstructure:"username"` Username string `mapstructure:"username"`
Password string `mapstructure:"password"` Password string `mapstructure:"password"`
Entrance string `mapstructure:"entrance"` Entrance string `mapstructure:"entrance"`
IsDemo bool `mapstructure:"is_demo"` IsDemo bool `mapstructure:"is_demo"`
ChangeUserInfo bool `mapstructure:"change_user_info"`
} }

@ -3,6 +3,9 @@ package hook
import ( import (
"github.com/1Panel-dev/1Panel/backend/app/repo" "github.com/1Panel-dev/1Panel/backend/app/repo"
"github.com/1Panel-dev/1Panel/backend/global" "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() { func Init() {
@ -27,6 +30,25 @@ func Init() {
_ = settingRepo.Create("SystemStatus", "Free") _ = settingRepo.Create("SystemStatus", "Free")
} }
if err := settingRepo.Update("SystemStatus", "Free"); err != nil { 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")
}
} }
} }

@ -99,6 +99,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.Viper = v global.Viper = v
} }
@ -113,3 +114,11 @@ func loadParams(param string) string {
} }
return info 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"
}

@ -83,6 +83,16 @@ func RandStr(n int) string {
return string(b) 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 { func ScanPort(port int) bool {
ln, err := net.Listen("tcp", ":"+strconv.Itoa(port)) ln, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil { if err != nil {

Loading…
Cancel
Save