Skip to content

Commit

Permalink
Enhance func:parseIPList to handle mixed validity IP list.
Browse files Browse the repository at this point in the history
  • Loading branch information
mJace committed Aug 30, 2024
1 parent 47b8420 commit 80ce105
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pkg/router/template/template_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 80ce105

Please sign in to comment.