🐛修复模板渲染bug
All checks were successful
continuous-integration/drone/tag Build is passing

This commit is contained in:
coward 2024-08-20 14:24:13 +08:00
parent af76eda4bc
commit 941b8da804
3 changed files with 18 additions and 25 deletions

View File

@ -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,
}
}

View File

@ -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)
}

View File

@ -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 }}