2020-11-09 10:45:44 +08:00
|
|
|
package iplibrary
|
|
|
|
|
|
2023-04-08 12:47:04 +08:00
|
|
|
import (
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
|
|
|
|
)
|
2020-11-09 10:45:44 +08:00
|
|
|
|
2021-02-02 15:26:00 +08:00
|
|
|
type IPItemType = string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
IPItemTypeIPv4 IPItemType = "ipv4" // IPv4
|
|
|
|
|
IPItemTypeIPv6 IPItemType = "ipv6" // IPv6
|
|
|
|
|
IPItemTypeAll IPItemType = "all" // 所有IP
|
|
|
|
|
)
|
|
|
|
|
|
2021-10-04 17:42:38 +08:00
|
|
|
// IPItem IP条目
|
2020-11-09 10:45:44 +08:00
|
|
|
type IPItem struct {
|
2021-02-06 17:34:33 +08:00
|
|
|
Type string `json:"type"`
|
2022-04-09 18:28:22 +08:00
|
|
|
Id uint64 `json:"id"`
|
2021-02-06 17:34:33 +08:00
|
|
|
IPFrom uint64 `json:"ipFrom"`
|
|
|
|
|
IPTo uint64 `json:"ipTo"`
|
|
|
|
|
ExpiredAt int64 `json:"expiredAt"`
|
|
|
|
|
EventLevel string `json:"eventLevel"`
|
2020-11-09 10:45:44 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-04 17:42:38 +08:00
|
|
|
// Contains 检查是否包含某个IP
|
2021-02-02 15:26:00 +08:00
|
|
|
func (this *IPItem) Contains(ip uint64) bool {
|
|
|
|
|
switch this.Type {
|
|
|
|
|
case IPItemTypeIPv4:
|
|
|
|
|
return this.containsIPv4(ip)
|
|
|
|
|
case IPItemTypeIPv6:
|
|
|
|
|
return this.containsIPv6(ip)
|
|
|
|
|
case IPItemTypeAll:
|
2021-11-14 20:46:08 +08:00
|
|
|
return this.containsAll()
|
2021-02-02 15:26:00 +08:00
|
|
|
default:
|
|
|
|
|
return this.containsIPv4(ip)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否包含某个IPv4
|
|
|
|
|
func (this *IPItem) containsIPv4(ip uint64) bool {
|
2020-11-09 10:45:44 +08:00
|
|
|
if this.IPTo == 0 {
|
|
|
|
|
if this.IPFrom != ip {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if this.IPFrom > ip || this.IPTo < ip {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-08 12:47:04 +08:00
|
|
|
if this.ExpiredAt > 0 && this.ExpiredAt < fasttime.Now().Unix() {
|
2020-11-09 10:45:44 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
2021-02-02 15:26:00 +08:00
|
|
|
|
|
|
|
|
// 检查是否包含某个IPv6
|
|
|
|
|
func (this *IPItem) containsIPv6(ip uint64) bool {
|
|
|
|
|
if this.IPFrom != ip {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2023-04-08 12:47:04 +08:00
|
|
|
if this.ExpiredAt > 0 && this.ExpiredAt < fasttime.Now().Unix() {
|
2021-02-02 15:26:00 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否包所有IP
|
2021-11-14 20:46:08 +08:00
|
|
|
func (this *IPItem) containsAll() bool {
|
2023-04-08 12:47:04 +08:00
|
|
|
if this.ExpiredAt > 0 && this.ExpiredAt < fasttime.Now().Unix() {
|
2021-02-02 15:26:00 +08:00
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|