From 1e5aebbdc9d6f50f002ec07a7f9e8c462ce70ed9 Mon Sep 17 00:00:00 2001 From: uubulb Date: Sat, 4 Jan 2025 15:09:46 +0800 Subject: [PATCH 1/2] feat(waf): return ip in string literal --- cmd/dashboard/controller/waf.go | 16 +++++++++++++--- pkg/geoip/geoip.db | 2 +- pkg/utils/utils.go | 11 +++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cmd/dashboard/controller/waf.go b/cmd/dashboard/controller/waf.go index 91eb389281..1b348236c3 100644 --- a/cmd/dashboard/controller/waf.go +++ b/cmd/dashboard/controller/waf.go @@ -1,6 +1,8 @@ package controller import ( + "net" + "slices" "strconv" "github.com/gin-gonic/gin" @@ -21,7 +23,7 @@ import ( // @Produce json // @Success 200 {object} model.PaginatedResponse[[]model.WAFApiMock, model.WAFApiMock] // @Router /waf [get] -func listBlockedAddress(c *gin.Context) (*model.Value[[]*model.WAF], error) { +func listBlockedAddress(c *gin.Context) (*model.Value[[]*model.WAFApiMock], error) { limit, err := strconv.Atoi(c.Query("limit")) if err != nil || limit < 1 { limit = 25 @@ -42,8 +44,16 @@ func listBlockedAddress(c *gin.Context) (*model.Value[[]*model.WAF], error) { return nil, err } - return &model.Value[[]*model.WAF]{ - Value: waf, + return &model.Value[[]*model.WAFApiMock]{ + Value: slices.Collect(utils.Map(slices.Values(waf), func(e *model.WAF) *model.WAFApiMock { + return &model.WAFApiMock{ + IP: net.IP(e.IP).String(), + BlockIdentifier: e.BlockIdentifier, + BlockReason: e.BlockReason, + BlockTimestamp: e.BlockTimestamp, + Count: e.Count, + } + })), Pagination: model.Pagination{ Offset: offset, Limit: limit, diff --git a/pkg/geoip/geoip.db b/pkg/geoip/geoip.db index 7bd069c80f..39802f6428 100644 --- a/pkg/geoip/geoip.db +++ b/pkg/geoip/geoip.db @@ -1 +1 @@ -stub \ No newline at end of file +stub diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 98ab9bf3ae..452f2aa1ba 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -3,6 +3,7 @@ package utils import ( "crypto/rand" "errors" + "iter" "maps" "math/big" "net/netip" @@ -164,3 +165,13 @@ func Unique[T comparable](s []T) []T { } return ret } + +func Map[T, U any](seq iter.Seq[T], f func(e T) U) iter.Seq[U] { + return func(yield func(U) bool) { + for e := range seq { + if !yield(f(e)) { + return + } + } + } +} From a2a5b03a6aa37fafd080de66908fccf25a9979a9 Mon Sep 17 00:00:00 2001 From: uubulb Date: Sat, 4 Jan 2025 20:23:44 +0800 Subject: [PATCH 2/2] rename --- cmd/dashboard/controller/waf.go | 2 +- pkg/utils/utils.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/dashboard/controller/waf.go b/cmd/dashboard/controller/waf.go index 1b348236c3..4b5faf7334 100644 --- a/cmd/dashboard/controller/waf.go +++ b/cmd/dashboard/controller/waf.go @@ -45,7 +45,7 @@ func listBlockedAddress(c *gin.Context) (*model.Value[[]*model.WAFApiMock], erro } return &model.Value[[]*model.WAFApiMock]{ - Value: slices.Collect(utils.Map(slices.Values(waf), func(e *model.WAF) *model.WAFApiMock { + Value: slices.Collect(utils.ConvertSeq(slices.Values(waf), func(e *model.WAF) *model.WAFApiMock { return &model.WAFApiMock{ IP: net.IP(e.IP).String(), BlockIdentifier: e.BlockIdentifier, diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 452f2aa1ba..cd8ba76b2b 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -166,7 +166,7 @@ func Unique[T comparable](s []T) []T { return ret } -func Map[T, U any](seq iter.Seq[T], f func(e T) U) iter.Seq[U] { +func ConvertSeq[T, U any](seq iter.Seq[T], f func(e T) U) iter.Seq[U] { return func(yield func(U) bool) { for e := range seq { if !yield(f(e)) {