增加检查IP状态接口

This commit is contained in:
刘祥超
2021-02-02 19:29:36 +08:00
parent a176b109a4
commit a819d87b02
9 changed files with 306 additions and 30 deletions

View File

@@ -2,18 +2,25 @@ package utils
import (
"encoding/binary"
"github.com/cespare/xxhash/v2"
"math"
"net"
"strings"
)
// 将IP转换为整型
func IP2Long(ip string) uint32 {
// 注意IPv6没有顺序
func IP2Long(ip string) uint64 {
if len(ip) == 0 {
return 0
}
s := net.ParseIP(ip)
if s == nil {
if len(s) == 0 {
return 0
}
if len(s) == 16 {
return binary.BigEndian.Uint32(s[12:16])
if strings.Contains(ip, ":") {
return math.MaxUint32 + xxhash.Sum64(s)
}
return binary.BigEndian.Uint32(s)
return uint64(binary.BigEndian.Uint32(s.To4()))
}

View File

@@ -14,5 +14,5 @@ func VersionToLong(version string) uint32 {
} else if countDots == 0 {
version += ".0.0.0"
}
return IP2Long(version)
return uint32(IP2Long(version))
}