clash/rules/ipcidr.go

49 lines
777 B
Go
Raw Normal View History

2018-06-10 14:50:03 +00:00
package rules
import (
"net"
C "github.com/Dreamacro/clash/constant"
)
type IPCIDR struct {
ipnet *net.IPNet
adapter string
isSourceIP bool
2018-06-10 14:50:03 +00:00
}
func (i *IPCIDR) RuleType() C.RuleType {
if i.isSourceIP {
2019-05-09 13:00:29 +00:00
return C.SrcIPCIDR
}
2018-06-10 14:50:03 +00:00
return C.IPCIDR
}
2018-09-30 04:25:52 +00:00
func (i *IPCIDR) IsMatch(metadata *C.Metadata) bool {
2019-05-09 13:00:29 +00:00
ip := metadata.DstIP
if i.isSourceIP {
2019-05-09 13:00:29 +00:00
ip = metadata.SrcIP
2018-06-10 14:50:03 +00:00
}
return ip != nil && i.ipnet.Contains(ip)
2018-06-10 14:50:03 +00:00
}
2018-06-20 14:41:02 +00:00
func (i *IPCIDR) Adapter() string {
return i.adapter
}
func (i *IPCIDR) Payload() string {
return i.ipnet.String()
2018-06-10 14:50:03 +00:00
}
func NewIPCIDR(s string, adapter string, isSourceIP bool) *IPCIDR {
2018-06-10 14:50:03 +00:00
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
2019-03-30 06:11:59 +00:00
return nil
2018-06-10 14:50:03 +00:00
}
return &IPCIDR{
ipnet: ipnet,
adapter: adapter,
isSourceIP: isSourceIP,
2018-06-10 14:50:03 +00:00
}
}