github.com/sagernet/sing-box@v1.9.0-rc.20/route/rule_item_query_type.go (about)

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