rule_auth_user.go 742 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package route
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. F "github.com/sagernet/sing/common/format"
  6. )
  7. var _ RuleItem = (*AuthUserItem)(nil)
  8. type AuthUserItem struct {
  9. users []string
  10. userMap map[string]bool
  11. }
  12. func NewAuthUserItem(users []string) *AuthUserItem {
  13. userMap := make(map[string]bool)
  14. for _, protocol := range users {
  15. userMap[protocol] = true
  16. }
  17. return &AuthUserItem{
  18. users: users,
  19. userMap: userMap,
  20. }
  21. }
  22. func (r *AuthUserItem) Match(metadata *adapter.InboundContext) bool {
  23. return r.userMap[metadata.User]
  24. }
  25. func (r *AuthUserItem) String() string {
  26. if len(r.users) == 1 {
  27. return F.ToString("auth_user=", r.users[0])
  28. }
  29. return F.ToString("auth_user=[", strings.Join(r.users, " "), "]")
  30. }