Files
EdgeNode/internal/iplibrary/ip_list.go

352 lines
6.8 KiB
Go
Raw Normal View History

package iplibrary
import (
2024-04-06 15:37:14 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
2021-01-03 20:18:47 +08:00
"github.com/TeaOSLab/EdgeNode/internal/utils/expires"
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
2021-10-04 17:42:38 +08:00
"sort"
"sync"
)
var GlobalBlackIPList = NewIPList()
var GlobalWhiteIPList = NewIPList()
2021-07-18 15:51:49 +08:00
// IPList IP名单
2024-03-30 14:42:56 +08:00
// TODO 对ipMap进行分区
type IPList struct {
2023-09-13 17:17:05 +08:00
isDeleted bool
2024-03-30 14:42:56 +08:00
itemsMap map[uint64]*IPItem // id => item
sortedRangeItems []*IPItem
ipMap map[string]*IPItem // ipFrom => IPItem
bufferItemsMap map[uint64]*IPItem // id => IPItem
2024-03-30 14:42:56 +08:00
2022-04-09 18:28:22 +08:00
allItemsMap map[uint64]*IPItem // id => item
2021-10-04 17:42:38 +08:00
expireList *expires.List
2024-03-30 14:42:56 +08:00
mu sync.RWMutex
}
func NewIPList() *IPList {
2024-03-30 14:42:56 +08:00
var list = &IPList{
itemsMap: map[uint64]*IPItem{},
bufferItemsMap: map[uint64]*IPItem{},
allItemsMap: map[uint64]*IPItem{},
ipMap: map[string]*IPItem{},
}
2021-01-03 20:18:47 +08:00
2024-03-30 14:42:56 +08:00
var expireList = expires.NewList()
2022-04-09 18:28:22 +08:00
expireList.OnGC(func(itemId uint64) {
list.Delete(itemId)
})
2021-01-03 20:18:47 +08:00
list.expireList = expireList
return list
}
func (this *IPList) Add(item *IPItem) {
2023-09-13 17:17:05 +08:00
if this.isDeleted {
return
}
2024-03-30 14:42:56 +08:00
this.addItem(item, true, true)
2021-10-04 17:42:38 +08:00
}
2021-01-03 20:18:47 +08:00
2021-10-04 17:42:38 +08:00
func (this *IPList) AddDelay(item *IPItem) {
2024-03-30 14:42:56 +08:00
if this.isDeleted || item == nil {
2023-09-13 17:17:05 +08:00
return
}
if !IsZero(item.IPTo) {
2024-03-30 14:42:56 +08:00
this.mu.Lock()
this.bufferItemsMap[item.Id] = item
this.mu.Unlock()
} else {
this.addItem(item, true, true)
}
2021-10-04 17:42:38 +08:00
}
2021-10-04 17:42:38 +08:00
func (this *IPList) Sort() {
2024-03-30 14:42:56 +08:00
this.mu.Lock()
this.sortRangeItems(false)
this.mu.Unlock()
}
2022-04-09 18:28:22 +08:00
func (this *IPList) Delete(itemId uint64) {
2024-03-30 14:42:56 +08:00
this.mu.Lock()
2021-01-03 20:18:47 +08:00
this.deleteItem(itemId)
2024-03-30 14:42:56 +08:00
this.mu.Unlock()
}
2021-07-18 15:51:49 +08:00
// Contains 判断是否包含某个IP
func (this *IPList) Contains(ipBytes []byte) bool {
2023-09-13 17:17:05 +08:00
if this.isDeleted {
return false
}
2024-03-30 14:42:56 +08:00
this.mu.RLock()
defer this.mu.RUnlock()
2021-10-04 17:42:38 +08:00
if len(this.allItemsMap) > 0 {
return true
}
2021-10-04 17:42:38 +08:00
var item = this.lookupIP(ipBytes)
2021-10-04 17:42:38 +08:00
return item != nil
2021-01-03 20:18:47 +08:00
}
2023-03-31 21:37:15 +08:00
// ContainsExpires 判断是否包含某个IP
func (this *IPList) ContainsExpires(ipBytes []byte) (expiresAt int64, ok bool) {
2023-09-13 17:17:05 +08:00
if this.isDeleted {
return
}
2024-03-30 14:42:56 +08:00
this.mu.RLock()
defer this.mu.RUnlock()
2023-03-31 21:37:15 +08:00
if len(this.allItemsMap) > 0 {
return 0, true
}
var item = this.lookupIP(ipBytes)
2023-03-31 21:37:15 +08:00
if item == nil {
return
}
return item.ExpiredAt, true
}
2021-10-04 17:42:38 +08:00
// ContainsIPStrings 是否包含一组IP中的任意一个并返回匹配的第一个Item
func (this *IPList) ContainsIPStrings(ipStrings []string) (item *IPItem, found bool) {
2023-09-13 17:17:05 +08:00
if this.isDeleted {
return
}
if len(ipStrings) == 0 {
2021-02-26 16:33:58 +08:00
return
}
2024-03-30 14:42:56 +08:00
this.mu.RLock()
defer this.mu.RUnlock()
2021-10-04 17:42:38 +08:00
if len(this.allItemsMap) > 0 {
for _, allItem := range this.allItemsMap {
item = allItem
break
2021-02-26 16:33:58 +08:00
}
2021-10-04 17:42:38 +08:00
if item != nil {
found = true
return
}
}
for _, ipString := range ipStrings {
if len(ipString) == 0 {
continue
}
2024-04-06 15:37:14 +08:00
item = this.lookupIP(iputils.ToBytes(ipString))
2021-10-04 17:42:38 +08:00
if item != nil {
2021-02-26 16:33:58 +08:00
found = true
return
}
}
2021-02-26 16:33:58 +08:00
return
}
2023-09-13 17:17:05 +08:00
func (this *IPList) SetDeleted() {
this.isDeleted = true
}
2024-03-30 14:42:56 +08:00
func (this *IPList) SortedRangeItems() []*IPItem {
return this.sortedRangeItems
}
func (this *IPList) IPMap() map[string]*IPItem {
2024-03-30 14:42:56 +08:00
return this.ipMap
}
func (this *IPList) ItemsMap() map[uint64]*IPItem {
return this.itemsMap
}
func (this *IPList) AllItemsMap() map[uint64]*IPItem {
return this.allItemsMap
}
func (this *IPList) BufferItemsMap() map[uint64]*IPItem {
return this.bufferItemsMap
}
2024-03-30 14:42:56 +08:00
func (this *IPList) addItem(item *IPItem, lock bool, sortable bool) {
2021-10-04 17:42:38 +08:00
if item == nil {
2021-01-03 20:18:47 +08:00
return
}
if item.ExpiredAt > 0 && item.ExpiredAt < fasttime.Now().Unix() {
2021-10-04 17:42:38 +08:00
return
}
2021-01-03 20:18:47 +08:00
2024-03-30 14:42:56 +08:00
var shouldSort bool
2024-04-06 15:37:14 +08:00
if iputils.CompareBytes(item.IPFrom, item.IPTo) == 0 {
item.IPTo = nil
2024-03-30 14:42:56 +08:00
}
if IsZero(item.IPFrom) && IsZero(item.IPTo) {
2021-10-04 17:42:38 +08:00
if item.Type != IPItemTypeAll {
return
}
} else if !IsZero(item.IPTo) {
2024-04-06 15:37:14 +08:00
if iputils.CompareBytes(item.IPFrom, item.IPTo) > 0 {
2021-10-04 17:42:38 +08:00
item.IPFrom, item.IPTo = item.IPTo, item.IPFrom
} else if IsZero(item.IPFrom) {
2021-10-04 17:42:38 +08:00
item.IPFrom = item.IPTo
item.IPTo = nil
2021-10-04 17:42:38 +08:00
}
}
2021-01-03 20:18:47 +08:00
2024-03-30 14:42:56 +08:00
if lock {
this.mu.Lock()
defer this.mu.Unlock()
}
2021-10-04 17:42:38 +08:00
// 是否已经存在
_, ok := this.itemsMap[item.Id]
2021-01-03 20:18:47 +08:00
if ok {
2021-10-04 17:42:38 +08:00
this.deleteItem(item.Id)
}
this.itemsMap[item.Id] = item
// 展开
if !IsZero(item.IPFrom) {
if !IsZero(item.IPTo) {
2024-03-30 14:42:56 +08:00
this.sortedRangeItems = append(this.sortedRangeItems, item)
shouldSort = true
} else {
this.ipMap[ToHex(item.IPFrom)] = item
2024-03-30 14:42:56 +08:00
}
2021-01-03 20:18:47 +08:00
} else {
2021-10-04 17:42:38 +08:00
this.allItemsMap[item.Id] = item
}
if item.ExpiredAt > 0 {
this.expireList.Add(item.Id, item.ExpiredAt)
2021-01-03 20:18:47 +08:00
}
2021-10-04 17:42:38 +08:00
2024-03-30 14:42:56 +08:00
if shouldSort && sortable {
this.sortRangeItems(true)
2021-10-04 17:42:38 +08:00
}
}
2023-03-31 21:37:15 +08:00
// 对列表进行排序
2024-03-30 14:42:56 +08:00
func (this *IPList) sortRangeItems(force bool) {
if len(this.bufferItemsMap) > 0 {
for _, item := range this.bufferItemsMap {
this.addItem(item, false, false)
2021-10-04 17:42:38 +08:00
}
2024-03-30 14:42:56 +08:00
this.bufferItemsMap = map[uint64]*IPItem{}
force = true
}
if force {
sort.Slice(this.sortedRangeItems, func(i, j int) bool {
var item1 = this.sortedRangeItems[i]
var item2 = this.sortedRangeItems[j]
2024-04-06 15:37:14 +08:00
if iputils.CompareBytes(item1.IPFrom, item2.IPFrom) == 0 {
return iputils.CompareBytes(item1.IPTo, item2.IPTo) < 0
2024-03-30 14:42:56 +08:00
}
2024-04-06 15:37:14 +08:00
return iputils.CompareBytes(item1.IPFrom, item2.IPFrom) < 0
2024-03-30 14:42:56 +08:00
})
}
2021-10-04 17:42:38 +08:00
}
// 不加锁的情况下查找Item
func (this *IPList) lookupIP(ipBytes []byte) *IPItem {
2024-03-30 14:42:56 +08:00
{
item, ok := this.ipMap[ToHex(ipBytes)]
if ok && (item.ExpiredAt == 0 || item.ExpiredAt > fasttime.Now().Unix()) {
2024-03-30 14:42:56 +08:00
return item
}
}
if len(this.sortedRangeItems) == 0 {
2022-03-17 19:48:04 +08:00
return nil
}
2024-03-30 14:42:56 +08:00
var count = len(this.sortedRangeItems)
2021-10-04 17:42:38 +08:00
var resultIndex = -1
sort.Search(count, func(i int) bool {
2024-03-30 14:42:56 +08:00
var item = this.sortedRangeItems[i]
2024-04-06 15:37:14 +08:00
var cmp = iputils.CompareBytes(item.IPFrom, ipBytes)
if cmp < 0 {
2024-04-06 15:37:14 +08:00
if iputils.CompareBytes(item.IPTo, ipBytes) >= 0 {
2021-10-04 17:42:38 +08:00
resultIndex = i
}
return false
} else if cmp == 0 {
2021-10-04 17:42:38 +08:00
resultIndex = i
return false
}
return true
})
if resultIndex < 0 || resultIndex >= count {
return nil
}
var item = this.sortedRangeItems[resultIndex]
if item.ExpiredAt == 0 || item.ExpiredAt > fasttime.Now().Unix() {
return item
}
return nil
2021-01-03 20:18:47 +08:00
}
2021-10-04 17:42:38 +08:00
// 在不加锁的情况下删除某个Item
// 将会被别的方法引用,切记不能加锁
2022-04-09 18:28:22 +08:00
func (this *IPList) deleteItem(itemId uint64) {
2024-03-30 14:42:56 +08:00
// 从buffer中删除
delete(this.bufferItemsMap, itemId)
// 检查是否存在
oldItem, existsOld := this.itemsMap[itemId]
if !existsOld {
2021-01-03 20:18:47 +08:00
return
}
2021-10-04 17:42:38 +08:00
2024-03-30 14:42:56 +08:00
// 从ipMap中删除
if IsZero(oldItem.IPTo) {
var ipHex = ToHex(oldItem.IPFrom)
ipItem, ok := this.ipMap[ipHex]
2024-03-30 14:42:56 +08:00
if ok && ipItem.Id == itemId {
delete(this.ipMap, ipHex)
2024-03-30 14:42:56 +08:00
}
}
2021-10-04 17:42:38 +08:00
delete(this.itemsMap, itemId)
// 是否为All Item
2024-03-30 14:42:56 +08:00
_, ok := this.allItemsMap[itemId]
2021-10-04 17:42:38 +08:00
if ok {
delete(this.allItemsMap, itemId)
return
}
// 删除排序中的Item
if !IsZero(oldItem.IPTo) {
2024-03-30 14:42:56 +08:00
var index = -1
for itemIndex, item := range this.sortedRangeItems {
if item.Id == itemId {
index = itemIndex
break
}
}
if index >= 0 {
copy(this.sortedRangeItems[index:], this.sortedRangeItems[index+1:])
this.sortedRangeItems = this.sortedRangeItems[:len(this.sortedRangeItems)-1]
2021-01-03 20:18:47 +08:00
}
}
}