mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-04-08 22:58:21 +08:00
优化IP名单相关代码
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package iplibrary
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
)
|
||||
|
||||
@@ -40,11 +41,11 @@ func (this *IPItem) Contains(ipBytes []byte) bool {
|
||||
// 检查是否包含某个
|
||||
func (this *IPItem) containsIP(ipBytes []byte) bool {
|
||||
if IsZero(this.IPTo) {
|
||||
if CompareBytes(this.IPFrom, ipBytes) != 0 {
|
||||
if iputils.CompareBytes(this.IPFrom, ipBytes) != 0 {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
if CompareBytes(this.IPFrom, ipBytes) > 0 || CompareBytes(this.IPTo, ipBytes) < 0 {
|
||||
if iputils.CompareBytes(this.IPFrom, ipBytes) > 0 || iputils.CompareBytes(this.IPTo, ipBytes) < 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package iplibrary_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
@@ -16,64 +17,64 @@ func TestIPItem_Contains(t *testing.T) {
|
||||
|
||||
{
|
||||
var item = &iplibrary.IPItem{
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.100"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.100"),
|
||||
IPTo: nil,
|
||||
ExpiredAt: 0,
|
||||
}
|
||||
a.IsTrue(item.Contains(iplibrary.IPBytes("192.168.1.100")))
|
||||
a.IsTrue(item.Contains(iputils.ToBytes("192.168.1.100")))
|
||||
}
|
||||
|
||||
{
|
||||
var item = &iplibrary.IPItem{
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.100"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.100"),
|
||||
IPTo: nil,
|
||||
ExpiredAt: time.Now().Unix() + 1,
|
||||
}
|
||||
a.IsTrue(item.Contains(iplibrary.IPBytes("192.168.1.100")))
|
||||
a.IsTrue(item.Contains(iputils.ToBytes("192.168.1.100")))
|
||||
}
|
||||
|
||||
{
|
||||
var item = &iplibrary.IPItem{
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.100"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.100"),
|
||||
IPTo: nil,
|
||||
ExpiredAt: time.Now().Unix() - 1,
|
||||
}
|
||||
a.IsFalse(item.Contains(iplibrary.IPBytes("192.168.1.100")))
|
||||
a.IsFalse(item.Contains(iputils.ToBytes("192.168.1.100")))
|
||||
}
|
||||
{
|
||||
var item = &iplibrary.IPItem{
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.100"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.100"),
|
||||
IPTo: nil,
|
||||
ExpiredAt: 0,
|
||||
}
|
||||
a.IsFalse(item.Contains(iplibrary.IPBytes("192.168.1.101")))
|
||||
a.IsFalse(item.Contains(iputils.ToBytes("192.168.1.101")))
|
||||
}
|
||||
|
||||
{
|
||||
var item = &iplibrary.IPItem{
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPTo: iplibrary.IPBytes("192.168.1.101"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
IPTo: iputils.ToBytes("192.168.1.101"),
|
||||
ExpiredAt: 0,
|
||||
}
|
||||
a.IsTrue(item.Contains(iplibrary.IPBytes("192.168.1.100")))
|
||||
a.IsTrue(item.Contains(iputils.ToBytes("192.168.1.100")))
|
||||
}
|
||||
|
||||
{
|
||||
var item = &iplibrary.IPItem{
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPTo: iplibrary.IPBytes("192.168.1.100"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
IPTo: iputils.ToBytes("192.168.1.100"),
|
||||
ExpiredAt: 0,
|
||||
}
|
||||
a.IsTrue(item.Contains(iplibrary.IPBytes("192.168.1.100")))
|
||||
a.IsTrue(item.Contains(iputils.ToBytes("192.168.1.100")))
|
||||
}
|
||||
|
||||
{
|
||||
var item = &iplibrary.IPItem{
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPTo: iplibrary.IPBytes("192.168.1.101"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
IPTo: iputils.ToBytes("192.168.1.101"),
|
||||
ExpiredAt: 0,
|
||||
}
|
||||
a.IsTrue(item.Contains(iplibrary.IPBytes("192.168.1.1")))
|
||||
a.IsTrue(item.Contains(iputils.ToBytes("192.168.1.1")))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +90,7 @@ func TestIPItem_Memory(t *testing.T) {
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Type: "ip",
|
||||
Id: uint64(i),
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
IPTo: nil,
|
||||
ExpiredAt: time.Now().Unix(),
|
||||
EventLevel: "",
|
||||
@@ -108,8 +109,8 @@ func BenchmarkIPItem_Contains(b *testing.B) {
|
||||
runtime.GOMAXPROCS(1)
|
||||
|
||||
var item = &iplibrary.IPItem{
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPTo: iplibrary.IPBytes("192.168.1.101"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
IPTo: iputils.ToBytes("192.168.1.101"),
|
||||
ExpiredAt: 0,
|
||||
}
|
||||
|
||||
@@ -117,7 +118,7 @@ func BenchmarkIPItem_Contains(b *testing.B) {
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
var ip = iplibrary.IPBytes("192.168.1." + strconv.Itoa(rand.Int()%255))
|
||||
var ip = iputils.ToBytes("192.168.1." + strconv.Itoa(rand.Int()%255))
|
||||
item.Contains(ip)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package iplibrary
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"sort"
|
||||
@@ -145,7 +146,7 @@ func (this *IPList) ContainsIPStrings(ipStrings []string) (item *IPItem, found b
|
||||
if len(ipString) == 0 {
|
||||
continue
|
||||
}
|
||||
item = this.lookupIP(IPBytes(ipString))
|
||||
item = this.lookupIP(iputils.ToBytes(ipString))
|
||||
if item != nil {
|
||||
found = true
|
||||
return
|
||||
@@ -189,7 +190,7 @@ func (this *IPList) addItem(item *IPItem, lock bool, sortable bool) {
|
||||
|
||||
var shouldSort bool
|
||||
|
||||
if CompareBytes(item.IPFrom, item.IPTo) == 0 {
|
||||
if iputils.CompareBytes(item.IPFrom, item.IPTo) == 0 {
|
||||
item.IPTo = nil
|
||||
}
|
||||
|
||||
@@ -198,7 +199,7 @@ func (this *IPList) addItem(item *IPItem, lock bool, sortable bool) {
|
||||
return
|
||||
}
|
||||
} else if !IsZero(item.IPTo) {
|
||||
if CompareBytes(item.IPFrom, item.IPTo) > 0 {
|
||||
if iputils.CompareBytes(item.IPFrom, item.IPTo) > 0 {
|
||||
item.IPFrom, item.IPTo = item.IPTo, item.IPFrom
|
||||
} else if IsZero(item.IPFrom) {
|
||||
item.IPFrom = item.IPTo
|
||||
@@ -254,10 +255,10 @@ func (this *IPList) sortRangeItems(force bool) {
|
||||
sort.Slice(this.sortedRangeItems, func(i, j int) bool {
|
||||
var item1 = this.sortedRangeItems[i]
|
||||
var item2 = this.sortedRangeItems[j]
|
||||
if CompareBytes(item1.IPFrom, item2.IPFrom) == 0 {
|
||||
return CompareBytes(item1.IPTo, item2.IPTo) < 0
|
||||
if iputils.CompareBytes(item1.IPFrom, item2.IPFrom) == 0 {
|
||||
return iputils.CompareBytes(item1.IPTo, item2.IPTo) < 0
|
||||
}
|
||||
return CompareBytes(item1.IPFrom, item2.IPFrom) < 0
|
||||
return iputils.CompareBytes(item1.IPFrom, item2.IPFrom) < 0
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -279,9 +280,9 @@ func (this *IPList) lookupIP(ipBytes []byte) *IPItem {
|
||||
var resultIndex = -1
|
||||
sort.Search(count, func(i int) bool {
|
||||
var item = this.sortedRangeItems[i]
|
||||
var cmp = CompareBytes(item.IPFrom, ipBytes)
|
||||
var cmp = iputils.CompareBytes(item.IPFrom, ipBytes)
|
||||
if cmp < 0 {
|
||||
if CompareBytes(item.IPTo, ipBytes) >= 0 {
|
||||
if iputils.CompareBytes(item.IPTo, ipBytes) >= 0 {
|
||||
resultIndex = i
|
||||
}
|
||||
return false
|
||||
|
||||
@@ -2,6 +2,7 @@ package iplibrary_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||
@@ -33,24 +34,24 @@ func TestIPList_Add_One(t *testing.T) {
|
||||
var ipList = iplibrary.NewIPList()
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 1,
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
})
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 2,
|
||||
IPTo: iplibrary.IPBytes("192.168.1.2"),
|
||||
IPTo: iputils.ToBytes("192.168.1.2"),
|
||||
})
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 3,
|
||||
IPFrom: iplibrary.IPBytes("192.168.0.2"),
|
||||
IPFrom: iputils.ToBytes("192.168.0.2"),
|
||||
})
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 4,
|
||||
IPFrom: iplibrary.IPBytes("192.168.0.2"),
|
||||
IPTo: iplibrary.IPBytes("192.168.0.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.0.2"),
|
||||
IPTo: iputils.ToBytes("192.168.0.1"),
|
||||
})
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 5,
|
||||
IPFrom: iplibrary.IPBytes("2001:db8:0:1::101"),
|
||||
IPFrom: iputils.ToBytes("2001:db8:0:1::101"),
|
||||
})
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 6,
|
||||
@@ -75,7 +76,7 @@ func TestIPList_Update(t *testing.T) {
|
||||
var ipList = iplibrary.NewIPList()
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 1,
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
})
|
||||
|
||||
t.Log("===before===")
|
||||
@@ -85,12 +86,12 @@ func TestIPList_Update(t *testing.T) {
|
||||
|
||||
/**ipList.Add(&iplibrary.IPItem{
|
||||
Id: 2,
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
})**/
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 1,
|
||||
//IPFrom: 123,
|
||||
IPTo: iplibrary.IPBytes("192.168.1.2"),
|
||||
IPTo: iputils.ToBytes("192.168.1.2"),
|
||||
})
|
||||
|
||||
t.Log("===after===")
|
||||
@@ -124,17 +125,17 @@ func TestIPList_Add_Range(t *testing.T) {
|
||||
var ipList = iplibrary.NewIPList()
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 1,
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPTo: iplibrary.IPBytes("192.168.2.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
IPTo: iputils.ToBytes("192.168.2.1"),
|
||||
})
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 2,
|
||||
IPTo: iplibrary.IPBytes("192.168.1.2"),
|
||||
IPTo: iputils.ToBytes("192.168.1.2"),
|
||||
})
|
||||
ipList.Add(&iplibrary.IPItem{
|
||||
Id: 3,
|
||||
IPFrom: iplibrary.IPBytes("192.168.0.1"),
|
||||
IPTo: iplibrary.IPBytes("192.168.0.2"),
|
||||
IPFrom: iputils.ToBytes("192.168.0.1"),
|
||||
IPTo: iputils.ToBytes("192.168.0.2"),
|
||||
})
|
||||
|
||||
a.IsTrue(len(ipList.SortedRangeItems()) == 2)
|
||||
@@ -163,8 +164,8 @@ func TestNewIPList_Memory(t *testing.T) {
|
||||
for i := 0; i < count; i++ {
|
||||
list.AddDelay(&iplibrary.IPItem{
|
||||
Id: uint64(i),
|
||||
IPFrom: iplibrary.IPBytes(testutils.RandIP()),
|
||||
IPTo: iplibrary.IPBytes(testutils.RandIP()),
|
||||
IPFrom: iputils.ToBytes(testutils.RandIP()),
|
||||
IPTo: iputils.ToBytes(testutils.RandIP()),
|
||||
ExpiredAt: time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
@@ -184,25 +185,39 @@ func TestIPList_Contains(t *testing.T) {
|
||||
for i := 0; i < 255; i++ {
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: uint64(i),
|
||||
IPFrom: iplibrary.IPBytes(strconv.Itoa(i) + ".168.0.1"),
|
||||
IPTo: iplibrary.IPBytes(strconv.Itoa(i) + ".168.255.1"),
|
||||
IPFrom: iputils.ToBytes(strconv.Itoa(i) + ".168.0.1"),
|
||||
IPTo: iputils.ToBytes(strconv.Itoa(i) + ".168.255.1"),
|
||||
ExpiredAt: 0,
|
||||
})
|
||||
}
|
||||
for i := 0; i < 255; i++ {
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: uint64(1000 + i),
|
||||
IPFrom: iplibrary.IPBytes("192.167.2." + strconv.Itoa(i)),
|
||||
IPFrom: iputils.ToBytes("192.167.2." + strconv.Itoa(i)),
|
||||
})
|
||||
}
|
||||
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: 10000,
|
||||
IPFrom: iputils.ToBytes("::1"),
|
||||
})
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: 10001,
|
||||
IPFrom: iputils.ToBytes("::2"),
|
||||
IPTo: iputils.ToBytes("::5"),
|
||||
})
|
||||
|
||||
t.Log(len(list.ItemsMap()), "ip")
|
||||
|
||||
var before = time.Now()
|
||||
a.IsTrue(list.Contains(iplibrary.IPBytes("192.168.1.100")))
|
||||
a.IsTrue(list.Contains(iplibrary.IPBytes("192.168.2.100")))
|
||||
a.IsFalse(list.Contains(iplibrary.IPBytes("192.169.3.100")))
|
||||
a.IsFalse(list.Contains(iplibrary.IPBytes("192.167.3.100")))
|
||||
a.IsTrue(list.Contains(iplibrary.IPBytes("192.167.2.100")))
|
||||
a.IsTrue(list.Contains(iputils.ToBytes("192.168.1.100")))
|
||||
a.IsTrue(list.Contains(iputils.ToBytes("192.168.2.100")))
|
||||
a.IsFalse(list.Contains(iputils.ToBytes("192.169.3.100")))
|
||||
a.IsFalse(list.Contains(iputils.ToBytes("192.167.3.100")))
|
||||
a.IsTrue(list.Contains(iputils.ToBytes("192.167.2.100")))
|
||||
a.IsTrue(list.Contains(iputils.ToBytes("::1")))
|
||||
a.IsTrue(list.Contains(iputils.ToBytes("::3")))
|
||||
a.IsFalse(list.Contains(iputils.ToBytes("::8")))
|
||||
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||
}
|
||||
|
||||
@@ -211,8 +226,8 @@ func TestIPList_Contains_Many(t *testing.T) {
|
||||
for i := 0; i < 1_000_000; i++ {
|
||||
list.AddDelay(&iplibrary.IPItem{
|
||||
Id: uint64(i),
|
||||
IPFrom: iplibrary.IPBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255))),
|
||||
IPTo: iplibrary.IPBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255))),
|
||||
IPFrom: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255))),
|
||||
IPTo: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255))),
|
||||
ExpiredAt: 0,
|
||||
})
|
||||
}
|
||||
@@ -223,7 +238,7 @@ func TestIPList_Contains_Many(t *testing.T) {
|
||||
t.Log(len(list.ItemsMap()), "ip")
|
||||
|
||||
before = time.Now()
|
||||
_ = list.Contains(iplibrary.IPBytes("192.168.1.100"))
|
||||
_ = list.Contains(iputils.ToBytes("192.168.1.100"))
|
||||
t.Log("contains cost:", time.Since(before).Seconds()*1000, "ms")
|
||||
}
|
||||
|
||||
@@ -236,12 +251,12 @@ func TestIPList_ContainsAll(t *testing.T) {
|
||||
Type: "all",
|
||||
IPFrom: nil,
|
||||
})
|
||||
var b = list.Contains(iplibrary.IPBytes("192.168.1.1"))
|
||||
var b = list.Contains(iputils.ToBytes("192.168.1.1"))
|
||||
a.IsTrue(b)
|
||||
|
||||
list.Delete(1)
|
||||
|
||||
b = list.Contains(iplibrary.IPBytes("192.168.1.1"))
|
||||
b = list.Contains(iputils.ToBytes("192.168.1.1"))
|
||||
a.IsFalse(b)
|
||||
}
|
||||
|
||||
@@ -252,8 +267,8 @@ func TestIPList_ContainsIPStrings(t *testing.T) {
|
||||
for i := 0; i < 255; i++ {
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: uint64(i),
|
||||
IPFrom: iplibrary.IPBytes(strconv.Itoa(i) + ".168.0.1"),
|
||||
IPTo: iplibrary.IPBytes(strconv.Itoa(i) + ".168.255.1"),
|
||||
IPFrom: iputils.ToBytes(strconv.Itoa(i) + ".168.0.1"),
|
||||
IPTo: iputils.ToBytes(strconv.Itoa(i) + ".168.255.1"),
|
||||
ExpiredAt: 0,
|
||||
})
|
||||
}
|
||||
@@ -275,18 +290,18 @@ func TestIPList_Delete(t *testing.T) {
|
||||
var list = iplibrary.NewIPList()
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: 1,
|
||||
IPFrom: iplibrary.IPBytes("192.168.0.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.0.1"),
|
||||
ExpiredAt: 0,
|
||||
})
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: 2,
|
||||
IPFrom: iplibrary.IPBytes("192.168.0.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.0.1"),
|
||||
ExpiredAt: 0,
|
||||
})
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: 3,
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.1"),
|
||||
IPTo: iplibrary.IPBytes("192.168.2.1"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.1"),
|
||||
IPTo: iputils.ToBytes("192.168.2.1"),
|
||||
ExpiredAt: 0,
|
||||
})
|
||||
t.Log("===before===")
|
||||
@@ -338,14 +353,14 @@ func TestIPList_GC(t *testing.T) {
|
||||
var list = iplibrary.NewIPList()
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: 1,
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.100"),
|
||||
IPTo: iplibrary.IPBytes("192.168.1.101"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.100"),
|
||||
IPTo: iputils.ToBytes("192.168.1.101"),
|
||||
ExpiredAt: time.Now().Unix() + 1,
|
||||
})
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Id: 2,
|
||||
IPFrom: iplibrary.IPBytes("192.168.1.102"),
|
||||
IPTo: iplibrary.IPBytes("192.168.1.103"),
|
||||
IPFrom: iputils.ToBytes("192.168.1.102"),
|
||||
IPTo: iputils.ToBytes("192.168.1.103"),
|
||||
ExpiredAt: 0,
|
||||
})
|
||||
logs.PrintAsJSON(list.ItemsMap(), t)
|
||||
@@ -386,8 +401,8 @@ func BenchmarkIPList_Add(b *testing.B) {
|
||||
for i := 1; i < 200_000; i++ {
|
||||
list.AddDelay(&iplibrary.IPItem{
|
||||
Id: uint64(i),
|
||||
IPFrom: iplibrary.IPBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||
IPTo: iplibrary.IPBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||
IPFrom: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||
IPTo: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||
ExpiredAt: time.Now().Unix() + 60,
|
||||
})
|
||||
}
|
||||
@@ -403,7 +418,7 @@ func BenchmarkIPList_Add(b *testing.B) {
|
||||
list.Add(&iplibrary.IPItem{
|
||||
Type: "",
|
||||
Id: uint64(i % 1_000_000),
|
||||
IPFrom: iplibrary.IPBytes(ip),
|
||||
IPFrom: iputils.ToBytes(ip),
|
||||
IPTo: nil,
|
||||
ExpiredAt: fasttime.Now().Unix() + 3600,
|
||||
EventLevel: "",
|
||||
@@ -418,11 +433,11 @@ func BenchmarkIPList_Contains(b *testing.B) {
|
||||
for i := 1; i < 1_000_000; i++ {
|
||||
var item = &iplibrary.IPItem{
|
||||
Id: uint64(i),
|
||||
IPFrom: iplibrary.IPBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||
IPFrom: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||
ExpiredAt: time.Now().Unix() + 60,
|
||||
}
|
||||
if i%100 == 0 {
|
||||
item.IPTo = iplibrary.IPBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1")
|
||||
item.IPTo = iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1")
|
||||
}
|
||||
list.Add(item)
|
||||
}
|
||||
@@ -432,7 +447,7 @@ func BenchmarkIPList_Contains(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
_ = list.Contains(iplibrary.IPBytes(testutils.RandIP()))
|
||||
_ = list.Contains(iputils.ToBytes(testutils.RandIP()))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -442,12 +457,12 @@ func BenchmarkIPList_Sort(b *testing.B) {
|
||||
for i := 0; i < 1_000_000; i++ {
|
||||
var item = &iplibrary.IPItem{
|
||||
Id: uint64(i),
|
||||
IPFrom: iplibrary.IPBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||
IPFrom: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
||||
ExpiredAt: time.Now().Unix() + 60,
|
||||
}
|
||||
|
||||
if i%100 == 0 {
|
||||
item.IPTo = iplibrary.IPBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1")
|
||||
item.IPTo = iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1")
|
||||
}
|
||||
|
||||
list.AddDelay(item)
|
||||
|
||||
@@ -3,11 +3,10 @@
|
||||
package iplibrary
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"net"
|
||||
)
|
||||
|
||||
// AllowIP 检查IP是否被允许访问
|
||||
@@ -26,7 +25,7 @@ func AllowIP(ip string, serverId int64) (canGoNext bool, inAllowList bool, expir
|
||||
}
|
||||
}
|
||||
|
||||
var ipBytes = IPBytes(ip)
|
||||
var ipBytes = iputils.ToBytes(ip)
|
||||
if IsZero(ipBytes) {
|
||||
return false, false, 0
|
||||
}
|
||||
@@ -64,7 +63,7 @@ func AllowIP(ip string, serverId int64) (canGoNext bool, inAllowList bool, expir
|
||||
|
||||
// IsInWhiteList 检查IP是否在白名单中
|
||||
func IsInWhiteList(ip string) bool {
|
||||
var ipBytes = IPBytes(ip)
|
||||
var ipBytes = iputils.ToBytes(ip)
|
||||
if IsZero(ipBytes) {
|
||||
return false
|
||||
}
|
||||
@@ -91,36 +90,6 @@ func IsZero(ipBytes []byte) bool {
|
||||
return len(ipBytes) == 0
|
||||
}
|
||||
|
||||
func CompareBytes(b1 []byte, b2 []byte) int {
|
||||
var l1 = len(b1)
|
||||
var l2 = len(b2)
|
||||
if l1 < l2 {
|
||||
return -1
|
||||
}
|
||||
if l1 > l2 {
|
||||
return 1
|
||||
}
|
||||
return bytes.Compare(b1, b2)
|
||||
}
|
||||
|
||||
func IPBytes(ip string) []byte {
|
||||
if len(ip) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var i = net.ParseIP(ip)
|
||||
if i == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var i4 = i.To4()
|
||||
if i4 != nil {
|
||||
return i4
|
||||
}
|
||||
|
||||
return i.To16()
|
||||
}
|
||||
|
||||
func ToHex(b []byte) string {
|
||||
if len(b) == 0 {
|
||||
return ""
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package iplibrary
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
@@ -53,7 +54,7 @@ type IPListManager struct {
|
||||
fetchPageSize int64
|
||||
|
||||
listMap map[int64]*IPList
|
||||
locker sync.Mutex
|
||||
mu sync.RWMutex
|
||||
|
||||
isFirstTime bool
|
||||
}
|
||||
@@ -232,9 +233,10 @@ func (this *IPListManager) fetch() (hasNext bool, err error) {
|
||||
}
|
||||
|
||||
func (this *IPListManager) FindList(listId int64) *IPList {
|
||||
this.locker.Lock()
|
||||
this.mu.RLock()
|
||||
var list = this.listMap[listId]
|
||||
this.locker.Unlock()
|
||||
this.mu.RUnlock()
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
@@ -274,15 +276,15 @@ func (this *IPListManager) processItems(items []*pb.IPItem, fromRemote bool) {
|
||||
list = GlobalWhiteIPList
|
||||
}
|
||||
} else { // 其他List
|
||||
this.locker.Lock()
|
||||
this.mu.Lock()
|
||||
list = this.listMap[item.ListId]
|
||||
this.locker.Unlock()
|
||||
this.mu.Unlock()
|
||||
}
|
||||
if list == nil {
|
||||
list = NewIPList()
|
||||
this.locker.Lock()
|
||||
this.mu.Lock()
|
||||
this.listMap[item.ListId] = list
|
||||
this.locker.Unlock()
|
||||
this.mu.Unlock()
|
||||
}
|
||||
|
||||
changedLists[list] = zero.New()
|
||||
@@ -304,8 +306,8 @@ func (this *IPListManager) processItems(items []*pb.IPItem, fromRemote bool) {
|
||||
list.AddDelay(&IPItem{
|
||||
Id: uint64(item.Id),
|
||||
Type: item.Type,
|
||||
IPFrom: IPBytes(item.IpFrom),
|
||||
IPTo: IPBytes(item.IpTo),
|
||||
IPFrom: iputils.ToBytes(item.IpFrom),
|
||||
IPTo: iputils.ToBytes(item.IpTo),
|
||||
ExpiredAt: item.ExpiredAt,
|
||||
EventLevel: item.EventLevel,
|
||||
})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package iplibrary_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
@@ -32,8 +33,8 @@ func TestIPListManager_check(t *testing.T) {
|
||||
defer func() {
|
||||
t.Log(time.Since(before).Seconds()*1000, "ms")
|
||||
}()
|
||||
t.Log(iplibrary.SharedServerListManager.FindBlackList(23, true).Contains(iplibrary.IPBytes("127.0.0.2")))
|
||||
t.Log(iplibrary.GlobalBlackIPList.Contains(iplibrary.IPBytes("127.0.0.6")))
|
||||
t.Log(iplibrary.SharedServerListManager.FindBlackList(23, true).Contains(iputils.ToBytes("127.0.0.2")))
|
||||
t.Log(iplibrary.GlobalBlackIPList.Contains(iputils.ToBytes("127.0.0.6")))
|
||||
}
|
||||
|
||||
func TestIPListManager_loop(t *testing.T) {
|
||||
|
||||
@@ -112,7 +112,7 @@ func (this *HTTPRequest) checkWAFRequest(firewallPolicy *firewallconfigs.HTTPFir
|
||||
}
|
||||
for _, ref := range inbound.AllAllowListRefs() {
|
||||
if ref.IsOn && ref.ListId > 0 {
|
||||
list := iplibrary.SharedIPListManager.FindList(ref.ListId)
|
||||
var list = iplibrary.SharedIPListManager.FindList(ref.ListId)
|
||||
if list != nil {
|
||||
_, found := list.ContainsIPStrings(remoteAddrs)
|
||||
if found {
|
||||
@@ -127,7 +127,7 @@ func (this *HTTPRequest) checkWAFRequest(firewallPolicy *firewallconfigs.HTTPFir
|
||||
if isDefendMode {
|
||||
for _, ref := range inbound.AllDenyListRefs() {
|
||||
if ref.IsOn && ref.ListId > 0 {
|
||||
list := iplibrary.SharedIPListManager.FindList(ref.ListId)
|
||||
var list = iplibrary.SharedIPListManager.FindList(ref.ListId)
|
||||
if list != nil {
|
||||
item, found := list.ContainsIPStrings(remoteAddrs)
|
||||
if found {
|
||||
|
||||
Reference in New Issue
Block a user