2024-07-19 19:04:11 +08:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/core/app/repo"
|
2025-01-21 18:32:14 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/core/constant"
|
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()
|
2025-01-24 11:00:07 +08:00
|
|
|
global.CONF.Conn.Port, _ = settingRepo.GetValueByKey("ServerPort")
|
|
|
|
global.CONF.Conn.Ipv6, _ = settingRepo.GetValueByKey("Ipv6")
|
|
|
|
global.Api.ApiInterfaceStatus, _ = settingRepo.GetValueByKey("ApiInterfaceStatus")
|
|
|
|
if global.Api.ApiInterfaceStatus == constant.StatusEnable {
|
|
|
|
global.Api.ApiKey, _ = settingRepo.GetValueByKey("ApiKey")
|
|
|
|
global.Api.IpWhiteList, _ = settingRepo.GetValueByKey("IpWhiteList")
|
|
|
|
global.Api.ApiKeyValidityTime, _ = settingRepo.GetValueByKey("ApiKeyValidityTime")
|
2024-07-19 19:04:11 +08:00
|
|
|
}
|
2025-01-24 11:00:07 +08:00
|
|
|
global.CONF.Conn.BindAddress, _ = settingRepo.GetValueByKey("BindAddress")
|
|
|
|
global.CONF.Conn.SSL, _ = settingRepo.GetValueByKey("SSL")
|
|
|
|
global.CONF.Base.Version, _ = settingRepo.GetValueByKey("SystemVersion")
|
2024-07-19 19:04:11 +08:00
|
|
|
if err := settingRepo.Update("SystemStatus", "Free"); err != nil {
|
|
|
|
global.LOG.Fatalf("init service before start failed, err: %v", err)
|
|
|
|
}
|
|
|
|
|
2025-01-24 11:00:07 +08:00
|
|
|
handleUserInfo(global.CONF.Base.ChangeUserInfo, settingRepo)
|
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
|
|
|
|
}
|
2025-01-24 11:00:07 +08:00
|
|
|
if strings.Contains(global.CONF.Base.ChangeUserInfo, "username") {
|
2024-07-19 19:04:11 +08:00
|
|
|
if err := settingRepo.Update("UserName", common.RandStrAndNum(10)); err != nil {
|
|
|
|
global.LOG.Fatalf("init username before start failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2025-01-24 11:00:07 +08:00
|
|
|
if strings.Contains(global.CONF.Base.ChangeUserInfo, "password") {
|
2024-07-19 19:04:11 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2025-01-24 11:00:07 +08:00
|
|
|
if strings.Contains(global.CONF.Base.ChangeUserInfo, "entrance") {
|
2024-07-19 19:04:11 +08:00
|
|
|
if err := settingRepo.Update("SecurityEntrance", common.RandStrAndNum(10)); err != nil {
|
|
|
|
global.LOG.Fatalf("init entrance before start failed, err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sudo := cmd.SudoHandleCmd()
|
2025-01-24 11:00:07 +08:00
|
|
|
_, _ = cmd.Execf("%s sed -i '/CHANGE_USER_INFO=%v/d' /usr/local/bin/1pctl", sudo, global.CONF.Base.ChangeUserInfo)
|
2024-07-19 19:04:11 +08:00
|
|
|
}
|