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