mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-12-22 11:23:32 +02:00
Routing: Add regexp syntax support to UserMatcher (#3799)
This commit is contained in:
parent
12c3908e8c
commit
3a8c5f38e8
1 changed files with 17 additions and 2 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue