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

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