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 {