2020-07-21 17:23:38 +08:00
|
|
|
package tools
|
2020-07-17 10:52:36 +08:00
|
|
|
|
|
|
|
import (
|
2020-09-28 08:26:08 +08:00
|
|
|
"net"
|
2020-07-17 10:52:36 +08:00
|
|
|
"strings"
|
2020-07-18 09:52:59 +08:00
|
|
|
|
2021-07-30 22:30:27 +08:00
|
|
|
"github.com/zu1k/nali/internal/re"
|
2020-07-17 10:52:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetIP4FromString(str string) []string {
|
|
|
|
str = strings.Trim(str, " ")
|
2021-07-30 22:30:27 +08:00
|
|
|
return re.IPv4Re.FindAllString(str, -1)
|
2020-07-17 10:52:36 +08:00
|
|
|
}
|
2020-07-18 09:52:59 +08:00
|
|
|
|
|
|
|
func GetIP6FromString(str string) []string {
|
|
|
|
str = strings.Trim(str, " ")
|
2021-07-30 22:30:27 +08:00
|
|
|
return re.IPv6Re.FindAllString(str, -1)
|
2020-07-18 09:52:59 +08:00
|
|
|
}
|
2020-09-07 10:23:35 +08:00
|
|
|
|
2020-09-28 08:26:08 +08:00
|
|
|
const (
|
|
|
|
ValidIPv4 = iota
|
|
|
|
ValidIPv6
|
|
|
|
InvalidIP
|
|
|
|
)
|
2020-09-07 10:23:35 +08:00
|
|
|
|
2020-09-28 08:26:08 +08:00
|
|
|
type Valid int
|
2020-09-07 19:00:32 +08:00
|
|
|
|
2020-09-28 08:26:08 +08:00
|
|
|
func ValidIP(IP string) (v Valid) {
|
|
|
|
for i := 0; i < len(IP); i++ {
|
|
|
|
switch IP[i] {
|
|
|
|
case '.':
|
|
|
|
v = ValidIPv4
|
|
|
|
case ':':
|
|
|
|
v = ValidIPv6
|
2020-09-07 10:23:35 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-28 08:26:08 +08:00
|
|
|
if ip := net.ParseIP(IP); ip != nil {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return InvalidIP
|
2020-09-07 10:23:35 +08:00
|
|
|
}
|