github.com/sagernet/sing-box@v1.9.0-rc.20/route/rule_item_rule_set.go (about) 1 package route 2 3 import ( 4 "strings" 5 6 "github.com/sagernet/sing-box/adapter" 7 "github.com/sagernet/sing/common" 8 E "github.com/sagernet/sing/common/exceptions" 9 F "github.com/sagernet/sing/common/format" 10 ) 11 12 var _ RuleItem = (*RuleSetItem)(nil) 13 14 type RuleSetItem struct { 15 router adapter.Router 16 tagList []string 17 setList []adapter.RuleSet 18 ipcidrMatchSource bool 19 } 20 21 func NewRuleSetItem(router adapter.Router, tagList []string, ipCIDRMatchSource bool) *RuleSetItem { 22 return &RuleSetItem{ 23 router: router, 24 tagList: tagList, 25 ipcidrMatchSource: ipCIDRMatchSource, 26 } 27 } 28 29 func (r *RuleSetItem) Start() error { 30 for _, tag := range r.tagList { 31 ruleSet, loaded := r.router.RuleSet(tag) 32 if !loaded { 33 return E.New("rule-set not found: ", tag) 34 } 35 r.setList = append(r.setList, ruleSet) 36 } 37 return nil 38 } 39 40 func (r *RuleSetItem) Match(metadata *adapter.InboundContext) bool { 41 metadata.IPCIDRMatchSource = r.ipcidrMatchSource 42 for _, ruleSet := range r.setList { 43 if ruleSet.Match(metadata) { 44 return true 45 } 46 } 47 return false 48 } 49 50 func (r *RuleSetItem) ContainsDestinationIPCIDRRule() bool { 51 if r.ipcidrMatchSource { 52 return false 53 } 54 return common.Any(r.setList, func(ruleSet adapter.RuleSet) bool { 55 return ruleSet.Metadata().ContainsIPCIDRRule 56 }) 57 } 58 59 func (r *RuleSetItem) String() string { 60 if len(r.tagList) == 1 { 61 return F.ToString("rule_set=", r.tagList[0]) 62 } else { 63 return F.ToString("rule_set=[", strings.Join(r.tagList, " "), "]") 64 } 65 }