2026-03-24 12:50:37 +08:00
|
|
|
package rule
|
|
|
|
|
|
2026-09-01 21:32:13 +08:00
|
|
|
import "github.com/sagernet/sing-box/adapter"
|
|
|
|
|
|
2026-03-24 12:50:37 +08:00
|
|
|
type ruleMatchState uint8
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
ruleMatchSourceAddress ruleMatchState = 1 << iota
|
|
|
|
|
ruleMatchSourcePort
|
|
|
|
|
ruleMatchDestinationAddress
|
|
|
|
|
ruleMatchDestinationPort
|
|
|
|
|
)
|
|
|
|
|
|
2026-07-23 11:09:48 +08:00
|
|
|
type ruleGroupMatch struct {
|
|
|
|
|
required ruleMatchState
|
|
|
|
|
satisfied ruleMatchState
|
2026-03-25 10:32:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-23 11:09:48 +08:00
|
|
|
func (g ruleGroupMatch) done() bool {
|
|
|
|
|
return g.required&^g.satisfied == 0
|
2026-03-24 12:50:37 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-23 11:09:48 +08:00
|
|
|
func (g ruleGroupMatch) mergeWith(other ruleGroupMatch) ruleGroupMatch {
|
|
|
|
|
return ruleGroupMatch{
|
|
|
|
|
required: g.required | other.required,
|
|
|
|
|
satisfied: g.satisfied | other.satisfied,
|
2026-03-24 12:50:37 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-01 21:32:13 +08:00
|
|
|
|
|
|
|
|
type ruleMatchSnapshot struct {
|
|
|
|
|
ipCidrMatchSource bool
|
|
|
|
|
ipCidrAcceptEmpty bool
|
|
|
|
|
deferredIPCIDRMatchGroups uint8
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func snapshotRuleMatch(metadata *adapter.InboundContext) ruleMatchSnapshot {
|
|
|
|
|
return ruleMatchSnapshot{
|
|
|
|
|
ipCidrMatchSource: metadata.IPCIDRMatchSource,
|
|
|
|
|
ipCidrAcceptEmpty: metadata.IPCIDRAcceptEmpty,
|
|
|
|
|
deferredIPCIDRMatchGroups: metadata.DeferredIPCIDRMatchGroups,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s ruleMatchSnapshot) restore(metadata *adapter.InboundContext) {
|
|
|
|
|
metadata.IPCIDRMatchSource = s.ipCidrMatchSource
|
|
|
|
|
metadata.IPCIDRAcceptEmpty = s.ipCidrAcceptEmpty
|
|
|
|
|
metadata.DeferredIPCIDRMatchGroups = s.deferredIPCIDRMatchGroups
|
|
|
|
|
}
|