Files
EdgeNode/internal/utils/ip.go

27 lines
408 B
Go
Raw Normal View History

package utils
import (
"encoding/binary"
"github.com/cespare/xxhash"
"math"
"net"
"strings"
)
2021-07-20 19:01:37 +08:00
// IP2Long 将IP转换为整型
// 注意IPv6没有顺序
func IP2Long(ip string) uint64 {
if len(ip) == 0 {
return 0
}
s := net.ParseIP(ip)
if len(s) == 0 {
return 0
}
if strings.Contains(ip, ":") {
return math.MaxUint32 + xxhash.Sum64(s)
}
return uint64(binary.BigEndian.Uint32(s.To4()))
}