From 864cf2f3f882b488f2d746293033f4ac2a950135 Mon Sep 17 00:00:00 2001 From: coward Date: Fri, 9 Aug 2024 11:06:58 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E5=AE=A2=E6=88=B7=E7=AB=AF=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/api/dashboard.go | 58 ++++++++++++++++++++++++++++++++++++++++ http/router/dashboard.go | 5 ++-- http/vo/client.go | 3 +++ service/client.go | 11 ++++++++ 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/http/api/dashboard.go b/http/api/dashboard.go index 3d3b355..faaa903 100644 --- a/http/api/dashboard.go +++ b/http/api/dashboard.go @@ -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) +} diff --git a/http/router/dashboard.go b/http/router/dashboard.go index f045356..c1f3147 100644 --- a/http/router/dashboard.go +++ b/http/router/dashboard.go @@ -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) // 客户端列表列表 } } diff --git a/http/vo/client.go b/http/vo/client.go index 8757601..233aad8 100644 --- a/http/vo/client.go +++ b/http/vo/client.go @@ -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"` // 传输流量 diff --git a/service/client.go b/service/client.go index 8b765fa..87770c6 100644 --- a/service/client.go +++ b/service/client.go @@ -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 +}