实现IP黑白名单、国家|地区封禁、省份封禁

This commit is contained in:
GoEdgeLab
2020-11-09 10:45:44 +08:00
parent 2b62841b35
commit 734c30ebfb
34 changed files with 1354 additions and 8 deletions

View File

@@ -0,0 +1,45 @@
package iplibrary
import (
"sync"
)
// IP名单
type IPList struct {
itemsMap map[int64]*IPItem // id => item
locker sync.RWMutex
}
func NewIPList() *IPList {
return &IPList{
itemsMap: map[int64]*IPItem{},
}
}
func (this *IPList) Add(item *IPItem) {
this.locker.Lock()
this.itemsMap[item.Id] = item
this.locker.Unlock()
}
func (this *IPList) Delete(itemId int64) {
this.locker.Lock()
delete(this.itemsMap, itemId)
this.locker.Unlock()
}
// 判断是否包含某个IP
func (this *IPList) Contains(ip uint32) bool {
// TODO 优化查询速度可能需要把items分成两组一组是单个的一组是按照范围的按照范围的再进行二分法查找
this.locker.RLock()
for _, item := range this.itemsMap {
if item.Contains(ip) {
this.locker.RUnlock()
return true
}
}
this.locker.RUnlock()
return false
}