Feature: SOURCE-IP-CIDR rule type (#96)

This commit is contained in:
宋辰文
2019-02-02 21:03:13 +08:00
committed by Dreamacro
parent bfe51e46b0
commit 42d33fe629
10 changed files with 39 additions and 15 deletions

View File

@ -7,20 +7,24 @@ import (
)
type IPCIDR struct {
ipnet *net.IPNet
adapter string
ipnet *net.IPNet
adapter string
isSourceIP bool
}
func (i *IPCIDR) RuleType() C.RuleType {
if i.isSourceIP {
return C.SourceIPCIDR
}
return C.IPCIDR
}
func (i *IPCIDR) IsMatch(metadata *C.Metadata) bool {
if metadata.IP == nil {
return false
ip := metadata.IP
if i.isSourceIP {
ip = metadata.SourceIP
}
return i.ipnet.Contains(*metadata.IP)
return i.ipnet.Contains(*ip)
}
func (i *IPCIDR) Adapter() string {
@ -31,12 +35,13 @@ func (i *IPCIDR) Payload() string {
return i.ipnet.String()
}
func NewIPCIDR(s string, adapter string) *IPCIDR {
func NewIPCIDR(s string, adapter string, isSourceIP bool) *IPCIDR {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
}
return &IPCIDR{
ipnet: ipnet,
adapter: adapter,
ipnet: ipnet,
adapter: adapter,
isSourceIP: isSourceIP,
}
}