Routing: Add regexp syntax support to UserMatcher (#3799)

This commit is contained in:
vnxme 2024-09-12 17:48:41 +03:00 committed by GitHub
parent 12c3908e8c
commit 3a8c5f38e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -192,18 +192,28 @@ func (v NetworkMatcher) Apply(ctx routing.Context) bool {
}
type UserMatcher struct {
user []string
user []string
pattern []*regexp.Regexp
}
func NewUserMatcher(users []string) *UserMatcher {
usersCopy := make([]string, 0, len(users))
patternsCopy := make([]*regexp.Regexp, 0, len(users))
for _, user := range users {
if len(user) > 0 {
if len(user) > 7 && strings.HasPrefix(user, "regexp:") {
if re, err := regexp.Compile(user[7:]); err != nil {
patternsCopy = append(patternsCopy, re)
}
// Items of users slice with an invalid regexp syntax are ignored.
continue
}
usersCopy = append(usersCopy, user)
}
}
return &UserMatcher{
user: usersCopy,
user: usersCopy,
pattern: patternsCopy,
}
}
@ -218,6 +228,11 @@ func (v *UserMatcher) Apply(ctx routing.Context) bool {
return true
}
}
for _, re := range v.pattern {
if re.MatchString(user) {
return true
}
}
return false
}