IP名单改成同时检查多个IP来源

This commit is contained in:
刘祥超
2021-02-02 19:32:19 +08:00
parent 881593f8b6
commit 5d58321167
5 changed files with 101 additions and 33 deletions

View File

@@ -11,13 +11,16 @@ import (
// 将IP转换为整型
// 注意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 strings.Contains(ip, ":") {
return math.MaxUint32 + xxhash.Sum64String(ip)
return math.MaxUint32 + xxhash.Sum64(s)
}
return uint64(binary.BigEndian.Uint32(s.To4()))
}

View File

@@ -8,4 +8,5 @@ func TestIP2Long(t *testing.T) {
t.Log(IP2Long("0.0.0.0.0"))
t.Log(IP2Long("2001:db8:0:1::101"))
t.Log(IP2Long("2001:db8:0:1::102"))
t.Log(IP2Long("::1"))
}