mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-15 09:19:21 +02:00
Routing: Add regexp syntax support to UserMatcher (#3799)
This commit is contained in:
parent
12c3908e8c
commit
3a8c5f38e8
|
@ -193,17 +193,27 @@ func (v NetworkMatcher) Apply(ctx routing.Context) bool {
|
||||||
|
|
||||||
type UserMatcher struct {
|
type UserMatcher struct {
|
||||||
user []string
|
user []string
|
||||||
|
pattern []*regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUserMatcher(users []string) *UserMatcher {
|
func NewUserMatcher(users []string) *UserMatcher {
|
||||||
usersCopy := make([]string, 0, len(users))
|
usersCopy := make([]string, 0, len(users))
|
||||||
|
patternsCopy := make([]*regexp.Regexp, 0, len(users))
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
if len(user) > 0 {
|
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)
|
usersCopy = append(usersCopy, user)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &UserMatcher{
|
return &UserMatcher{
|
||||||
user: usersCopy,
|
user: usersCopy,
|
||||||
|
pattern: patternsCopy,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,6 +228,11 @@ func (v *UserMatcher) Apply(ctx routing.Context) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, re := range v.pattern {
|
||||||
|
if re.MatchString(user) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue