Add process_name/package_name/user/user_id rule item

This commit is contained in:
世界
2022-07-23 19:01:41 +08:00
parent 4abf669d09
commit 5f6f33c464
23 changed files with 1191 additions and 55 deletions

37
route/rule_auth_user.go Normal file
View File

@@ -0,0 +1,37 @@
package route
import (
"strings"
"github.com/sagernet/sing-box/adapter"
F "github.com/sagernet/sing/common/format"
)
var _ RuleItem = (*AuthUserItem)(nil)
type AuthUserItem struct {
users []string
userMap map[string]bool
}
func NewAuthUserItem(users []string) *AuthUserItem {
userMap := make(map[string]bool)
for _, protocol := range users {
userMap[protocol] = true
}
return &AuthUserItem{
users: users,
userMap: userMap,
}
}
func (r *AuthUserItem) Match(metadata *adapter.InboundContext) bool {
return r.userMap[metadata.User]
}
func (r *AuthUserItem) String() string {
if len(r.users) == 1 {
return F.ToString("auth_user=", r.users[0])
}
return F.ToString("auth_user=[", strings.Join(r.users, " "), "]")
}