🆕完成客户端链接状态监控
This commit is contained in:
@@ -7,9 +7,11 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"os"
|
||||
"strings"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/http/param"
|
||||
"wireguard-dashboard/model/entity"
|
||||
"wireguard-dashboard/model/template_data"
|
||||
"wireguard-dashboard/model/vo"
|
||||
"wireguard-dashboard/queues"
|
||||
"wireguard-dashboard/repository"
|
||||
"wireguard-dashboard/utils"
|
||||
@@ -245,3 +247,52 @@ func (clients) GenerateQrCode(c *gin.Context) {
|
||||
"qrCode": png,
|
||||
})
|
||||
}
|
||||
|
||||
// Status
|
||||
// @description: 获取客户端状态信息,链接状态等
|
||||
// @receiver clients
|
||||
// @param c
|
||||
func (clients) Status(c *gin.Context) {
|
||||
var p param.ClientStatusList
|
||||
if err := c.ShouldBind(&p); err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("参数错误", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 使用sdk拉取一下客户端信息
|
||||
devices, err := client.WireguardClient.Devices()
|
||||
if err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("获取客户端信息失败", err)
|
||||
return
|
||||
}
|
||||
|
||||
var data []vo.ClientStatus
|
||||
// 遍历客户端数据,并渲染数据信息
|
||||
for _, d := range devices {
|
||||
for _, p := range d.Peers {
|
||||
clientInfo, err := repository.Client().GetByPublicKey(p.PublicKey.String())
|
||||
if err != nil {
|
||||
log.Errorf("没有找到公钥匹配的客户端: %s", p.PublicKey.String())
|
||||
continue
|
||||
}
|
||||
var ipAllocation string
|
||||
for _, iaip := range p.AllowedIPs {
|
||||
ipAllocation += iaip.String() + ","
|
||||
}
|
||||
ipAllocation = strings.TrimRight(ipAllocation, ",")
|
||||
isOnline := p.LastHandshakeTime.Minute() < 1
|
||||
data = append(data, vo.ClientStatus{
|
||||
Name: clientInfo.Name,
|
||||
Email: clientInfo.Email,
|
||||
IpAllocation: ipAllocation,
|
||||
Endpoint: p.Endpoint.String(),
|
||||
Received: utils.FlowCalculation().Parse(p.ReceiveBytes),
|
||||
Transmitted: utils.FlowCalculation().Parse(p.TransmitBytes),
|
||||
IsOnline: isOnline,
|
||||
LastHandShake: p.LastHandshakeTime.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
utils.GinResponse(c).OKWithData(data)
|
||||
}
|
||||
|
@@ -8,6 +8,12 @@ type ClientList struct {
|
||||
page
|
||||
}
|
||||
|
||||
// ClientStatusList
|
||||
// @description: 客户端状态列表
|
||||
type ClientStatusList struct {
|
||||
page
|
||||
}
|
||||
|
||||
// SaveClient
|
||||
// @description: 新增/编辑客户端
|
||||
type SaveClient struct {
|
||||
|
Reference in New Issue
Block a user