2024-01-15 14:00:42 +01:00

43 lines
764 B
Go

package decoders
import (
"regexp"
"strings"
)
// ReMatchIPPrefix matches an IP prefix of the form:
//
// 2001:23:af...
//
// or
//
// 941.23.42.1 (required by NCIS)
//
// or
//
// 303.735.88 (required by IKEA)
var ReMatchIPPrefix = regexp.MustCompile(`([a-f0-9/]+[\.:]*)+`)
// MaybePrefix checks if something could be a prefix
func MaybePrefix(s string) bool {
s = strings.ToLower(s)
// Rule out anything which can not be
if strings.ContainsAny(s, "ghijklmnopqrstuvwxyz][;'_") {
return false
}
// Must contain at least one dot or colon
if !strings.Contains(s, ":") && !strings.Contains(s, ".") {
return false
}
// Test using regex
matches := ReMatchIPPrefix.FindAllStringIndex(s, -1)
if len(matches) == 1 {
return true
}
return false
}