hotfix(firewall): fix ip prefix parsing for ipv6 (again)

This commit is contained in:
Quentin McGaw
2024-08-19 17:06:45 +00:00
parent 946f055fed
commit 3f130931d2
2 changed files with 63 additions and 6 deletions

View File

@@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/netip"
"regexp"
"slices"
"strconv"
"strings"
@@ -153,11 +152,15 @@ func parseInstructionFlag(key, value string, instruction *iptablesInstruction) (
return nil
}
var regexCidrSuffix = regexp.MustCompile(`/[0-9][0-9]{0,2}$`)
func parseIPPrefix(value string) (prefix netip.Prefix, err error) {
if !regexCidrSuffix.MatchString(value) {
value += "/32"
slashIndex := strings.Index(value, "/")
if slashIndex >= 0 {
return netip.ParsePrefix(value)
}
return netip.ParsePrefix(value)
ip, err := netip.ParseAddr(value)
if err != nil {
return netip.Prefix{}, fmt.Errorf("parsing IP address: %w", err)
}
return netip.PrefixFrom(ip, ip.BitLen()), nil
}