🎨客户端链接信息

This commit is contained in:
coward 2024-08-09 11:06:58 +08:00
parent 8f28f790e0
commit 864cf2f3f8
4 changed files with 75 additions and 2 deletions

View File

@ -3,6 +3,8 @@ package api
import (
"fmt"
"github.com/gin-gonic/gin"
"time"
"wireguard-ui/component"
"wireguard-ui/http/param"
"wireguard-ui/http/response"
"wireguard-ui/http/vo"
@ -52,3 +54,59 @@ func (DashboardApi) DailyPoetry(c *gin.Context) {
}
response.R(c).OkWithData(data)
}
// ConnectionList
// @description: 客户端链接信息列表
// @receiver DashboardApi
// @param c
func (DashboardApi) ConnectionList(c *gin.Context) {
var connections []vo.DataTraffic
peers, err := component.Wireguard().GetClients()
if err != nil {
if len(connections) == 0 {
for i := 0; i < 10; i++ {
var online bool
if i%2 == 0 {
online = true
}
connections = append(connections, vo.DataTraffic{
Name: fmt.Sprintf("客户端: %d", i+1),
Email: fmt.Sprintf("%d@qq.com", i+1),
IpAllocation: fmt.Sprintf("10.100.25.%d/32", i+1),
Online: online,
ReceiveBytes: utils.FlowCalculation().Parse(12212),
TransmitBytes: utils.FlowCalculation().Parse(322212),
ConnectEndpoint: "1.14.30.133",
LastHandAt: time.Now().Format("2006-01-02 15:04:05"),
})
}
response.R(c).OkWithData(connections)
return
}
return
}
for _, peer := range peers {
// 获取客户端链接信息
clientInfo, err := service.Client().GetByPublicKey(peer.PublicKey.String())
if err != nil {
continue
}
var ipAllocation string
for _, iaip := range peer.AllowedIPs {
ipAllocation += iaip.String() + ","
}
connections = append(connections, vo.DataTraffic{
Name: clientInfo.Name,
Email: clientInfo.Email,
IpAllocation: clientInfo.IpAllocation,
Online: time.Since(peer.LastHandshakeTime).Minutes() < 3,
ReceiveBytes: utils.FlowCalculation().Parse(peer.TransmitBytes),
TransmitBytes: utils.FlowCalculation().Parse(peer.ReceiveBytes),
ConnectEndpoint: ipAllocation,
LastHandAt: peer.LastHandshakeTime.Format("2006-01-02 15:04:05"),
})
}
response.R(c).OkWithData(connections)
}

View File

@ -12,7 +12,8 @@ import (
func DashboardApi(r *gin.RouterGroup) {
dashboard := r.Group("dashboard", middleware.Authorization(), middleware.RequestLog())
{
dashboard.GET("/request/list", api.Dashboard().List) // 请求日志
dashboard.GET("/daily-poetry", api.Dashboard().DailyPoetry) // 每日诗词
dashboard.GET("/request/list", api.Dashboard().List) // 请求日志
dashboard.GET("/daily-poetry", api.Dashboard().DailyPoetry) // 每日诗词
dashboard.GET("/connections", api.Dashboard().ConnectionList) // 客户端列表列表
}
}

View File

@ -35,6 +35,9 @@ type Keys struct {
// DataTraffic
// @description: 数据流量
type DataTraffic struct {
Name string `json:"name"` // 客户端名称
Email string `json:"email"` // 联系邮箱
IpAllocation string `json:"ipAllocation"` // 分配的IP
Online bool `json:"online"` // 是否在线
ReceiveBytes string `json:"receiveBytes"` // 接收流量
TransmitBytes string `json:"transmitBytes"` // 传输流量

View File

@ -169,3 +169,14 @@ func (s client) GetByID(id string) (data *model.Client, err error) {
err = s.Model(&model.Client{}).Where("id = ?", id).Take(&data).Error
return
}
// GetByPublicKey
// @description: 通过公钥匹配客户端信息
// @receiver s
// @param pk
// @return data
// @return err
func (s client) GetByPublicKey(pk string) (data *model.Client, err error) {
err = s.Model(&model.Client{}).Where(fmt.Sprintf("json_extract(keys, '$.publicKey') = '%s'", pk)).First(&data).Error
return
}