mihomo/rules/common/ipsuffix.go
gVisor bot d6dc5ba19c 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 17:30:35 +08:00

80 lines
1.4 KiB
Go

package common
import (
C "github.com/Dreamacro/clash/constant"
"net/netip"
)
type IPSuffix struct {
*Base
ipBytes []byte
bits int
payload string
adapter string
isSourceIP bool
noResolveIP bool
}
func (is *IPSuffix) RuleType() C.RuleType {
if is.isSourceIP {
return C.SrcIPSuffix
}
return C.IPSuffix
}
func (is *IPSuffix) Match(metadata *C.Metadata) (bool, string) {
ip := metadata.DstIP
if is.isSourceIP {
ip = metadata.SrcIP
}
mIPBytes := ip.AsSlice()
if len(is.ipBytes) != len(mIPBytes) {
return false, ""
}
size := len(mIPBytes)
bits := is.bits
for i := bits / 8; i > 0; i-- {
if is.ipBytes[size-i] != mIPBytes[size-i] {
return false, ""
}
}
if (is.ipBytes[size-bits/8-1] << (8 - bits%8)) != (mIPBytes[size-bits/8-1] << (8 - bits%8)) {
return false, ""
}
return true, is.adapter
}
func (is *IPSuffix) Adapter() string {
return is.adapter
}
func (is *IPSuffix) Payload() string {
return is.payload
}
func (is *IPSuffix) ShouldResolveIP() bool {
return !is.noResolveIP
}
func NewIPSuffix(payload, adapter string, isSrc, noResolveIP bool) (*IPSuffix, error) {
ipnet, err := netip.ParsePrefix(payload)
if err != nil {
return nil, errPayload
}
return &IPSuffix{
Base: &Base{},
payload: payload,
ipBytes: ipnet.Addr().AsSlice(),
bits: ipnet.Bits(),
adapter: adapter,
isSourceIP: isSrc,
noResolveIP: noResolveIP,
}, nil
}