Add query_type DNS rule item

This commit is contained in:
世界
2023-02-08 16:18:40 +08:00
committed by GitHub
parent 41ec2e7944
commit 687b4509df
8 changed files with 143 additions and 27 deletions

View File

@@ -50,7 +50,8 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
}
ctx, metadata := adapter.AppendContext(ctx)
if len(message.Question) > 0 {
switch message.Question[0].Qtype {
metadata.QueryType = message.Question[0].Qtype
switch metadata.QueryType {
case mDNS.TypeA:
metadata.IPVersion = 4
case mDNS.TypeAAAA:

View File

@@ -71,6 +71,11 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
return nil, E.New("invalid ip version: ", options.IPVersion)
}
}
if len(options.QueryType) > 0 {
item := NewQueryTypeItem(options.QueryType)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}
if options.Network != "" {
switch options.Network {
case N.NetworkTCP, N.NetworkUDP:

47
route/rule_query_type.go Normal file
View File

@@ -0,0 +1,47 @@
package route
import (
"strings"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
)
var _ RuleItem = (*QueryTypeItem)(nil)
type QueryTypeItem struct {
typeList []uint16
typeMap map[uint16]bool
}
func NewQueryTypeItem(typeList []option.DNSQueryType) *QueryTypeItem {
rule := &QueryTypeItem{
typeList: common.Map(typeList, func(it option.DNSQueryType) uint16 {
return uint16(it)
}),
typeMap: make(map[uint16]bool),
}
for _, userId := range rule.typeList {
rule.typeMap[userId] = true
}
return rule
}
func (r *QueryTypeItem) Match(metadata *adapter.InboundContext) bool {
if metadata.QueryType == 0 {
return false
}
return r.typeMap[metadata.QueryType]
}
func (r *QueryTypeItem) String() string {
var description string
pLen := len(r.typeList)
if pLen == 1 {
description = "query_type=" + option.DNSQueryTypeToString(r.typeList[0])
} else {
description = "query_type=[" + strings.Join(common.Map(r.typeList, option.DNSQueryTypeToString), " ") + "]"
}
return description
}