2020-11-09 10:45:44 +08:00
|
|
|
package iplibrary
|
|
|
|
|
|
|
|
|
|
import "github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
|
|
|
|
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-01-03 20:18:47 +08:00
|
|
|
// IP条目
|
2020-11-09 10:45:44 +08:00
|
|
|
type IPItem struct {
|
2021-02-02 15:26:00 +08:00
|
|
|
Type string
|
2020-11-09 10:45:44 +08:00
|
|
|
Id int64
|
2021-02-02 15:26:00 +08:00
|
|
|
IPFrom uint64
|
|
|
|
|
IPTo uint64
|
2020-11-09 10:45:44 +08:00
|
|
|
ExpiredAt int64
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 20:18:47 +08:00
|
|
|
// 检查是否包含某个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:
|
|
|
|
|
return this.containsAll(ip)
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if this.ExpiredAt > 0 && this.ExpiredAt < utils.UnixTime() {
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
if this.ExpiredAt > 0 && this.ExpiredAt < utils.UnixTime() {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查是否包所有IP
|
|
|
|
|
func (this *IPItem) containsAll(ip uint64) bool {
|
|
|
|
|
if this.ExpiredAt > 0 && this.ExpiredAt < utils.UnixTime() {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|