2018-06-10 22:50:03 +08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IPCIDR struct {
|
2019-02-02 21:03:13 +08:00
|
|
|
ipnet *net.IPNet
|
|
|
|
adapter string
|
|
|
|
isSourceIP bool
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IPCIDR) RuleType() C.RuleType {
|
2019-02-02 21:03:13 +08:00
|
|
|
if i.isSourceIP {
|
2019-05-09 21:00:29 +08:00
|
|
|
return C.SrcIPCIDR
|
2019-02-02 21:03:13 +08:00
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
return C.IPCIDR
|
|
|
|
}
|
|
|
|
|
2018-09-30 12:25:52 +08:00
|
|
|
func (i *IPCIDR) IsMatch(metadata *C.Metadata) bool {
|
2019-05-09 21:00:29 +08:00
|
|
|
ip := metadata.DstIP
|
2019-02-02 21:03:13 +08:00
|
|
|
if i.isSourceIP {
|
2019-05-09 21:00:29 +08:00
|
|
|
ip = metadata.SrcIP
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2019-02-12 12:29:33 +08:00
|
|
|
return ip != nil && i.ipnet.Contains(*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
|
|
|
}
|
|
|
|
|
2019-02-02 21:03:13 +08:00
|
|
|
func NewIPCIDR(s string, adapter string, isSourceIP bool) *IPCIDR {
|
2018-06-10 22:50:03 +08:00
|
|
|
_, ipnet, err := net.ParseCIDR(s)
|
|
|
|
if err != nil {
|
2019-03-30 14:11:59 +08:00
|
|
|
return nil
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
return &IPCIDR{
|
2019-02-02 21:03:13 +08:00
|
|
|
ipnet: ipnet,
|
|
|
|
adapter: adapter,
|
|
|
|
isSourceIP: isSourceIP,
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
}
|