2024-07-05 14:41:35 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-09-23 15:58:46 +08:00
|
|
|
slog "gitee.ltd/lxh/logger/log"
|
2024-07-05 14:41:35 +08:00
|
|
|
"gorm.io/gorm"
|
2024-09-20 17:26:41 +08:00
|
|
|
"strings"
|
2024-07-05 14:41:35 +08:00
|
|
|
gdb "wireguard-ui/global/client"
|
2024-09-23 15:58:46 +08:00
|
|
|
"wireguard-ui/http/param"
|
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
|
|
|
|
}
|
2024-09-20 17:26:41 +08:00
|
|
|
|
|
|
|
// Export
|
|
|
|
// @description: 导出
|
|
|
|
// @receiver s
|
|
|
|
// @return data
|
|
|
|
// @return err
|
|
|
|
func (s setting) Export() (data vo.Export, err error) {
|
|
|
|
// 先查询global配置
|
|
|
|
var gs, ss *model.Setting
|
|
|
|
if err = s.Model(&model.Setting{}).Where("code = ?", "WG_SETTING").Take(&gs).Error; err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal([]byte(gs.Data), &data.Global); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询server配置
|
|
|
|
if err = s.Model(&model.Setting{}).Where("code = ?", "WG_SERVER").Take(&ss).Error; err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = json.Unmarshal([]byte(ss.Data), &data.Server); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 查询client配置
|
|
|
|
var clients []vo.ClientItem
|
|
|
|
if err = s.Model(&model.Client{}).Select("id,name,email,ip_allocation as ip_allocation_str," +
|
|
|
|
"allowed_ips as allowed_ips_str,extra_allowed_ips as extra_allowed_ips_str," +
|
|
|
|
"endpoint,use_server_dns,keys as keys_str," +
|
|
|
|
"enabled,offline_monitoring").Order("created_at DESC").Find(&clients).Error; err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range clients {
|
|
|
|
if v.KeysStr != "" {
|
|
|
|
_ = json.Unmarshal([]byte(v.KeysStr), &clients[i].Keys)
|
|
|
|
}
|
|
|
|
if v.IpAllocationStr != "" {
|
|
|
|
clients[i].IpAllocation = strings.Split(v.IpAllocationStr, ",")
|
|
|
|
}
|
|
|
|
if v.AllowedIpsStr != "" {
|
|
|
|
clients[i].AllowedIps = strings.Split(v.AllowedIpsStr, ",")
|
|
|
|
} else {
|
|
|
|
clients[i].AllowedIps = []string{}
|
|
|
|
}
|
|
|
|
if v.ExtraAllowedIpsStr != "" {
|
|
|
|
clients[i].ExtraAllowedIps = strings.Split(v.ExtraAllowedIpsStr, ",")
|
|
|
|
} else {
|
|
|
|
clients[i].ExtraAllowedIps = []string{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cj, _ := json.Marshal(clients)
|
|
|
|
_ = json.Unmarshal(cj, &data.Clients)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2024-09-23 15:58:46 +08:00
|
|
|
|
|
|
|
// Import
|
|
|
|
// @description: 导入
|
|
|
|
// @receiver s
|
|
|
|
// @param data
|
|
|
|
// @return err
|
|
|
|
func (s setting) Import(data *vo.Export, loginUser *vo.User) (err error) {
|
|
|
|
// 先更新global配置
|
|
|
|
gst, _ := json.Marshal(data.Global)
|
|
|
|
gs := &model.Setting{
|
|
|
|
Code: "WG_SETTING",
|
|
|
|
Data: string(gst),
|
|
|
|
Describe: "服务端全局配置",
|
|
|
|
}
|
|
|
|
if err = s.SetData(gs); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
st, _ := json.Marshal(data.Server)
|
|
|
|
ss := &model.Setting{
|
|
|
|
Code: "WG_SERVER",
|
|
|
|
Data: string(st),
|
|
|
|
Describe: "服务端配置",
|
|
|
|
}
|
|
|
|
if err = s.SetData(ss); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新client配置
|
|
|
|
for _, v := range data.Clients {
|
|
|
|
keys := ¶m.Keys{
|
|
|
|
PrivateKey: v.Keys.PrivateKey,
|
|
|
|
PublicKey: v.Keys.PublicKey,
|
|
|
|
PresharedKey: v.Keys.PresharedKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
cc := param.SaveClient{
|
|
|
|
Name: v.Name,
|
|
|
|
Email: v.Email,
|
|
|
|
IpAllocation: v.IpAllocation,
|
|
|
|
AllowedIps: v.AllowedIps,
|
|
|
|
ExtraAllowedIps: v.ExtraAllowedIps,
|
|
|
|
Endpoint: v.Endpoint,
|
|
|
|
UseServerDns: v.UseServerDns,
|
|
|
|
Keys: keys,
|
|
|
|
Enabled: v.Enabled,
|
|
|
|
OfflineMonitoring: v.OfflineMonitoring,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Client().SaveClient(cc, loginUser); err != nil {
|
|
|
|
slog.Errorf("客户端[%s]导入失败: %v", v.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|