2024-07-19 19:04:11 +08:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2024-08-09 18:30:39 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/app/model"
|
2024-07-19 19:04:11 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/app/repo"
|
2024-08-14 10:15:55 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/app/service"
|
2024-07-19 19:04:11 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/core/utils/cmd"
|
|
|
|
"github.com/1Panel-dev/1Panel/core/utils/common"
|
|
|
|
"github.com/1Panel-dev/1Panel/core/utils/encrypt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
settingRepo := repo.NewISettingRepo()
|
2024-10-30 15:58:40 +08:00
|
|
|
commonRepo := repo.NewICommonRepo()
|
|
|
|
masterSetting, err := settingRepo.Get(commonRepo.WithByKey("MasterAddr"))
|
2024-08-13 15:33:34 +08:00
|
|
|
if err != nil {
|
2024-08-14 10:15:55 +08:00
|
|
|
global.LOG.Errorf("load master addr from setting failed, err: %v", err)
|
2024-08-13 15:33:34 +08:00
|
|
|
}
|
2024-08-14 10:15:55 +08:00
|
|
|
global.CONF.System.MasterAddr = masterSetting.Value
|
2024-10-30 15:58:40 +08:00
|
|
|
portSetting, err := settingRepo.Get(commonRepo.WithByKey("ServerPort"))
|
2024-07-19 19:04:11 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load service port from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CONF.System.Port = portSetting.Value
|
2024-10-30 15:58:40 +08:00
|
|
|
ipv6Setting, err := settingRepo.Get(commonRepo.WithByKey("Ipv6"))
|
2024-07-19 19:04:11 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load ipv6 status from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CONF.System.Ipv6 = ipv6Setting.Value
|
2024-10-30 15:58:40 +08:00
|
|
|
bindAddressSetting, err := settingRepo.Get(commonRepo.WithByKey("BindAddress"))
|
2024-07-19 19:04:11 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load bind address from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CONF.System.BindAddress = bindAddressSetting.Value
|
2024-10-30 15:58:40 +08:00
|
|
|
sslSetting, err := settingRepo.Get(commonRepo.WithByKey("SSL"))
|
2024-07-19 19:04:11 +08:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load service ssl from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CONF.System.SSL = sslSetting.Value
|
|
|
|
|
2024-10-30 15:58:40 +08:00
|
|
|
if _, err := settingRepo.Get(commonRepo.WithByKey("SystemStatus")); err != nil {
|
2024-07-19 19:04:11 +08:00
|
|
|
_ = settingRepo.Create("SystemStatus", "Free")
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("SystemStatus", "Free"); err != nil {
|
|
|
|
global.LOG.Fatalf("init service before start failed, err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
handleUserInfo(global.CONF.System.ChangeUserInfo, settingRepo)
|
2024-08-09 18:30:39 +08:00
|
|
|
loadLocalDir()
|
2024-07-19 19:04:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2024-08-09 18:30:39 +08:00
|
|
|
|
|
|
|
func loadLocalDir() {
|
|
|
|
var backup model.BackupAccount
|
|
|
|
_ = global.DB.Where("type = ?", "LOCAL").First(&backup).Error
|
|
|
|
if backup.ID == 0 {
|
|
|
|
global.LOG.Errorf("no such backup account `%s` in db", "LOCAL")
|
|
|
|
return
|
|
|
|
}
|
2024-08-14 10:15:55 +08:00
|
|
|
dir, err := service.LoadLocalDirByStr(backup.Vars)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load local backup dir failed,err: %v", err)
|
2024-08-09 18:30:39 +08:00
|
|
|
return
|
|
|
|
}
|
2024-08-14 10:15:55 +08:00
|
|
|
global.CONF.System.BackupDir = dir
|
2024-08-09 18:30:39 +08:00
|
|
|
}
|