Skip to content

Commit 577b850

Browse files
authored
Routing: Add regexp syntax support to UserMatcher (XTLS#3799)
1 parent 762eb62 commit 577b850

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

app/router/condition.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,28 @@ func (v NetworkMatcher) Apply(ctx routing.Context) bool {
192192
}
193193

194194
type UserMatcher struct {
195-
user []string
195+
user []string
196+
pattern []*regexp.Regexp
196197
}
197198

198199
func NewUserMatcher(users []string) *UserMatcher {
199200
usersCopy := make([]string, 0, len(users))
201+
patternsCopy := make([]*regexp.Regexp, 0, len(users))
200202
for _, user := range users {
201203
if len(user) > 0 {
204+
if len(user) > 7 && strings.HasPrefix(user, "regexp:") {
205+
if re, err := regexp.Compile(user[7:]); err != nil {
206+
patternsCopy = append(patternsCopy, re)
207+
}
208+
// Items of users slice with an invalid regexp syntax are ignored.
209+
continue
210+
}
202211
usersCopy = append(usersCopy, user)
203212
}
204213
}
205214
return &UserMatcher{
206-
user: usersCopy,
215+
user: usersCopy,
216+
pattern: patternsCopy,
207217
}
208218
}
209219

@@ -218,6 +228,11 @@ func (v *UserMatcher) Apply(ctx routing.Context) bool {
218228
return true
219229
}
220230
}
231+
for _, re := range v.pattern {
232+
if re.MatchString(user) {
233+
return true
234+
}
235+
}
221236
return false
222237
}
223238

0 commit comments

Comments
 (0)