mihomo/rules/common/ipsuffix.go
wwqgtxx ae7967f662
Some checks are pending
Test / test (1.20, macos-13) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-13) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, ubuntu-latest) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-13) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, ubuntu-latest) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-13) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, ubuntu-latest) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-13) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, ubuntu-latest) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run
chore: the resolve and findProcess behaviors of Logic and SubRules follow the order and needs of the internal rules
2025-06-10 20:11:50 +08:00

80 lines
1.5 KiB
Go

package common
import (
C "github.com/metacubex/mihomo/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, helper C.RuleMatchHelper) (bool, string) {
if !is.noResolveIP && !is.isSourceIP && helper.ResolveIP != nil {
helper.ResolveIP()
}
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 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
}