提升IP名单性能

This commit is contained in:
刘祥超
2024-03-30 14:42:56 +08:00
parent 10319ab48f
commit b4995868c9
10 changed files with 496 additions and 245 deletions

View File

@@ -8,9 +8,9 @@ import (
"strings"
)
// IP2Long 将IP转换为整型
// IP2LongHash 非标地将IP转换为整型
// 注意IPv6没有顺序
func IP2Long(ip string) uint64 {
func IP2LongHash(ip string) uint64 {
if len(ip) == 0 {
return 0
}

View File

@@ -7,12 +7,12 @@ import (
)
func TestIP2Long(t *testing.T) {
t.Log(utils.IP2Long("0.0.0.0"))
t.Log(utils.IP2Long("1.0.0.0"))
t.Log(utils.IP2Long("0.0.0.0.0"))
t.Log(utils.IP2Long("2001:db8:0:1::101"))
t.Log(utils.IP2Long("2001:db8:0:1::102"))
t.Log(utils.IP2Long("::1"))
t.Log(utils.IP2LongHash("0.0.0.0"))
t.Log(utils.IP2LongHash("1.0.0.0"))
t.Log(utils.IP2LongHash("0.0.0.0.0"))
t.Log(utils.IP2LongHash("2001:db8:0:1::101"))
t.Log(utils.IP2LongHash("2001:db8:0:1::102"))
t.Log(utils.IP2LongHash("::1"))
}
func TestIsLocalIP(t *testing.T) {

View File

@@ -1,12 +1,13 @@
package utils
import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"strings"
)
// 计算版本代号
// VersionToLong 计算版本代号
func VersionToLong(version string) uint32 {
countDots := strings.Count(version, ".")
var countDots = strings.Count(version, ".")
if countDots == 2 {
version += ".0"
} else if countDots == 1 {
@@ -14,5 +15,5 @@ func VersionToLong(version string) uint32 {
} else if countDots == 0 {
version += ".0.0.0"
}
return uint32(IP2Long(version))
return uint32(configutils.IPString2Long(version))
}

View File

@@ -0,0 +1,22 @@
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package utils_test
import (
"github.com/TeaOSLab/EdgeNode/internal/utils"
"testing"
)
func TestVersionToLong(t *testing.T) {
for _, v := range []string{
"",
"a",
"1",
"1.2",
"1.2.1",
"1.2.1.4",
"1.2.3.4.5",
} {
t.Log(v, "=>", utils.VersionToLong(v))
}
}