2023-02-02 07:01:37 +00:00
|
|
|
package hook
|
|
|
|
|
|
|
|
import (
|
2023-07-04 08:16:12 +00:00
|
|
|
"encoding/base64"
|
2024-02-02 02:47:24 +00:00
|
|
|
"encoding/json"
|
|
|
|
"os"
|
2024-05-08 09:36:23 +00:00
|
|
|
"path"
|
2024-04-16 06:44:07 +00:00
|
|
|
"strings"
|
2023-07-04 08:16:12 +00:00
|
|
|
|
2024-01-29 08:34:57 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-02-02 07:01:37 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
2023-08-23 09:16:18 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-02-02 07:01:37 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-05-13 04:20:29 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
|
2023-02-02 07:01:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
settingRepo := repo.NewISettingRepo()
|
|
|
|
portSetting, err := settingRepo.Get(settingRepo.WithByKey("ServerPort"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load service port from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CONF.System.Port = portSetting.Value
|
2023-10-25 02:41:26 +00:00
|
|
|
ipv6Setting, err := settingRepo.Get(settingRepo.WithByKey("Ipv6"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load ipv6 status from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CONF.System.Ipv6 = ipv6Setting.Value
|
|
|
|
bindAddressSetting, err := settingRepo.Get(settingRepo.WithByKey("BindAddress"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load bind address from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CONF.System.BindAddress = bindAddressSetting.Value
|
2023-04-25 06:34:16 +00:00
|
|
|
sslSetting, err := settingRepo.Get(settingRepo.WithByKey("SSL"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load service ssl from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
global.CONF.System.SSL = sslSetting.Value
|
2023-02-02 07:01:37 +00:00
|
|
|
|
2023-07-04 08:16:12 +00:00
|
|
|
OneDriveID, err := settingRepo.Get(settingRepo.WithByKey("OneDriveID"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load onedrive info from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
idItem, _ := base64.StdEncoding.DecodeString(OneDriveID.Value)
|
|
|
|
global.CONF.System.OneDriveID = string(idItem)
|
|
|
|
OneDriveSc, err := settingRepo.Get(settingRepo.WithByKey("OneDriveSc"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("load onedrive info from setting failed, err: %v", err)
|
|
|
|
}
|
|
|
|
scItem, _ := base64.StdEncoding.DecodeString(OneDriveSc.Value)
|
|
|
|
global.CONF.System.OneDriveSc = string(scItem)
|
|
|
|
|
2023-02-02 07:01:37 +00:00
|
|
|
if _, err := settingRepo.Get(settingRepo.WithByKey("SystemStatus")); err != nil {
|
|
|
|
_ = settingRepo.Create("SystemStatus", "Free")
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("SystemStatus", "Free"); err != nil {
|
2023-05-13 04:20:29 +00:00
|
|
|
global.LOG.Fatalf("init service before start failed, err: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-16 06:44:07 +00:00
|
|
|
handleUserInfo(global.CONF.System.ChangeUserInfo, settingRepo)
|
2023-08-23 09:16:18 +00:00
|
|
|
|
2024-01-29 08:34:57 +00:00
|
|
|
handleCronjobStatus()
|
2023-08-23 09:16:18 +00:00
|
|
|
handleSnapStatus()
|
2024-02-02 02:47:24 +00:00
|
|
|
loadLocalDir()
|
2024-05-08 09:36:23 +00:00
|
|
|
initDir()
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleSnapStatus() {
|
2024-01-30 08:11:49 +00:00
|
|
|
msgFailed := "the task was interrupted due to the restart of the 1panel service"
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("status = ?", "OnSaveData").
|
|
|
|
Updates(map[string]interface{}{"status": constant.StatusSuccess}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"status": constant.StatusFailed,
|
|
|
|
"message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("recover_status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"recover_status": constant.StatusFailed,
|
|
|
|
"recover_message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
|
|
|
|
_ = global.DB.Model(&model.Snapshot{}).Where("rollback_status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"rollback_status": constant.StatusFailed,
|
|
|
|
"rollback_message": msgFailed,
|
|
|
|
}).Error
|
|
|
|
|
2023-08-23 09:16:18 +00:00
|
|
|
snapRepo := repo.NewISnapshotRepo()
|
|
|
|
|
|
|
|
status, _ := snapRepo.GetStatusList()
|
2024-01-30 08:11:49 +00:00
|
|
|
for _, item := range status {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates := make(map[string]interface{})
|
2024-01-30 08:11:49 +00:00
|
|
|
if item.Panel == constant.StatusRunning {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates["panel"] = constant.StatusFailed
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
2024-01-30 08:11:49 +00:00
|
|
|
if item.PanelInfo == constant.StatusRunning {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates["panel_info"] = constant.StatusFailed
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
2024-01-30 08:11:49 +00:00
|
|
|
if item.DaemonJson == constant.StatusRunning {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates["daemon_json"] = constant.StatusFailed
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
2024-01-30 08:11:49 +00:00
|
|
|
if item.AppData == constant.StatusRunning {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates["app_data"] = constant.StatusFailed
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
2024-01-30 08:11:49 +00:00
|
|
|
if item.PanelData == constant.StatusRunning {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates["panel_data"] = constant.StatusFailed
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
2024-01-30 08:11:49 +00:00
|
|
|
if item.BackupData == constant.StatusRunning {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates["backup_data"] = constant.StatusFailed
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
2024-01-30 08:11:49 +00:00
|
|
|
if item.Compress == constant.StatusRunning {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates["compress"] = constant.StatusFailed
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
2024-01-30 08:11:49 +00:00
|
|
|
if item.Upload == constant.StatusUploading {
|
2023-08-29 02:50:15 +00:00
|
|
|
updates["upload"] = constant.StatusFailed
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
2023-08-29 02:50:15 +00:00
|
|
|
if len(updates) != 0 {
|
2024-01-30 08:11:49 +00:00
|
|
|
_ = snapRepo.UpdateStatus(item.ID, updates)
|
2023-08-23 09:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-02 07:01:37 +00:00
|
|
|
}
|
2024-01-29 08:34:57 +00:00
|
|
|
|
|
|
|
func handleCronjobStatus() {
|
|
|
|
_ = global.DB.Model(&model.JobRecords{}).Where("status = ?", constant.StatusWaiting).
|
|
|
|
Updates(map[string]interface{}{
|
|
|
|
"status": constant.StatusFailed,
|
|
|
|
"message": "the task was interrupted due to the restart of the 1panel service",
|
|
|
|
}).Error
|
|
|
|
}
|
2024-02-02 02:47:24 +00: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
|
|
|
|
}
|
|
|
|
varMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal([]byte(backup.Vars), &varMap); err != nil {
|
|
|
|
global.LOG.Errorf("json unmarshal backup.Vars: %v failed, err: %v", backup.Vars, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := varMap["dir"]; !ok {
|
|
|
|
global.LOG.Error("load local backup dir failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
baseDir, ok := varMap["dir"].(string)
|
|
|
|
if ok {
|
|
|
|
if _, err := os.Stat(baseDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(baseDir, os.ModePerm); err != nil {
|
|
|
|
global.LOG.Errorf("mkdir %s failed, err: %v", baseDir, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
global.CONF.System.Backup = baseDir
|
|
|
|
return
|
|
|
|
}
|
|
|
|
global.LOG.Errorf("error type dir: %T", varMap["dir"])
|
|
|
|
}
|
2024-04-16 06:44:07 +00: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-05-08 09:36:23 +00:00
|
|
|
|
|
|
|
func initDir() {
|
|
|
|
composePath := path.Join(global.CONF.System.BaseDir, "1panel/docker/compose/")
|
|
|
|
if _, err := os.Stat(composePath); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(composePath, os.ModePerm); err != nil {
|
|
|
|
global.LOG.Errorf("mkdir %s failed, err: %v", composePath, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|