Add user rule item
This commit is contained in:
@@ -82,8 +82,13 @@ func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options opt
|
||||
return nil, E.New("invalid network: ", options.Network)
|
||||
}
|
||||
}
|
||||
if len(options.User) > 0 {
|
||||
item := NewUserItem(options.User)
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
if len(options.Protocol) > 0 {
|
||||
item := NewProtocolItem(options.Protocol)
|
||||
item := NewUserItem(options.Protocol)
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
|
||||
@@ -66,8 +66,13 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
|
||||
return nil, E.New("invalid network: ", options.Network)
|
||||
}
|
||||
}
|
||||
if len(options.User) > 0 {
|
||||
item := NewUserItem(options.User)
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
if len(options.Protocol) > 0 {
|
||||
item := NewProtocolItem(options.Protocol)
|
||||
item := NewUserItem(options.Protocol)
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
|
||||
37
route/rule_user.go
Normal file
37
route/rule_user.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
)
|
||||
|
||||
var _ RuleItem = (*UserItem)(nil)
|
||||
|
||||
type UserItem struct {
|
||||
users []string
|
||||
userMap map[string]bool
|
||||
}
|
||||
|
||||
func NewUserItem(users []string) *UserItem {
|
||||
userMap := make(map[string]bool)
|
||||
for _, protocol := range users {
|
||||
userMap[protocol] = true
|
||||
}
|
||||
return &UserItem{
|
||||
users: users,
|
||||
userMap: userMap,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *UserItem) Match(metadata *adapter.InboundContext) bool {
|
||||
return r.userMap[metadata.User]
|
||||
}
|
||||
|
||||
func (r *UserItem) String() string {
|
||||
if len(r.users) == 1 {
|
||||
return F.ToString("user=", r.users[0])
|
||||
}
|
||||
return F.ToString("user=[", strings.Join(r.users, " "), "]")
|
||||
}
|
||||
Reference in New Issue
Block a user