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

     1  package route
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/inazumav/sing-box/adapter"
     7  )
     8  
     9  var _ RuleItem = (*PackageNameItem)(nil)
    10  
    11  type PackageNameItem struct {
    12  	packageNames []string
    13  	packageMap   map[string]bool
    14  }
    15  
    16  func NewPackageNameItem(packageNameList []string) *PackageNameItem {
    17  	rule := &PackageNameItem{
    18  		packageNames: packageNameList,
    19  		packageMap:   make(map[string]bool),
    20  	}
    21  	for _, packageName := range packageNameList {
    22  		rule.packageMap[packageName] = true
    23  	}
    24  	return rule
    25  }
    26  
    27  func (r *PackageNameItem) Match(metadata *adapter.InboundContext) bool {
    28  	if metadata.ProcessInfo == nil || metadata.ProcessInfo.PackageName == "" {
    29  		return false
    30  	}
    31  	return r.packageMap[metadata.ProcessInfo.PackageName]
    32  }
    33  
    34  func (r *PackageNameItem) String() string {
    35  	var description string
    36  	pLen := len(r.packageNames)
    37  	if pLen == 1 {
    38  		description = "package_name=" + r.packageNames[0]
    39  	} else {
    40  		description = "package_name=[" + strings.Join(r.packageNames, " ") + "]"
    41  	}
    42  	return description
    43  }