github.com/sagernet/sing-box@v1.9.0-rc.20/route/rule_item_outbound.go (about)

     1  package route
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/sagernet/sing-box/adapter"
     7  	F "github.com/sagernet/sing/common/format"
     8  )
     9  
    10  var _ RuleItem = (*OutboundItem)(nil)
    11  
    12  type OutboundItem struct {
    13  	outbounds   []string
    14  	outboundMap map[string]bool
    15  	matchAny    bool
    16  }
    17  
    18  func NewOutboundRule(outbounds []string) *OutboundItem {
    19  	rule := &OutboundItem{outbounds: outbounds, outboundMap: make(map[string]bool)}
    20  	for _, outbound := range outbounds {
    21  		if outbound == "any" {
    22  			rule.matchAny = true
    23  		} else {
    24  			rule.outboundMap[outbound] = true
    25  		}
    26  	}
    27  	return rule
    28  }
    29  
    30  func (r *OutboundItem) Match(metadata *adapter.InboundContext) bool {
    31  	if r.matchAny && metadata.Outbound != "" {
    32  		return true
    33  	}
    34  	return r.outboundMap[metadata.Outbound]
    35  }
    36  
    37  func (r *OutboundItem) String() string {
    38  	if len(r.outbounds) == 1 {
    39  		return F.ToString("outbound=", r.outbounds[0])
    40  	} else {
    41  		return F.ToString("outbound=[", strings.Join(r.outbounds, " "), "]")
    42  	}
    43  }