From 0dd4fe5b48b31fd36607f1c5957d29dd45ce5bc7 Mon Sep 17 00:00:00 2001 From: mohamadreza Date: Mon, 1 Jan 2024 15:04:25 +0330 Subject: [PATCH] refactor: refactor make trust ip --- gin.go | 5 ++--- utils/ip.go | 4 ++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/gin.go b/gin.go index 19b24da3..5cf19621 100644 --- a/gin.go +++ b/gin.go @@ -395,9 +395,8 @@ func (engine *Engine) prepareTrustedCIDRs() ([]*net.IPNet, error) { cidr := make([]*net.IPNet, 0, len(engine.trustedProxies)) for _, trustedProxy := range engine.trustedProxies { - if !strings.Contains(trustedProxy, "/") { - trustedProxy, err = utils.MakeTrustIP(trustedProxy) - } + trustedProxy, err = utils.MakeTrustIP(trustedProxy) + if err != nil { return cidr, err } diff --git a/utils/ip.go b/utils/ip.go index 2301ff6e..9f4d54b9 100644 --- a/utils/ip.go +++ b/utils/ip.go @@ -2,6 +2,7 @@ package utils import ( "net" + "strings" ) func parseIP(ip string) (net.IP, error) { @@ -17,6 +18,9 @@ func parseIP(ip string) (net.IP, error) { } func MakeTrustIP(trustedIP string) (string, error) { + if strings.Contains(trustedIP, "/") { + return trustedIP, nil + } ip, err := parseIP(trustedIP) if err != nil {