diff --git a/component/template.go b/component/template.go index 7ae4d2f..21919d3 100644 --- a/component/template.go +++ b/component/template.go @@ -4,6 +4,7 @@ import ( "gitee.ltd/lxh/logger/log" "html/template" "os" + "strings" ) type TemplateComponent struct{} @@ -47,7 +48,7 @@ func (t TemplateComponent) ParseTemplate(filepath string) (tp *template.Template return } - tp, err = template.New("wg.conf").Parse(string(file)) + tp, err = template.New("wg.conf").Funcs(t.FuncMap()).Parse(string(file)) return } @@ -74,3 +75,18 @@ func (t TemplateComponent) Render(tp *template.Template, data any, filepath stri return tp.Execute(wg0Conf, data) } + +// FuncMap +// @description: 模板内的操作方法 +// @receiver t +// @return template.FuncMap +// @return error +func (t TemplateComponent) FuncMap() template.FuncMap { + sliceToString := func(str []string) string { + return strings.Join(str, ",") + } + + return template.FuncMap{ + "sliceToStr": sliceToString, + } +} diff --git a/http/api/client.go b/http/api/client.go index 89ea579..e63a34d 100644 --- a/http/api/client.go +++ b/http/api/client.go @@ -9,7 +9,6 @@ import ( "golang.zx2c4.com/wireguard/wgctrl/wgtypes" "os" "strings" - "time" "wireguard-ui/component" "wireguard-ui/http/param" "wireguard-ui/http/response" @@ -96,28 +95,6 @@ func (ClientApi) List(c *gin.Context) { return } - for i, v := range data { - if v.Keys != nil { - // 获取客户端链接信息 - 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/template/conf/wg.conf b/template/conf/wg.conf index d4f515a..359bc7c 100644 --- a/template/conf/wg.conf +++ b/template/conf/wg.conf @@ -1,5 +1,5 @@ [Interface] -Address = {{ .Server.Address|html }} +Address = {{ sliceToStr .Server.Address |html }} ListenPort = {{ .Server.ListenPort|html }} PrivateKey = {{ .Server.PrivateKey|html }} MTU = {{ .Server.MTU|html }}