mihomo/rules/ipcidr.go

43 lines
624 B
Go
Raw Normal View History

2018-06-10 22:50:03 +08:00
package rules
import (
"net"
C "github.com/Dreamacro/clash/constant"
)
type IPCIDR struct {
ipnet *net.IPNet
adapter string
}
func (i *IPCIDR) RuleType() C.RuleType {
return C.IPCIDR
}
2018-09-30 12:25:52 +08:00
func (i *IPCIDR) IsMatch(metadata *C.Metadata) bool {
if metadata.IP == nil {
2018-06-10 22:50:03 +08:00
return false
}
2018-09-30 12:25:52 +08:00
return i.ipnet.Contains(*metadata.IP)
2018-06-10 22:50:03 +08:00
}
2018-06-20 22:41:02 +08:00
func (i *IPCIDR) Adapter() string {
return i.adapter
}
func (i *IPCIDR) Payload() string {
return i.ipnet.String()
2018-06-10 22:50:03 +08:00
}
func NewIPCIDR(s string, adapter string) *IPCIDR {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
}
return &IPCIDR{
ipnet: ipnet,
adapter: adapter,
}
}