github.com/TeaOSLab/EdgeNode@v1.3.8/internal/iplibrary/ip_item.go (about) 1 package iplibrary 2 3 import ( 4 "github.com/TeaOSLab/EdgeCommon/pkg/iputils" 5 "github.com/TeaOSLab/EdgeNode/internal/utils/fasttime" 6 ) 7 8 type IPItemType = string 9 10 const ( 11 IPItemTypeIPv4 IPItemType = "ipv4" // IPv4 12 IPItemTypeIPv6 IPItemType = "ipv6" // IPv6 13 IPItemTypeAll IPItemType = "all" // 所有IP 14 ) 15 16 // IPItem IP条目 17 type IPItem struct { 18 Type string `json:"type"` 19 Id uint64 `json:"id"` 20 IPFrom []byte `json:"ipFrom"` 21 IPTo []byte `json:"ipTo"` 22 23 ExpiredAt int64 `json:"expiredAt"` 24 EventLevel string `json:"eventLevel"` 25 } 26 27 // Contains 检查是否包含某个IP 28 func (this *IPItem) Contains(ipBytes []byte) bool { 29 switch this.Type { 30 case IPItemTypeIPv4: 31 return this.containsIP(ipBytes) 32 case IPItemTypeIPv6: 33 return this.containsIP(ipBytes) 34 case IPItemTypeAll: 35 return this.containsAll() 36 default: 37 return this.containsIP(ipBytes) 38 } 39 } 40 41 // 检查是否包含某个 42 func (this *IPItem) containsIP(ipBytes []byte) bool { 43 if IsZero(this.IPTo) { 44 if iputils.CompareBytes(this.IPFrom, ipBytes) != 0 { 45 return false 46 } 47 } else { 48 if iputils.CompareBytes(this.IPFrom, ipBytes) > 0 || iputils.CompareBytes(this.IPTo, ipBytes) < 0 { 49 return false 50 } 51 } 52 if this.ExpiredAt > 0 && this.ExpiredAt < fasttime.Now().Unix() { 53 return false 54 } 55 return true 56 } 57 58 // 检查是否包所有IP 59 func (this *IPItem) containsAll() bool { 60 if this.ExpiredAt > 0 && this.ExpiredAt < fasttime.Now().Unix() { 61 return false 62 } 63 return true 64 }