2020-07-21 17:23:38 +08:00
|
|
|
|
package tools
|
2020-07-17 10:52:36 +08:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2020-09-07 10:23:35 +08:00
|
|
|
|
ipv4re *regexp.Regexp
|
2020-07-18 09:52:59 +08:00
|
|
|
|
|
|
|
|
|
ipv6re0 *regexp.Regexp
|
|
|
|
|
ipv6re *regexp.Regexp
|
2020-07-17 10:52:36 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
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}`)
|
2020-07-18 09:52:59 +08:00
|
|
|
|
|
|
|
|
|
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})?`)
|
2020-07-17 10:52:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 10:23:35 +08:00
|
|
|
|
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
|
2020-07-17 10:52:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-07 19:00:32 +08:00
|
|
|
|
func ValidIP6Re(str string) bool {
|
2020-07-18 09:52:59 +08:00
|
|
|
|
str = strings.Trim(str, " ")
|
|
|
|
|
return ipv6re0.MatchString(str)
|
2020-09-07 10:23:35 +08:00
|
|
|
|
|
|
|
|
|
//return isIPV6(str)
|
2020-07-18 09:52:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-17 10:52:36 +08:00
|
|
|
|
func GetIP4FromString(str string) []string {
|
|
|
|
|
str = strings.Trim(str, " ")
|
|
|
|
|
return ipv4re.FindAllString(str, -1)
|
|
|
|
|
}
|
2020-07-18 09:52:59 +08:00
|
|
|
|
|
|
|
|
|
func GetIP6FromString(str string) []string {
|
|
|
|
|
str = strings.Trim(str, " ")
|
|
|
|
|
return ipv6re.FindAllString(str, -1)
|
|
|
|
|
}
|
2020-09-07 10:23:35 +08:00
|
|
|
|
|
|
|
|
|
//IPV6地址的判断:
|
|
|
|
|
//1. 用“:”分割字符串,若长度不等于8,则return Neither
|
|
|
|
|
//2. 遍历每一个数组的每一个元素,若元素的长度大于4,则return Neither
|
|
|
|
|
//3. 判断每一个元素的字符,若出现非0-9,A-F的字符,则return Neither
|
2020-09-07 19:00:32 +08:00
|
|
|
|
func ValidIP6(IP string) bool {
|
2020-09-07 10:23:35 +08:00
|
|
|
|
IP = strings.ToUpper(IP)
|
2020-09-07 19:00:32 +08:00
|
|
|
|
n := len(IP)
|
|
|
|
|
if n > 39 || n == 0 {
|
2020-09-07 10:23:35 +08:00
|
|
|
|
return false
|
|
|
|
|
}
|
2020-09-07 19:00:32 +08:00
|
|
|
|
|
|
|
|
|
// 以 ":" 结尾 但是只有一个
|
|
|
|
|
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 {
|
2020-09-07 10:23:35 +08:00
|
|
|
|
return false
|
|
|
|
|
}
|
2020-09-07 19:00:32 +08:00
|
|
|
|
}
|
2020-09-07 10:23:35 +08:00
|
|
|
|
|
2020-09-07 19:00:32 +08:00
|
|
|
|
// 对每个元素进行遍历
|
|
|
|
|
for k := 0; k < n-1; {
|
|
|
|
|
if IP[k] == ':' {
|
|
|
|
|
k++
|
|
|
|
|
continue
|
|
|
|
|
} else if valid(IP[k]) {
|
|
|
|
|
var bits int
|
|
|
|
|
for valid(IP[k]) {
|
|
|
|
|
k++
|
|
|
|
|
bits++
|
|
|
|
|
if bits > 4 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
if k == n {
|
|
|
|
|
break
|
|
|
|
|
}
|
2020-09-07 10:23:35 +08:00
|
|
|
|
}
|
2020-09-07 19:00:32 +08:00
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
2020-09-07 10:23:35 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-07 19:00:32 +08:00
|
|
|
|
|
|
|
|
|
// 到了这一步, 可以确定是ipv6
|
2020-09-07 10:23:35 +08:00
|
|
|
|
return true
|
|
|
|
|
}
|
2020-09-07 19:00:32 +08:00
|
|
|
|
|
|
|
|
|
func valid(i uint8) bool {
|
|
|
|
|
return (i >= 'A' && i <= 'F') || (i >= '0' && i <= '9')
|
|
|
|
|
}
|