github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/route/rule_item_network.go (about)

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