♻️重构了部分代码
This commit is contained in:
@@ -2,9 +2,13 @@ package repository
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/spf13/cast"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"gorm.io/gorm"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/http/param"
|
||||
"wireguard-dashboard/model/entity"
|
||||
"wireguard-dashboard/model/template_data"
|
||||
"wireguard-dashboard/model/vo"
|
||||
"wireguard-dashboard/utils"
|
||||
)
|
||||
@@ -49,7 +53,69 @@ func (r clientRepo) List(p param.ClientList) (data []vo.Client, total int64, err
|
||||
// @description: 新增/编辑客户端
|
||||
// @receiver r
|
||||
// @param p
|
||||
// @param adminId
|
||||
// @return err
|
||||
func (r clientRepo) Save(p param.SaveClient) (err error) {
|
||||
return nil
|
||||
func (r clientRepo) Save(p param.SaveClient, adminId string) (client *entity.Client, err error) {
|
||||
|
||||
ent := &entity.Client{
|
||||
Base: entity.Base{
|
||||
Id: p.Id,
|
||||
},
|
||||
ServerId: p.ServerId,
|
||||
Name: p.Name,
|
||||
Email: p.Email,
|
||||
SubnetRange: p.SubnetRange,
|
||||
IpAllocation: p.IpAllocation,
|
||||
AllowedIps: p.AllowedIPS,
|
||||
ExtraAllowedIps: p.ExtraAllowedIPS,
|
||||
Endpoint: p.Endpoint,
|
||||
UseServerDns: p.UseServerDNS,
|
||||
EnableAfterCreation: p.EnabledAfterCreation,
|
||||
UserId: adminId,
|
||||
Enabled: cast.ToBool(p.Enabled),
|
||||
}
|
||||
|
||||
// id不为空,更新信息
|
||||
if p.Id != "" {
|
||||
keys, _ := json.Marshal(p.Keys)
|
||||
ent.Keys = string(keys)
|
||||
if err = r.Model(&entity.Client{}).Where("id = ?", p.Id).Updates(ent).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 为空,新增
|
||||
privateKey, err := wgtypes.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
publicKey := privateKey.PublicKey().String()
|
||||
presharedKey, err := wgtypes.GenerateKey()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
keys := template_data.Keys{
|
||||
PublicKey: publicKey,
|
||||
PresharedKey: presharedKey.String(),
|
||||
}
|
||||
keysStr, _ := json.Marshal(keys)
|
||||
|
||||
ent = &entity.Client{
|
||||
ServerId: p.ServerId,
|
||||
Name: p.Name,
|
||||
Email: p.Email,
|
||||
SubnetRange: p.SubnetRange,
|
||||
IpAllocation: p.IpAllocation,
|
||||
AllowedIps: p.AllowedIPS,
|
||||
ExtraAllowedIps: p.ExtraAllowedIPS,
|
||||
Endpoint: p.Endpoint,
|
||||
UseServerDns: p.UseServerDNS,
|
||||
EnableAfterCreation: p.EnabledAfterCreation,
|
||||
Keys: string(keysStr),
|
||||
UserId: adminId,
|
||||
}
|
||||
|
||||
err = r.Model(&entity.Client{}).Create(ent).Error
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user