2024-07-05 14:41:35 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
gdb "wireguard-ui/global/client"
|
2024-08-08 15:30:09 +08:00
|
|
|
"wireguard-ui/http/vo"
|
2024-07-05 14:41:35 +08:00
|
|
|
"wireguard-ui/model"
|
|
|
|
"wireguard-ui/template/render_data"
|
|
|
|
)
|
|
|
|
|
|
|
|
type setting struct{ *gorm.DB }
|
|
|
|
|
|
|
|
func Setting() setting {
|
|
|
|
return setting{gdb.DB}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s setting) SetData(data *model.Setting) error {
|
|
|
|
// 判断code是否已经存在
|
|
|
|
var count int64
|
|
|
|
if err := s.Model(&model.Setting{}).Where("code = ?", data.Code).Count(&count).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 存在就更新,反之新增
|
|
|
|
if count > 0 {
|
2024-08-08 15:30:09 +08:00
|
|
|
return s.Where("code = ?", data.Code).Updates(&data).Error
|
2024-07-05 14:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.Create(&data).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWGSetForConfig
|
|
|
|
// @description: 获取全局配置
|
|
|
|
// @receiver s
|
|
|
|
// @return data
|
|
|
|
// @return err
|
|
|
|
func (s setting) GetWGSetForConfig() (data *render_data.ServerSetting, err error) {
|
|
|
|
var rs *model.Setting
|
|
|
|
if err = s.Model(&model.Setting{}).Where("code = ?", "WG_SETTING").Take(&rs).Error; err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal([]byte(rs.Data), &data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWGServerForConfig
|
|
|
|
// @description: 获取wireguard服务端配置为了渲染数据
|
|
|
|
// @receiver s
|
|
|
|
// @return render_data
|
|
|
|
// @return err
|
|
|
|
func (s setting) GetWGServerForConfig() (data *render_data.Server, err error) {
|
|
|
|
var rs *model.Setting
|
|
|
|
if err = s.Model(&model.Setting{}).Where("code = ?", "WG_SERVER").Take(&rs).Error; err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = json.Unmarshal([]byte(rs.Data), &data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取一下全局的服务配置
|
|
|
|
gs, err := s.GetWGSetForConfig()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data.MTU = gs.MTU
|
|
|
|
data.Table = gs.Table
|
|
|
|
return
|
|
|
|
}
|
2024-08-08 15:30:09 +08:00
|
|
|
|
|
|
|
// GetAllSetting
|
|
|
|
// @description: 获取全部配置
|
|
|
|
// @receiver s
|
|
|
|
// @param blackList
|
|
|
|
// @return data
|
|
|
|
// @return err
|
|
|
|
func (s setting) GetAllSetting(blackList []string) (data []vo.SettingItem, err error) {
|
|
|
|
err = s.Model(&model.Setting{}).Select("code, data, describe").Where("code not in ?", blackList).Find(&data).Error
|
|
|
|
return
|
|
|
|
}
|