From 4b864f76bbea03224905d82a857c575dbda37231 Mon Sep 17 00:00:00 2001 From: coward Date: Wed, 10 Jul 2024 10:51:20 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E5=AE=A2=E6=88=B7=E7=AB=AF=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=96=B0=E5=A2=9E=E5=AE=A2=E6=88=B7=E7=AB=AF=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=B5=81=E9=87=8F=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/api/client.go | 20 ++++++++++++++++++++ http/router/root.go | 5 +++++ http/vo/client.go | 28 +++++++++++++++++++--------- utils/file_system.go | 9 +++++++-- utils/flow_calculation.go | 22 ++++++++++++++++++++++ 5 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 utils/flow_calculation.go diff --git a/http/api/client.go b/http/api/client.go index 13dbc47..9cd5794 100644 --- a/http/api/client.go +++ b/http/api/client.go @@ -9,6 +9,7 @@ import ( "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "os" "strings" + "time" "wireguard-ui/component" "wireguard-ui/http/param" "wireguard-ui/http/response" @@ -82,6 +83,25 @@ func (ClientApi) List(c *gin.Context) { return } + for i, v := range data { + // 获取客户端链接信息 + peer, err := component.Wireguard().GetClientByPublicKey(v.Keys.PublicKey) + if err != nil { + continue + } + var ipAllocation string + for _, iaip := range peer.AllowedIPs { + ipAllocation += iaip.String() + "," + } + data[i].DataTraffic = &vo.DataTraffic{ + 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).Paginate(data, total, p.Current, p.Size) } diff --git a/http/router/root.go b/http/router/root.go index b504cde..fb20a2d 100644 --- a/http/router/root.go +++ b/http/router/root.go @@ -2,6 +2,7 @@ package router import ( "github.com/gin-gonic/gin" + "wireguard-ui/config" ) type Option func(engine *gin.RouterGroup) @@ -19,6 +20,10 @@ func InitRouter() *gin.Engine { // 将请求打印至控制台 r.Use(gin.Logger()) + if config.Config.File.Type == "local" { + r.Static("/assets", config.Config.File.Path) + } + for _, opt := range options { opt(r.Group("api")) } diff --git a/http/vo/client.go b/http/vo/client.go index f6607fb..8757601 100644 --- a/http/vo/client.go +++ b/http/vo/client.go @@ -5,10 +5,9 @@ import "wireguard-ui/model" // ClientItem // @description: 客户端信息 type ClientItem struct { - Id string `json:"id"` // id - Name string `json:"name"` // 名称 - Email string `json:"email"` // 通知邮箱 - //SubnetRange string `json:"subnetRange"` // 子网范围段 + Id string `json:"id"` // id + Name string `json:"name"` // 名称 + Email string `json:"email"` // 通知邮箱 IpAllocation []string `json:"ipAllocation" gorm:"-"` // 分配的IP IpAllocationStr string `json:"-" gorm:"ipAllocationStr"` AllowedIps []string `json:"allowedIps" gorm:"-"` // 允许访问的IP @@ -19,11 +18,12 @@ type ClientItem struct { UseServerDns int `json:"useServerDns"` // 是否使用服务端DNS Keys *Keys `json:"keys" gorm:"-"` // 密钥等 KeysStr string `json:"-" gorm:"keys_str"` - CreateUser string `json:"createUser"` // 创建人 - Enabled int `json:"enabled"` // 是否启用 - OfflineMonitoring int `json:"offlineMonitoring"` // 离线通知 - CreatedAt model.JsonTime `json:"createdAt"` // 创建时间 - UpdatedAt model.JsonTime `json:"updatedAt"` // 更新时间 + CreateUser string `json:"createUser"` // 创建人 + Enabled int `json:"enabled"` // 是否启用 + OfflineMonitoring int `json:"offlineMonitoring"` // 离线通知 + DataTraffic *DataTraffic `json:"dataTraffic" gorm:"-"` // 数据流量 + CreatedAt model.JsonTime `json:"createdAt"` // 创建时间 + UpdatedAt model.JsonTime `json:"updatedAt"` // 更新时间 } type Keys struct { @@ -31,3 +31,13 @@ type Keys struct { PublicKey string `json:"publicKey"` PresharedKey string `json:"presharedKey"` } + +// DataTraffic +// @description: 数据流量 +type DataTraffic struct { + Online bool `json:"online"` // 是否在线 + ReceiveBytes string `json:"receiveBytes"` // 接收流量 + TransmitBytes string `json:"transmitBytes"` // 传输流量 + ConnectEndpoint string `json:"connectEndpoint"` // 链接端点 + LastHandAt string `json:"lastHandAt"` // 最后握手时间 +} diff --git a/utils/file_system.go b/utils/file_system.go index 6676779..be4b30a 100644 --- a/utils/file_system.go +++ b/utils/file_system.go @@ -3,7 +3,9 @@ package utils import ( "fmt" "gitee.ltd/lxh/logger/log" + "github.com/google/uuid" "os" + "strings" "time" "wireguard-ui/config" "wireguard-ui/global/client" @@ -31,15 +33,18 @@ func (fileSystem) UploadFile(file []byte, suffix string) (filePath string, err e filePath = ossObj.LongPath case "local": - filePath = fmt.Sprintf("%v/%d-avatar%s", config.Config.File.Path, time.Now().Unix(), suffix) + basePath := fmt.Sprintf("%s/%d/avatar", config.Config.File.Path, time.Now().Unix()) + filePath = fmt.Sprintf("%s/%s%s", basePath, strings.ReplaceAll(uuid.NewString(), "-", ""), suffix) // 创建目录 - if err = os.MkdirAll(filePath, os.FileMode(0777)); err != nil { + if err = os.MkdirAll(basePath, os.FileMode(0777)); err != nil { log.Errorf("本地存储目录创建失败: %v", err) return "", err } if err = os.WriteFile(filePath, file, os.FileMode(0777)); err != nil { return "", err } + subPath := strings.ReplaceAll(filePath, fmt.Sprintf("%s/", config.Config.File.Path), "") + filePath = fmt.Sprintf("http://%s/assets/%s", config.Config.Http.Endpoint, subPath) } return filePath, err } diff --git a/utils/flow_calculation.go b/utils/flow_calculation.go new file mode 100644 index 0000000..7292056 --- /dev/null +++ b/utils/flow_calculation.go @@ -0,0 +1,22 @@ +package utils + +import ( + "github.com/dustin/go-humanize" + "math/big" +) + +type flowCalculation struct{} + +func FlowCalculation() flowCalculation { + return flowCalculation{} +} + +// Parse +// @description: 解析流量,序列化为字符串 +// @receiver flowCalculation +// @param b +// @return string +func (flowCalculation) Parse(b int64) string { + b2 := big.Int{} + return humanize.BigBytes(b2.SetInt64(b)) +}