Add ip_version rule item
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-dns"
|
||||
@@ -18,6 +19,15 @@ func (r *Router) Exchange(ctx context.Context, message *dnsmessage.Message) (*dn
|
||||
if len(message.Questions) > 0 {
|
||||
r.dnsLogger.DebugContext(ctx, "exchange ", formatDNSQuestion(message.Questions[0]))
|
||||
}
|
||||
ctx, metadata := adapter.AppendContext(ctx)
|
||||
if len(message.Questions) > 0 {
|
||||
switch message.Questions[0].Type {
|
||||
case dnsmessage.TypeA:
|
||||
metadata.IPVersion = 4
|
||||
case dnsmessage.TypeAAAA:
|
||||
metadata.IPVersion = 6
|
||||
}
|
||||
}
|
||||
ctx, transport := r.matchDNS(ctx)
|
||||
ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
|
||||
defer cancel()
|
||||
|
||||
@@ -58,6 +58,16 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
}
|
||||
if options.IPVersion > 0 {
|
||||
switch options.IPVersion {
|
||||
case 4, 6:
|
||||
item := NewIPVersionItem(options.IPVersion == 6)
|
||||
rule.items = append(rule.items, item)
|
||||
rule.allItems = append(rule.allItems, item)
|
||||
default:
|
||||
return nil, E.New("invalid ip version: ", options.IPVersion)
|
||||
}
|
||||
}
|
||||
if options.Network != "" {
|
||||
switch options.Network {
|
||||
case N.NetworkTCP, N.NetworkUDP:
|
||||
|
||||
@@ -15,7 +15,8 @@ func NewIPVersionItem(isIPv6 bool) *IPVersionItem {
|
||||
}
|
||||
|
||||
func (r *IPVersionItem) Match(metadata *adapter.InboundContext) bool {
|
||||
return metadata.Destination.IsIP() && metadata.Destination.IsIPv6() == r.isIPv6
|
||||
return metadata.IPVersion != 0 && metadata.IPVersion == 6 == r.isIPv6 ||
|
||||
metadata.Destination.IsIP() && metadata.Destination.IsIPv6() == r.isIPv6
|
||||
}
|
||||
|
||||
func (r *IPVersionItem) String() string {
|
||||
|
||||
Reference in New Issue
Block a user