2022-01-22 14:10:45 +00:00
|
|
|
package common
|
2018-06-10 14:50:03 +00:00
|
|
|
|
|
|
|
import (
|
2022-04-19 17:52:51 +00:00
|
|
|
"net/netip"
|
2018-06-10 14:50:03 +00:00
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
2019-10-27 16:02:23 +00:00
|
|
|
type IPCIDROption func(*IPCIDR)
|
|
|
|
|
|
|
|
func WithIPCIDRSourceIP(b bool) IPCIDROption {
|
|
|
|
return func(i *IPCIDR) {
|
|
|
|
i.isSourceIP = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithIPCIDRNoResolve(noResolve bool) IPCIDROption {
|
|
|
|
return func(i *IPCIDR) {
|
2019-11-04 02:42:39 +00:00
|
|
|
i.noResolveIP = noResolve
|
2019-10-27 16:02:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-10 14:50:03 +00:00
|
|
|
type IPCIDR struct {
|
2022-03-12 17:21:23 +00:00
|
|
|
*Base
|
2022-04-19 17:52:51 +00:00
|
|
|
ipnet *netip.Prefix
|
2019-10-27 16:02:23 +00:00
|
|
|
adapter string
|
|
|
|
isSourceIP bool
|
|
|
|
noResolveIP bool
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IPCIDR) RuleType() C.RuleType {
|
2019-02-02 13:03:13 +00:00
|
|
|
if i.isSourceIP {
|
2019-05-09 13:00:29 +00:00
|
|
|
return C.SrcIPCIDR
|
2019-02-02 13:03:13 +00:00
|
|
|
}
|
2018-06-10 14:50:03 +00:00
|
|
|
return C.IPCIDR
|
|
|
|
}
|
|
|
|
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 09:30:35 +00:00
|
|
|
func (i *IPCIDR) Match(metadata *C.Metadata) (bool, string) {
|
2019-05-09 13:00:29 +00:00
|
|
|
ip := metadata.DstIP
|
2019-02-02 13:03:13 +00:00
|
|
|
if i.isSourceIP {
|
2019-05-09 13:00:29 +00:00
|
|
|
ip = metadata.SrcIP
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 09:30:35 +00:00
|
|
|
return ip.IsValid() && i.ipnet.Contains(ip), i.adapter
|
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
|
|
|
}
|
|
|
|
|
2020-07-27 03:57:55 +00:00
|
|
|
func (i *IPCIDR) ShouldResolveIP() bool {
|
|
|
|
return !i.noResolveIP
|
2019-10-27 16:02:23 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 17:21:23 +00:00
|
|
|
func NewIPCIDR(s string, adapter string, opts ...IPCIDROption) (*IPCIDR, error) {
|
2022-04-19 17:52:51 +00:00
|
|
|
ipnet, err := netip.ParsePrefix(s)
|
2018-06-10 14:50:03 +00:00
|
|
|
if err != nil {
|
2019-10-27 16:02:23 +00:00
|
|
|
return nil, errPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
ipcidr := &IPCIDR{
|
2022-03-12 17:21:23 +00:00
|
|
|
Base: &Base{},
|
2022-04-19 17:52:51 +00:00
|
|
|
ipnet: &ipnet,
|
2022-03-12 17:21:23 +00:00
|
|
|
adapter: adapter,
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2019-10-27 16:02:23 +00:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(ipcidr)
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2019-10-27 16:02:23 +00:00
|
|
|
|
|
|
|
return ipcidr, nil
|
2018-06-10 14:50:03 +00:00
|
|
|
}
|
2022-03-12 17:21:23 +00:00
|
|
|
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 09:30:35 +00:00
|
|
|
//var _ C.Rule = (*IPCIDR)(nil)
|