2022-09-15 02:44:43 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-09-16 08:00:49 +00:00
|
|
|
"strconv"
|
2022-09-09 09:17:02 +00:00
|
|
|
"time"
|
2022-09-15 02:44:43 +00:00
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2023-03-07 10:20:52 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-01-29 08:38:34 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-03-07 10:20:52 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/encrypt"
|
2022-09-08 10:47:15 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-15 02:44:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SettingService struct{}
|
|
|
|
|
|
|
|
type ISettingService interface {
|
|
|
|
GetSettingInfo() (*dto.SettingInfo, error)
|
2023-02-10 10:07:29 +00:00
|
|
|
Update(key, value string) error
|
2023-04-17 03:12:43 +00:00
|
|
|
UpdateEntrance(value string) error
|
2022-09-08 10:47:15 +00:00
|
|
|
UpdatePassword(c *gin.Context, old, new string) error
|
2023-01-29 08:38:34 +00:00
|
|
|
UpdatePort(port uint) error
|
2022-09-29 08:15:59 +00:00
|
|
|
HandlePasswordExpired(c *gin.Context, old, new string) error
|
2022-09-15 02:44:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewISettingService() ISettingService {
|
|
|
|
return &SettingService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) {
|
2022-09-08 10:47:15 +00:00
|
|
|
setting, err := settingRepo.GetList()
|
2022-09-15 02:44:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, constant.ErrRecordNotFound
|
|
|
|
}
|
|
|
|
settingMap := make(map[string]string)
|
|
|
|
for _, set := range setting {
|
|
|
|
settingMap[set.Key] = set.Value
|
|
|
|
}
|
|
|
|
var info dto.SettingInfo
|
|
|
|
arr, err := json.Marshal(settingMap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-22 10:06:49 +00:00
|
|
|
if err := json.Unmarshal(arr, &info); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-28 10:35:53 +00:00
|
|
|
info.LocalTime = time.Now().Format("2006-01-02 15:04:05 MST -0700")
|
2022-09-15 02:44:43 +00:00
|
|
|
return &info, err
|
|
|
|
}
|
|
|
|
|
2023-02-10 10:07:29 +00:00
|
|
|
func (u *SettingService) Update(key, value string) error {
|
2022-09-29 08:15:59 +00:00
|
|
|
if key == "ExpirationDays" {
|
|
|
|
timeout, _ := strconv.Atoi(value)
|
2022-12-02 02:23:35 +00:00
|
|
|
if err := settingRepo.Update("ExpirationTime", time.Now().AddDate(0, 0, timeout).Format("2006-01-02 15:04:05")); err != nil {
|
2022-09-29 08:15:59 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 10:54:34 +00:00
|
|
|
if key == "SecurityEntrance" {
|
|
|
|
if err := settingRepo.Update("SecurityEntranceStatus", "enable"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if key == "SecurityEntranceStatus" {
|
|
|
|
if err := settingRepo.Update("SecurityEntrance", ""); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-08 10:47:15 +00:00
|
|
|
if err := settingRepo.Update(key, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-11 03:33:21 +00:00
|
|
|
if key == "UserName" {
|
|
|
|
_ = global.SESSION.Clean()
|
|
|
|
}
|
2022-09-29 08:15:59 +00:00
|
|
|
return nil
|
2022-09-15 02:44:43 +00:00
|
|
|
}
|
2022-09-08 10:47:15 +00:00
|
|
|
|
2023-04-17 03:12:43 +00:00
|
|
|
func (u *SettingService) UpdateEntrance(value string) error {
|
|
|
|
if err := settingRepo.Update("SecurityEntranceStatus", "enable"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("SecurityEntrance", value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-29 08:38:34 +00:00
|
|
|
func (u *SettingService) UpdatePort(port uint) error {
|
2023-03-07 10:20:52 +00:00
|
|
|
if common.ScanPort(int(port)) {
|
|
|
|
return buserr.WithDetail(constant.ErrPortInUsed, port, nil)
|
|
|
|
}
|
2023-03-31 16:51:25 +00:00
|
|
|
serverPort, err := settingRepo.Get(settingRepo.WithByKey("ServerPort"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
portValue, _ := strconv.Atoi(serverPort.Value)
|
|
|
|
if err := OperateFirewallPort([]int{portValue}, []int{int(port)}); err != nil {
|
|
|
|
global.LOG.Errorf("set system firewall ports failed, err: %v", err)
|
|
|
|
}
|
2023-02-02 07:01:37 +00:00
|
|
|
if err := settingRepo.Update("ServerPort", strconv.Itoa(int(port))); err != nil {
|
2023-01-29 08:38:34 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-02-02 07:01:37 +00:00
|
|
|
go func() {
|
|
|
|
_, err := cmd.Exec("systemctl restart 1panel.service")
|
|
|
|
if err != nil {
|
2023-02-05 15:48:37 +00:00
|
|
|
global.LOG.Errorf("restart system port failed, err: %v", err)
|
2023-02-02 07:01:37 +00:00
|
|
|
}
|
|
|
|
}()
|
2023-01-29 08:38:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 08:15:59 +00:00
|
|
|
func (u *SettingService) HandlePasswordExpired(c *gin.Context, old, new string) error {
|
2022-09-08 10:47:15 +00:00
|
|
|
setting, err := settingRepo.Get(settingRepo.WithByKey("Password"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
passwordFromDB, err := encrypt.StringDecrypt(setting.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if passwordFromDB == old {
|
|
|
|
newPassword, err := encrypt.StringEncrypt(new)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := settingRepo.Update("Password", newPassword); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-29 08:15:59 +00:00
|
|
|
|
|
|
|
expiredSetting, err := settingRepo.Get(settingRepo.WithByKey("ExpirationDays"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
timeout, _ := strconv.Atoi(expiredSetting.Value)
|
2022-12-02 02:23:35 +00:00
|
|
|
if err := settingRepo.Update("ExpirationTime", time.Now().AddDate(0, 0, timeout).Format("2006-01-02 15:04:05")); err != nil {
|
2022-09-29 08:15:59 +00:00
|
|
|
return err
|
2022-09-08 10:47:15 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return constant.ErrInitialPassword
|
|
|
|
}
|
2022-09-29 08:15:59 +00:00
|
|
|
|
|
|
|
func (u *SettingService) UpdatePassword(c *gin.Context, old, new string) error {
|
|
|
|
if err := u.HandlePasswordExpired(c, old, new); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-11 03:33:21 +00:00
|
|
|
_ = global.SESSION.Clean()
|
2022-09-29 08:15:59 +00:00
|
|
|
return nil
|
|
|
|
}
|