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

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