Files
EdgeNode/internal/iplibrary/ip_list.go

275 lines
5.0 KiB
Go
Raw Normal View History

package iplibrary
import (
"github.com/TeaOSLab/EdgeNode/internal/utils"
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名单
2021-10-04 17:42:38 +08:00
// TODO IP名单可以分片关闭这样让每一片的数据量减少查询更快
type IPList struct {
2023-09-13 17:17:05 +08:00
isDeleted bool
2022-04-09 18:28:22 +08:00
itemsMap map[uint64]*IPItem // id => item
2021-10-04 17:42:38 +08:00
sortedItems []*IPItem
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
locker sync.RWMutex
}
func NewIPList() *IPList {
2021-01-03 20:18:47 +08:00
list := &IPList{
2022-04-09 18:28:22 +08:00
itemsMap: map[uint64]*IPItem{},
allItemsMap: map[uint64]*IPItem{},
}
2021-01-03 20:18:47 +08:00
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
}
2021-10-04 17:42:38 +08:00
this.addItem(item, true)
}
2021-01-03 20:18:47 +08:00
2021-10-04 17:42:38 +08:00
// AddDelay 延迟添加需要手工调用Sort()函数
func (this *IPList) AddDelay(item *IPItem) {
2023-09-13 17:17:05 +08:00
if this.isDeleted {
return
}
2021-10-04 17:42:38 +08:00
this.addItem(item, false)
}
2021-10-04 17:42:38 +08:00
func (this *IPList) Sort() {
this.locker.Lock()
2021-10-04 17:42:38 +08:00
this.sortItems()
this.locker.Unlock()
}
2022-04-09 18:28:22 +08:00
func (this *IPList) Delete(itemId uint64) {
this.locker.Lock()
2021-01-03 20:18:47 +08:00
this.deleteItem(itemId)
2021-10-04 17:42:38 +08:00
this.locker.Unlock()
}
2021-07-18 15:51:49 +08:00
// Contains 判断是否包含某个IP
func (this *IPList) Contains(ip uint64) bool {
2023-09-13 17:17:05 +08:00
if this.isDeleted {
return false
}
this.locker.RLock()
2021-10-04 17:42:38 +08:00
if len(this.allItemsMap) > 0 {
this.locker.RUnlock()
return true
}
2021-10-04 17:42:38 +08:00
var item = this.lookupIP(ip)
2021-01-03 20:18:47 +08:00
this.locker.RUnlock()
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(ip uint64) (expiresAt int64, ok bool) {
2023-09-13 17:17:05 +08:00
if this.isDeleted {
return
}
2023-03-31 21:37:15 +08:00
this.locker.RLock()
if len(this.allItemsMap) > 0 {
this.locker.RUnlock()
return 0, true
}
var item = this.lookupIP(ip)
this.locker.RUnlock()
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
}
this.locker.RLock()
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 {
this.locker.RUnlock()
found = true
return
}
}
for _, ipString := range ipStrings {
if len(ipString) == 0 {
continue
}
2021-10-04 17:42:38 +08:00
item = this.lookupIP(utils.IP2Long(ipString))
if item != nil {
this.locker.RUnlock()
2021-02-26 16:33:58 +08:00
found = true
return
}
}
this.locker.RUnlock()
2021-02-26 16:33:58 +08:00
return
}
2023-09-13 17:17:05 +08:00
func (this *IPList) SetDeleted() {
this.isDeleted = true
}
2021-10-04 17:42:38 +08:00
func (this *IPList) addItem(item *IPItem, sortable bool) {
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
2021-10-04 17:42:38 +08:00
if item.IPFrom == 0 && item.IPTo == 0 {
if item.Type != IPItemTypeAll {
return
}
2021-01-03 20:18:47 +08:00
} else if item.IPTo > 0 {
2021-10-04 17:42:38 +08:00
if item.IPFrom > item.IPTo {
item.IPFrom, item.IPTo = item.IPTo, item.IPFrom
} else if item.IPFrom == 0 {
item.IPFrom = item.IPTo
item.IPTo = 0
}
}
2021-01-03 20:18:47 +08:00
2021-10-04 17:42:38 +08:00
this.locker.Lock()
// 是否已经存在
_, 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 item.IPFrom > 0 {
this.sortedItems = append(this.sortedItems, item)
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
if sortable {
this.sortItems()
}
this.locker.Unlock()
}
2023-03-31 21:37:15 +08:00
// 对列表进行排序
2021-10-04 17:42:38 +08:00
func (this *IPList) sortItems() {
sort.Slice(this.sortedItems, func(i, j int) bool {
var item1 = this.sortedItems[i]
var item2 = this.sortedItems[j]
if item1.IPFrom == item2.IPFrom {
return item1.IPTo < item2.IPTo
}
return item1.IPFrom < item2.IPFrom
})
}
// 不加锁的情况下查找Item
func (this *IPList) lookupIP(ip uint64) *IPItem {
2022-03-17 19:48:04 +08:00
if len(this.sortedItems) == 0 {
return nil
}
2021-10-04 17:42:38 +08:00
var count = len(this.sortedItems)
var resultIndex = -1
sort.Search(count, func(i int) bool {
var item = this.sortedItems[i]
if item.IPFrom < ip {
if item.IPTo >= ip {
resultIndex = i
}
return false
} else if item.IPFrom == ip {
resultIndex = i
return false
}
return true
})
if resultIndex < 0 || resultIndex >= count {
return nil
}
return this.sortedItems[resultIndex]
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) {
2021-10-04 17:42:38 +08:00
_, ok := this.itemsMap[itemId]
2021-01-03 20:18:47 +08:00
if !ok {
return
}
2021-10-04 17:42:38 +08:00
delete(this.itemsMap, itemId)
// 是否为All Item
_, ok = this.allItemsMap[itemId]
if ok {
delete(this.allItemsMap, itemId)
return
}
// 删除排序中的Item
var index = -1
for itemIndex, item := range this.sortedItems {
if item.Id == itemId {
index = itemIndex
break
2021-01-03 20:18:47 +08:00
}
}
2021-10-04 17:42:38 +08:00
if index >= 0 {
copy(this.sortedItems[index:], this.sortedItems[index+1:])
this.sortedItems = this.sortedItems[:len(this.sortedItems)-1]
2021-01-03 20:18:47 +08:00
}
}