github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/route/rule_item_auth_user.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 = (*AuthUserItem)(nil)
    11  
    12  type AuthUserItem struct {
    13  	users   []string
    14  	userMap map[string]bool
    15  }
    16  
    17  func NewAuthUserItem(users []string) *AuthUserItem {
    18  	userMap := make(map[string]bool)
    19  	for _, protocol := range users {
    20  		userMap[protocol] = true
    21  	}
    22  	return &AuthUserItem{
    23  		users:   users,
    24  		userMap: userMap,
    25  	}
    26  }
    27  
    28  func (r *AuthUserItem) Match(metadata *adapter.InboundContext) bool {
    29  	return r.userMap[metadata.User]
    30  }
    31  
    32  func (r *AuthUserItem) String() string {
    33  	if len(r.users) == 1 {
    34  		return F.ToString("auth_user=", r.users[0])
    35  	}
    36  	return F.ToString("auth_user=[", strings.Join(r.users, " "), "]")
    37  }