From c93cf01fe0c771feb189878cbaf07785fbf4dc4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 10 Aug 2023 14:47:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=87=E6=8D=A2util=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 3 ++- go.sum | 4 ++++ middleware/init_request.go | 5 +++-- middleware/ip_validate.go | 6 +++--- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 679b764..5f6607c 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.20 require ( git.zhangdeman.cn/zhangdeman/exception v0.0.0-20230609101214-16920b714d94 - git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810035709-c318ed9f367d + git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562 github.com/gin-gonic/gin v1.9.1 github.com/go-playground/validator/v10 v10.15.0 github.com/pkg/errors v0.9.1 @@ -13,6 +13,7 @@ require ( ) require ( + git.zhangdeman.cn/zhangdeman/network v0.0.0-20230810064349-0948cd51c01d // indirect git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230810031624-94c86f913f9c // indirect github.com/BurntSushi/toml v1.3.2 // indirect github.com/Jeffail/gabs v1.4.0 // indirect diff --git a/go.sum b/go.sum index 29b7404..c809acb 100644 --- a/go.sum +++ b/go.sum @@ -2,10 +2,14 @@ git.zhangdeman.cn/zhangdeman/exception v0.0.0-20220703050824-0679ce509241 h1:tBa git.zhangdeman.cn/zhangdeman/exception v0.0.0-20220703050824-0679ce509241/go.mod h1:mIMM/t9BkrKHAcDCmarLCHQhHfWf0/ZjtcqJPboqmSA= git.zhangdeman.cn/zhangdeman/exception v0.0.0-20230609101214-16920b714d94 h1:0A37IoVHd71aUADVueGE6MEUPFsakQS7KYHhwKd9MEE= git.zhangdeman.cn/zhangdeman/exception v0.0.0-20230609101214-16920b714d94/go.mod h1:Voc8J4ordx7nuMWpgACXXZULQy7ZIuBzcEIoS8VnDIw= +git.zhangdeman.cn/zhangdeman/network v0.0.0-20230810064349-0948cd51c01d h1:xDIqlRdXIM5U87dZCR2ua9uZ0cI8ryv12mLkinWdUFM= +git.zhangdeman.cn/zhangdeman/network v0.0.0-20230810064349-0948cd51c01d/go.mod h1:BPahs8KYF7x/YHB46oCMZXR8asEX48ZnwUGI0px9UiI= git.zhangdeman.cn/zhangdeman/util v0.0.0-20220714054940-3cfcad0c6547 h1:ieuPrfZDDgwz9ANQdXQrGNWlKWN6bK3qOUC+XUAITrk= git.zhangdeman.cn/zhangdeman/util v0.0.0-20220714054940-3cfcad0c6547/go.mod h1:zTir/0IWdK3E7n0GiaogyWHADAQnBtTdl2I6Z2/OPqw= git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810035709-c318ed9f367d h1:oju8MKvJ82l0P0I3mnKrhcF3yw9Ck3AqOPh8fM0g32w= git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810035709-c318ed9f367d/go.mod h1:trYFOShINaQBvinQrH4A0G2kfL22Y2lygEcAiGDt/sc= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562 h1:wo0r4mexqkPzQ1SZOw5z8A7FJ3ne1G6A/qWR3iaqlhw= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562/go.mod h1:trYFOShINaQBvinQrH4A0G2kfL22Y2lygEcAiGDt/sc= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230810031624-94c86f913f9c h1:n7lws3x/eR4DlcubTMepiLEgJikSN22sHYqJRaYWIkg= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20230810031624-94c86f913f9c/go.mod h1:pL3BbQdogtClFCrsiAyzBIQRWKSSIf3v2GuzZu6dGWY= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= diff --git a/middleware/init_request.go b/middleware/init_request.go index d2634f1..67c81c7 100644 --- a/middleware/init_request.go +++ b/middleware/init_request.go @@ -14,6 +14,7 @@ import ( "git.zhangdeman.cn/zhangdeman/gin/define" + networkUtil "git.zhangdeman.cn/zhangdeman/network/util" "git.zhangdeman.cn/zhangdeman/util" "github.com/gin-gonic/gin" ) @@ -43,8 +44,8 @@ func getTraceID(ctx *gin.Context) string { return fmt.Sprintf( "%v-%v-%v-%v", time.Now().UnixNano()/1e6, - strings.ReplaceAll(util.IP.GetHostIP(), ".", ""), - strings.ReplaceAll(util.IP.GetRemoteIP(ctx.Request), ".", ""), + strings.ReplaceAll(networkUtil.IP.GetHostIP(), ".", ""), + strings.ReplaceAll(networkUtil.IP.GetRemoteIP(ctx.Request), ".", ""), util.String.Md5(util.String.GenRandom("", 32)), ) } diff --git a/middleware/ip_validate.go b/middleware/ip_validate.go index 0f970ea..8df84d8 100644 --- a/middleware/ip_validate.go +++ b/middleware/ip_validate.go @@ -10,7 +10,7 @@ package middleware import ( "git.zhangdeman.cn/zhangdeman/gin/define" "git.zhangdeman.cn/zhangdeman/gin/response" - "git.zhangdeman.cn/zhangdeman/util" + networkUtil "git.zhangdeman.cn/zhangdeman/network/util" "github.com/gin-gonic/gin" ) @@ -26,7 +26,7 @@ func ValidateBlackIPMiddleware(code interface{}, httpCode int, validateFunc defi ctx.Next() return } - if validateFunc(ctx, util.IP.GetRemoteIP(ctx.Request)) { + if validateFunc(ctx, networkUtil.IP.GetRemoteIP(ctx.Request)) { // 命中黑名单 response.Send(ctx, code, httpCode, nil) ctx.Abort() @@ -48,7 +48,7 @@ func ValidateWhiteIPMiddleware(code interface{}, httpCode int, validateFunc defi ctx.Next() return } - if !validateFunc(ctx, util.IP.GetRemoteIP(ctx.Request)) { + if !validateFunc(ctx, networkUtil.IP.GetRemoteIP(ctx.Request)) { // 非名单 response.Send(ctx, code, httpCode, nil) ctx.Abort()