From 80ce105963d81609d3bf64e9b2824c199de6e21d Mon Sep 17 00:00:00 2001 From: mJace Date: Fri, 30 Aug 2024 14:51:24 +0800 Subject: [PATCH] Enhance func:parseIPList to handle mixed validity IP list. --- pkg/router/template/template_helper.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/router/template/template_helper.go b/pkg/router/template/template_helper.go index 3bf0c821b..25e40770d 100644 --- a/pkg/router/template/template_helper.go +++ b/pkg/router/template/template_helper.go @@ -376,17 +376,29 @@ func parseIPList(list string) string { return "" } + var validIPs []string + var invalidIPs []string + ipList := strings.Fields(list) for _, ip := range ipList { - if net.ParseIP(ip) == nil { - if _, _, err := net.ParseCIDR(ip); err != nil { - log.V(7).Info("parseIPList found not IP/CIDR item", "value", ip, "err", err) - return "" - } + if net.ParseIP(ip) != nil || net.ParseCIDR(ip) != nil { + validIPs = append(validIPs, ip) + } else { + invalidIPs = append(invalidIPs, ip) } } - log.V(7).Info("parseIPList parsed the list", "value", list) - return list + + if len(invalidIPs) > 0 { + log.V(7).Info("parseIPList found invalid IP/CIDR items", "invalidIPs", invalidIPs) + } + + if len(validIPs) == 0 { + return "" + } + + result := strings.Join(validIPs, " ") + log.V(7).Info("parseIPList parsed the list", "validIPs", result) + return result } var helperFunctions = template.FuncMap{