💩代码改进
This commit is contained in:
@@ -2,9 +2,12 @@ package repository
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/spf13/cast"
|
||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/http/param"
|
||||
"wireguard-dashboard/model/entity"
|
||||
@@ -32,9 +35,9 @@ func Client() clientRepo {
|
||||
// @return err
|
||||
func (r clientRepo) List(p param.ClientList) (data []vo.Client, total int64, err error) {
|
||||
err = r.Table("t_wg_client as twc").Scopes(utils.Page(p.Current, p.Size)).Joins("LEFT JOIN t_user as tu ON twc.user_id = tu.id").
|
||||
Select("twc.id", "twc.created_at", "twc.updated_at", "twc.name", "twc.email", "twc.subnet_range", "twc.ip_allocation", "twc.allowed_ips",
|
||||
"twc.extra_allowed_ips", "twc.endpoint", "twc.use_server_dns", "twc.enable_after_creation", "twc.enabled", "twc.keys as keys_str", "tu.name as create_user").
|
||||
Find(&data).Offset(-1).Limit(-1).Count(&total).Error
|
||||
Select("twc.id", "twc.created_at", "twc.updated_at", "twc.name", "twc.email", "twc.subnet_range", "twc.ip_allocation as ip_allocation_str", "twc.allowed_ips as allowed_ips_str",
|
||||
"twc.extra_allowed_ips as extra_allowed_ips_str", "twc.endpoint", "twc.use_server_dns", "twc.enable_after_creation", "twc.enabled", "twc.keys as keys_str", "tu.name as create_user").
|
||||
Order("twc.created_at DESC").Find(&data).Offset(-1).Limit(-1).Count(&total).Error
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
@@ -44,6 +47,15 @@ func (r clientRepo) List(p param.ClientList) (data []vo.Client, total int64, err
|
||||
if v.KeysStr != "" {
|
||||
_ = json.Unmarshal([]byte(v.KeysStr), &data[i].Keys)
|
||||
}
|
||||
if v.IpAllocationStr != "" {
|
||||
data[i].IpAllocation = strings.Split(v.IpAllocationStr, ",")
|
||||
}
|
||||
if v.AllowedIpsStr != "" {
|
||||
data[i].AllowedIps = strings.Split(v.AllowedIpsStr, ",")
|
||||
}
|
||||
if v.ExtraAllowedIpsStr != "" {
|
||||
data[i].ExtraAllowedIps = strings.Split(v.ExtraAllowedIpsStr, ",")
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
@@ -65,9 +77,9 @@ func (r clientRepo) Save(p param.SaveClient, adminId string) (client *entity.Cli
|
||||
Name: p.Name,
|
||||
Email: p.Email,
|
||||
SubnetRange: p.SubnetRange,
|
||||
IpAllocation: p.IpAllocation,
|
||||
AllowedIps: p.AllowedIPS,
|
||||
ExtraAllowedIps: p.ExtraAllowedIPS,
|
||||
IpAllocation: strings.Join(p.IpAllocation, ","),
|
||||
AllowedIps: strings.Join(p.AllowedIPS, ","),
|
||||
ExtraAllowedIps: strings.Join(p.ExtraAllowedIPS, ","),
|
||||
Endpoint: p.Endpoint,
|
||||
UseServerDns: p.UseServerDNS,
|
||||
EnableAfterCreation: p.EnabledAfterCreation,
|
||||
@@ -85,6 +97,17 @@ func (r clientRepo) Save(p param.SaveClient, adminId string) (client *entity.Cli
|
||||
return
|
||||
}
|
||||
|
||||
// 查询新增的ip地址是否已经存在了
|
||||
var count int64
|
||||
if err = r.Model(&entity.Client{}).Where("ip_allocation in (?)", p.IpAllocation).Count(&count).Error; err != nil {
|
||||
log.Errorf("查询IP地址是否存在失败: %v", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
return nil, errors.New("该客户端的IP已经存在,请检查后再添加!")
|
||||
}
|
||||
|
||||
// 为空,新增
|
||||
privateKey, err := wgtypes.GeneratePrivateKey()
|
||||
if err != nil {
|
||||
@@ -107,9 +130,9 @@ func (r clientRepo) Save(p param.SaveClient, adminId string) (client *entity.Cli
|
||||
Name: p.Name,
|
||||
Email: p.Email,
|
||||
SubnetRange: p.SubnetRange,
|
||||
IpAllocation: p.IpAllocation,
|
||||
AllowedIps: p.AllowedIPS,
|
||||
ExtraAllowedIps: p.ExtraAllowedIPS,
|
||||
IpAllocation: strings.Join(p.IpAllocation, ","),
|
||||
AllowedIps: strings.Join(p.AllowedIPS, ","),
|
||||
ExtraAllowedIps: strings.Join(p.ExtraAllowedIPS, ","),
|
||||
Endpoint: p.Endpoint,
|
||||
UseServerDns: p.UseServerDNS,
|
||||
EnableAfterCreation: p.EnabledAfterCreation,
|
||||
@@ -121,3 +144,26 @@ func (r clientRepo) Save(p param.SaveClient, adminId string) (client *entity.Cli
|
||||
err = r.Model(&entity.Client{}).Create(ent).Error
|
||||
return
|
||||
}
|
||||
|
||||
// Delete
|
||||
// @description: 删除客户端
|
||||
// @receiver r
|
||||
// @param id
|
||||
// @return err
|
||||
func (r clientRepo) Delete(id string) (err error) {
|
||||
return r.Model(&entity.Client{}).Where("id = ?", id).Delete(&entity.Client{}).Error
|
||||
}
|
||||
|
||||
// GetById
|
||||
// @description: 根据id获取客户端详情
|
||||
// @receiver r
|
||||
// @param id
|
||||
// @return data
|
||||
// @return err
|
||||
func (r clientRepo) GetById(id string) (data entity.Client, err error) {
|
||||
err = r.Model(&entity.Client{}).Where("id = ?", id).Preload("Server").First(&data).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ func Server() server {
|
||||
// @return data
|
||||
// @return err
|
||||
func (r server) GetServer() (data *vo.Server, err error) {
|
||||
err = r.Model(&entity.Server{}).First(&data).Error
|
||||
err = r.Model(&entity.Server{}).Select("id", "ip_scope as ip_scope_str", "listen_port", "private_key", "public_key", "post_up_script", "pre_down_script", "post_down_script").First(&data).Error
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user