wireguard-dashboard/http/api/dashboard.go

113 lines
2.8 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) {
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)
}