github.com/sagernet/sing-box@v1.9.0-rc.20/route/rule_item_protocol.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 = (*ProtocolItem)(nil)
    11  
    12  type ProtocolItem struct {
    13  	protocols   []string
    14  	protocolMap map[string]bool
    15  }
    16  
    17  func NewProtocolItem(protocols []string) *ProtocolItem {
    18  	protocolMap := make(map[string]bool)
    19  	for _, protocol := range protocols {
    20  		protocolMap[protocol] = true
    21  	}
    22  	return &ProtocolItem{
    23  		protocols:   protocols,
    24  		protocolMap: protocolMap,
    25  	}
    26  }
    27  
    28  func (r *ProtocolItem) Match(metadata *adapter.InboundContext) bool {
    29  	return r.protocolMap[metadata.Protocol]
    30  }
    31  
    32  func (r *ProtocolItem) String() string {
    33  	if len(r.protocols) == 1 {
    34  		return F.ToString("protocol=", r.protocols[0])
    35  	}
    36  	return F.ToString("protocol=[", strings.Join(r.protocols, " "), "]")
    37  }