mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-31 18:46:36 +08:00
IP名单新增IPv6和所有IP两种类型
This commit is contained in:
@@ -2,18 +2,22 @@ package utils
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"github.com/cespare/xxhash"
|
||||
"math"
|
||||
"net"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 将IP转换为整型
|
||||
func IP2Long(ip string) uint32 {
|
||||
// 注意IPv6没有顺序
|
||||
func IP2Long(ip string) uint64 {
|
||||
s := net.ParseIP(ip)
|
||||
if s == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
if len(s) == 16 {
|
||||
return binary.BigEndian.Uint32(s[12:16])
|
||||
if strings.Contains(ip, ":") {
|
||||
return math.MaxUint32 + xxhash.Sum64String(ip)
|
||||
}
|
||||
return binary.BigEndian.Uint32(s)
|
||||
return uint64(binary.BigEndian.Uint32(s.To4()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user