wireguard-dashboard/http/api/dashboard.go

98 lines
2.3 KiB
Go
Raw Normal View History

2024-08-08 17:26:50 +08:00
package api
import (
"fmt"
"github.com/gin-gonic/gin"
2024-08-09 11:06:58 +08:00
"time"
"wireguard-ui/component"
2024-08-08 17:26:50 +08:00
"wireguard-ui/http/param"
"wireguard-ui/http/response"
"wireguard-ui/http/vo"
"wireguard-ui/service"
2024-08-09 10:06:34 +08:00
"wireguard-ui/utils"
2024-08-08 17:26:50 +08:00
)
type DashboardApi struct{}
func Dashboard() DashboardApi {
return DashboardApi{}
}
// List
// @description: 操作日志
// @receiver DashboardApi
// @param c
func (DashboardApi) List(c *gin.Context) {
var p param.Page
if err := c.ShouldBind(&p); err != nil {
response.R(c).Validator(err)
return
}
var loginUser *vo.User
if loginUser = GetCurrentLoginUser(c); c.IsAborted() {
return
}
data, total, err := service.Log().List(p, loginUser)
if err != nil {
response.R(c).FailedWithError(fmt.Errorf("获取操作日志失败: %v", err.Error()))
return
}
response.R(c).Paginate(data, total, p.Current, p.Size)
}
2024-08-09 10:06:34 +08:00
// DailyPoetry
// @description: 每日诗词
// @receiver DashboardApi
// @param c
func (DashboardApi) DailyPoetry(c *gin.Context) {
data, err := utils.DailyPoetry().HitokotoPoetry()
if err != nil {
response.R(c).FailedWithError("获取失败")
return
}
response.R(c).OkWithData(data)
}
2024-08-09 11:06:58 +08:00
// ConnectionList
// @description: 客户端链接信息列表
// @receiver DashboardApi
// @param c
func (DashboardApi) ConnectionList(c *gin.Context) {
peers, err := component.Wireguard().GetClients()
if err != nil {
2024-08-09 11:45:32 +08:00
response.R(c).FailedWithError("获取失败")
2024-08-09 11:06:58 +08:00
return
}
2024-08-09 11:45:32 +08:00
var connections []vo.DataTraffic
2024-08-09 11:06:58 +08:00
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,
2024-08-21 14:12:43 +08:00
IpAllocation: ipAllocation,
2024-08-09 11:06:58 +08:00
Online: time.Since(peer.LastHandshakeTime).Minutes() < 3,
ReceiveBytes: utils.FlowCalculation().Parse(peer.TransmitBytes),
TransmitBytes: utils.FlowCalculation().Parse(peer.ReceiveBytes),
2024-08-21 14:12:43 +08:00
ConnectEndpoint: peer.Endpoint.String(),
2024-08-09 11:06:58 +08:00
LastHandAt: peer.LastHandshakeTime.Format("2006-01-02 15:04:05"),
})
}
2024-08-21 14:33:50 +08:00
if len(connections) <= 0 {
connections = []vo.DataTraffic{}
}
2024-08-09 11:06:58 +08:00
response.R(c).OkWithData(connections)
}