rule_query_type.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package route
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. "github.com/sagernet/sing-box/option"
  6. "github.com/sagernet/sing/common"
  7. )
  8. var _ RuleItem = (*QueryTypeItem)(nil)
  9. type QueryTypeItem struct {
  10. typeList []uint16
  11. typeMap map[uint16]bool
  12. }
  13. func NewQueryTypeItem(typeList []option.DNSQueryType) *QueryTypeItem {
  14. rule := &QueryTypeItem{
  15. typeList: common.Map(typeList, func(it option.DNSQueryType) uint16 {
  16. return uint16(it)
  17. }),
  18. typeMap: make(map[uint16]bool),
  19. }
  20. for _, userId := range rule.typeList {
  21. rule.typeMap[userId] = true
  22. }
  23. return rule
  24. }
  25. func (r *QueryTypeItem) Match(metadata *adapter.InboundContext) bool {
  26. if metadata.QueryType == 0 {
  27. return false
  28. }
  29. return r.typeMap[metadata.QueryType]
  30. }
  31. func (r *QueryTypeItem) String() string {
  32. var description string
  33. pLen := len(r.typeList)
  34. if pLen == 1 {
  35. description = "query_type=" + option.DNSQueryTypeToString(r.typeList[0])
  36. } else {
  37. description = "query_type=[" + strings.Join(common.Map(r.typeList, option.DNSQueryTypeToString), " ") + "]"
  38. }
  39. return description
  40. }