Skip to content

Commit

Permalink
fix a1:a2:a3:a4::b1:b2:b3:b4 is not ipv6 address
Browse files Browse the repository at this point in the history
  • Loading branch information
oldthreefeng committed Sep 7, 2020
1 parent 36e6351 commit e8fba71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 13 additions & 0 deletions internal/tools/ipparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func ValidIP6(IP string) bool {
if n > 39 || n == 0 {
return false
}

// 以 ":" 结尾 但是只有一个
if strings.HasSuffix(IP, ":") && !strings.HasSuffix(IP, "::") {
return false
Expand All @@ -90,6 +91,18 @@ func ValidIP6(IP string) bool {
if ValidIP4(tmp[len(tmp)-1]) {
return true
}
if strings.Contains(IP,"::") {
var count int
for _, v := range tmp {
if v != "" {
count++
continue
}
}
if count == 8 {
return false
}
}

// 对每个元素进行遍历
for k := 0; k < n-1; {
Expand Down
27 changes: 26 additions & 1 deletion internal/tools/iptools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestValidIP6(t *testing.T) {
"G:0f:0F:FFFF:5:6:7:8",
"2001::25de::cade",
"2001:0db8:85a3:0:0:8A2E:0370:73341",
"a1:a2:a3:a4::b1:b2:b3:b4",
}

for _, i := range ipv6Valid {
Expand Down Expand Up @@ -83,4 +84,28 @@ func BenchmarkValidIP6(b *testing.B) {
for i:=0; i<b.N;i++ {
ValidIP6(origin)
}
}
}

/*
IPv6-addr = IPv6-full / IPv6-comp / IPv6v4-full / IPv6v4-comp
IPv6-hex = 1*4HEXDIG
IPv6-full = IPv6-hex 7(":" IPv6-hex)
IPv6-comp = [IPv6-hex *5(":" IPv6-hex)] "::"
[IPv6-hex *5(":" IPv6-hex)]
; The "::" represents at least 2 16-bit groups of
; zeros. No more than 6 groups in addition to the
; "::" may be present.
IPv6v4-full = IPv6-hex 5(":" IPv6-hex) ":" IPv4-address-literal
IPv6v4-comp = [IPv6-hex *3(":" IPv6-hex)] "::"
[IPv6-hex *3(":" IPv6-hex) ":"]
IPv4-address-literal
; The "::" represents at least 2 16-bit groups of
; zeros. No more than 4 groups in addition to the
; "::" and IPv4-address-literal may be present.
*/

0 comments on commit e8fba71

Please sign in to comment.