Update ipv6.go

This commit is contained in:
spiritlhl
2024-11-07 21:49:02 +08:00
committed by GitHub
parent be3c166845
commit d06175f506

View File

@@ -48,28 +48,25 @@ func getIPv6PrefixLength(interfaceName string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
// 去掉负向先行断言,手动排除 fe80 前缀
re := regexp.MustCompile(`inet6 (?!fe80:).*prefixlen (\d+)`) re := regexp.MustCompile(`inet6 ([^ ]+) prefixlen (\d+)`)
matches := re.FindAllStringSubmatch(string(output), -1) matches := re.FindAllStringSubmatch(string(output), -1)
if len(matches) == 0 { if len(matches) == 0 {
return "", nil return "", nil
} }
var prefixLens []string var prefixLens []string
for _, match := range matches { for _, match := range matches {
if len(match) > 1 { if len(match) > 2 && !strings.HasPrefix(match[1], "fe80") {
prefixLens = append(prefixLens, match[1]) prefixLens = append(prefixLens, match[2])
} }
} }
if len(prefixLens) >= 2 { if len(prefixLens) >= 2 {
sort.Strings(prefixLens) sort.Strings(prefixLens)
return prefixLens[0], nil return prefixLens[0], nil
} else if len(prefixLens) == 1 { } else if len(prefixLens) == 1 {
return prefixLens[0], nil return prefixLens[0], nil
} }
return "", nil return "", nil
} }