mirror of
https://github.com/zu1k/nali.git
synced 2025-01-23 05:39:03 +08:00
Optimized ip checking algorithm
This commit is contained in:
parent
144ea7f647
commit
7814171982
@ -6,7 +6,6 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ipv4re0 *regexp.Regexp
|
||||
ipv4re *regexp.Regexp
|
||||
|
||||
ipv6re0 *regexp.Regexp
|
||||
@ -14,21 +13,44 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
ipv4re0 = regexp.MustCompile(`^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$`)
|
||||
ipv4re = regexp.MustCompile(`(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}`)
|
||||
|
||||
ipv6re0 = regexp.MustCompile(`^fe80:(:[0-9a-fA-F]{1,4}){0,4}(%\w+)?|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?$`)
|
||||
ipv6re = regexp.MustCompile(`fe80:(:[0-9a-fA-F]{1,4}){0,4}(%\w+)?|([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4})?`)
|
||||
}
|
||||
|
||||
func ValidIP4(str string) bool {
|
||||
str = strings.Trim(str, " ")
|
||||
return ipv4re0.MatchString(str)
|
||||
func ValidIP4(IP string) bool {
|
||||
arr := strings.Split(IP, ".")
|
||||
if len(arr) != 4 {
|
||||
return false
|
||||
}
|
||||
for _, elem := range arr {
|
||||
if elem == "" {
|
||||
return false
|
||||
}
|
||||
if len(elem) > 1 && elem[0] == '0' {
|
||||
return false
|
||||
}
|
||||
num := 0
|
||||
for _, c := range elem {
|
||||
if c >= '0' && c <= '9' {
|
||||
num = num*10 + int(c-'0')
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if num > 255 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func ValidIP6(str string) bool {
|
||||
str = strings.Trim(str, " ")
|
||||
return ipv6re0.MatchString(str)
|
||||
|
||||
//return isIPV6(str)
|
||||
}
|
||||
|
||||
func GetIP4FromString(str string) []string {
|
||||
@ -40,3 +62,29 @@ func GetIP6FromString(str string) []string {
|
||||
str = strings.Trim(str, " ")
|
||||
return ipv6re.FindAllString(str, -1)
|
||||
}
|
||||
|
||||
//IPV6地址的判断:
|
||||
//1. 用“:”分割字符串,若长度不等于8,则return Neither
|
||||
//2. 遍历每一个数组的每一个元素,若元素的长度大于4,则return Neither
|
||||
//3. 判断每一个元素的字符,若出现非0-9,A-F的字符,则return Neither
|
||||
func isIPV6(IP string) bool {
|
||||
IP = strings.ToUpper(IP)
|
||||
arr := strings.Split(IP, ":")
|
||||
if len(arr) != 8 {
|
||||
return false
|
||||
}
|
||||
for _, elem := range arr {
|
||||
if elem == "" || len(elem) > 4 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, c := range elem {
|
||||
if (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') {
|
||||
continue
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
@ -10,3 +10,59 @@ func TestIP4Re(t *testing.T) {
|
||||
fmt.Println(GetIP4FromString(str))
|
||||
fmt.Println(ValidIP4(str))
|
||||
}
|
||||
|
||||
func TestValidIP6(t *testing.T) {
|
||||
ipv6Valid := []string{
|
||||
"1:2:3:4:5:6:7::",
|
||||
"1:2:3:4:5:6:7:8",
|
||||
|
||||
"1:2:3:4:5:6::",
|
||||
"1:2:3:4:5:6::8",
|
||||
|
||||
"1:2:3:4:5::",
|
||||
"1:2:3:4:5::8",
|
||||
|
||||
"1:2:3:4::",
|
||||
"1:2:3:4::8",
|
||||
|
||||
"1:2:3::",
|
||||
"1:2:3::8",
|
||||
|
||||
"1:2::",
|
||||
"1:2::8",
|
||||
|
||||
"1::",
|
||||
"1::8",
|
||||
|
||||
"::",
|
||||
"::8",
|
||||
"::7:8",
|
||||
"::6:7:8",
|
||||
"::5:6:7:8",
|
||||
"::4:5:6:7:8",
|
||||
"::3:4:5:6:7:8",
|
||||
"::2:3:4:5:6:7:8",
|
||||
|
||||
"::192.168.1.1",
|
||||
"::ffff:135.75.43.52",
|
||||
"A:0f:0F:FFFF:5:6:7:8",
|
||||
}
|
||||
|
||||
ipv6Invalid := []string{
|
||||
"A:0f:0F:FFFF1:5:6:7:8",
|
||||
"G:0f:0F:FFFF:5:6:7:8",
|
||||
"2001::25de::cade",
|
||||
}
|
||||
|
||||
for _, i := range ipv6Valid {
|
||||
if !ValidIP6(i) {
|
||||
t.Log("valid:", i)
|
||||
}
|
||||
}
|
||||
|
||||
for _, i := range ipv6Invalid {
|
||||
if ValidIP6(i) {
|
||||
t.Log("invalid:", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user