sing-box/route/rule/rule_item_user.go

41 lines
802 B
Go
Raw Permalink Normal View History

2024-10-21 15:38:34 +00:00
package rule
2022-07-17 07:11:26 +00:00
import (
"strings"
"github.com/sagernet/sing-box/adapter"
F "github.com/sagernet/sing/common/format"
)
var _ RuleItem = (*UserItem)(nil)
type UserItem struct {
users []string
userMap map[string]bool
}
func NewUserItem(users []string) *UserItem {
userMap := make(map[string]bool)
for _, protocol := range users {
userMap[protocol] = true
}
return &UserItem{
users: users,
userMap: userMap,
}
}
func (r *UserItem) Match(metadata *adapter.InboundContext) bool {
if metadata.ProcessInfo == nil || metadata.ProcessInfo.User == "" {
return false
}
return r.userMap[metadata.ProcessInfo.User]
2022-07-17 07:11:26 +00:00
}
func (r *UserItem) String() string {
if len(r.users) == 1 {
return F.ToString("user=", r.users[0])
}
return F.ToString("user=[", strings.Join(r.users, " "), "]")
}