43 lines
764 B
Go
Raw Permalink Normal View History

2021-10-27 14:48:52 +00:00
package decoders
import (
"regexp"
"strings"
)
// ReMatchIPPrefix matches an IP prefix of the form:
2022-11-10 10:58:15 +01:00
//
// 2001:23:af...
//
2021-10-27 14:48:52 +00:00
// or
2022-11-10 10:58:15 +01:00
//
// 941.23.42.1 (required by NCIS)
//
2021-10-27 14:48:52 +00:00
// or
2022-11-10 10:58:15 +01:00
//
// 303.735.88 (required by IKEA)
2021-10-27 14:48:52 +00:00
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
}
2024-01-15 14:00:42 +01:00
// Must contain at least one dot or colon
if !strings.Contains(s, ":") && !strings.Contains(s, ".") {
return false
}
2021-10-27 14:48:52 +00:00
// Test using regex
matches := ReMatchIPPrefix.FindAllStringIndex(s, -1)
if len(matches) == 1 {
return true
}
return false
}