1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-23 21:59:02 +08:00

better ValidIP

This commit is contained in:
zu1k 2020-09-28 08:26:08 +08:00
parent 35d2fc4db7
commit f624993476
3 changed files with 48 additions and 134 deletions

View File

@ -44,13 +44,17 @@ func ParseIPs(ips []string) {
db1 = nil db1 = nil
} }
for _, ip := range ips { for _, ip := range ips {
if tools.ValidIP4(ip) { v := tools.ValidIP(ip)
switch v {
case tools.ValidIPv4:
result := db0.Find(ip) result := db0.Find(ip)
fmt.Println(formatResult(ip, result)) fmt.Println(formatResult(ip, result))
} else if tools.ValidIP6(ip) && db1 != nil { case tools.ValidIPv6:
if db1 != nil {
result := db1.Find(ip) result := db1.Find(ip)
fmt.Println(formatResult(ip, result)) fmt.Println(formatResult(ip, result))
} else { }
default:
fmt.Println(ReplaceIPInString(ip)) fmt.Println(ReplaceIPInString(ip))
} }
} }

View File

@ -1,6 +1,7 @@
package tools package tools
import ( import (
"net"
"regexp" "regexp"
"strings" "strings"
) )
@ -19,40 +20,6 @@ func init() {
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})?`) 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(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 ValidIP6Re(str string) bool {
str = strings.Trim(str, " ")
return ipv6re0.MatchString(str)
//return isIPV6(str)
}
func GetIP4FromString(str string) []string { func GetIP4FromString(str string) []string {
str = strings.Trim(str, " ") str = strings.Trim(str, " ")
return ipv4re.FindAllString(str, -1) return ipv4re.FindAllString(str, -1)
@ -63,74 +30,25 @@ func GetIP6FromString(str string) []string {
return ipv6re.FindAllString(str, -1) return ipv6re.FindAllString(str, -1)
} }
//IPV6地址的判断 const (
//1. 用“”分割字符串若长度不等于8则return Neither ValidIPv4 = iota
//2. 遍历每一个数组的每一个元素若元素的长度大于4则return Neither ValidIPv6
//3. 判断每一个元素的字符若出现非0-9A-F的字符则return Neither InvalidIP
func ValidIP6(IP string) bool { )
IP = strings.ToUpper(IP)
n := len(IP)
if n > 39 || n == 0 {
return false
}
// 以 ":" 结尾 但是只有一个 type Valid int
if strings.HasSuffix(IP, ":") && !strings.HasSuffix(IP, "::") {
return false
}
// 如果"::" 有两个以上
if strings.Count(IP, "::") > 1 {
return false
}
// 如果 ":" 有8个以上
if strings.Count(IP, ":") > 8 {
return false
}
tmp := strings.Split(IP, ":")
// 如果有ipv4 则返回真, 前面的部分未校验。
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
}
}
// 对每个元素进行遍历 func ValidIP(IP string) (v Valid) {
for k := 0; k < n-1; { for i := 0; i < len(IP); i++ {
if IP[k] == ':' { switch IP[i] {
k++ case '.':
continue v = ValidIPv4
} else if valid(IP[k]) { case ':':
var bits int v = ValidIPv6
for valid(IP[k]) {
k++
bits++
if bits > 4 {
return false
}
if k == n {
break
} }
} }
if ip := net.ParseIP(IP); ip != nil {
} else { return v
return false
} }
} return InvalidIP
// 到了这一步, 可以确定是ipv6
return true
}
func valid(i uint8) bool {
return (i >= 'A' && i <= 'F') || (i >= '0' && i <= '9')
} }

View File

@ -8,7 +8,7 @@ import (
func TestIP4Re(t *testing.T) { func TestIP4Re(t *testing.T) {
str := "aaa1.1.11.23a36.36.32.200" str := "aaa1.1.11.23a36.36.32.200"
fmt.Println(GetIP4FromString(str)) fmt.Println(GetIP4FromString(str))
fmt.Println(ValidIP4(str)) ValidIP(str)
} }
func TestValidIP6(t *testing.T) { func TestValidIP6(t *testing.T) {
@ -58,31 +58,23 @@ func TestValidIP6(t *testing.T) {
} }
for _, i := range ipv6Valid { for _, i := range ipv6Valid {
if !ValidIP6(i) { if v := ValidIP(i); v == InvalidIP {
t.Log("valid:", i) t.Log("valid:", i)
} }
} }
for _, i := range ipv6Invalid { for _, i := range ipv6Invalid {
if ValidIP6(i) { if v := ValidIP(i); v != InvalidIP {
t.Log("invalid:", i) t.Log("invalid:", i)
} }
} }
} }
func BenchmarkValidIP6Re(b *testing.B) { func BenchmarkValidIP6STD(b *testing.B) {
b.ResetTimer() b.ResetTimer()
origin := "::ffff:135.75.43.52" origin := "::ffff:135.75.43.52"
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
ValidIP6Re(origin) ValidIP(origin)
}
}
func BenchmarkValidIP6(b *testing.B) {
b.ResetTimer()
origin := "::ffff:135.75.43.52"
for i:=0; i<b.N;i++ {
ValidIP6(origin)
} }
} }