🆕导入配置文件完成

This commit is contained in:
coward
2024-09-23 15:58:46 +08:00
parent edaf9ba770
commit f2dcb13e0d
10 changed files with 261 additions and 36 deletions

View File

@@ -2,9 +2,11 @@ package service
import (
"encoding/json"
slog "gitee.ltd/lxh/logger/log"
"gorm.io/gorm"
"strings"
gdb "wireguard-ui/global/client"
"wireguard-ui/http/param"
"wireguard-ui/http/vo"
"wireguard-ui/model"
"wireguard-ui/template/render_data"
@@ -142,3 +144,60 @@ func (s setting) Export() (data vo.Export, err error) {
return
}
// 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 := &param.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
}