Fix DNS match

This commit is contained in:
世界 2024-12-15 21:27:39 +08:00
parent 9a1efbe54d
commit ff7aaf977b
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -45,26 +45,22 @@ func (r *Router) matchDNS(ctx context.Context, allowFakeIP bool, ruleIndex int,
panic("no context") panic("no context")
} }
var options dns.QueryOptions var options dns.QueryOptions
if ruleIndex < len(r.dnsRules) { var currentRuleIndex int
dnsRules := r.dnsRules
if ruleIndex != -1 { if ruleIndex != -1 {
dnsRules = dnsRules[ruleIndex+1:] currentRuleIndex = ruleIndex + 1
} }
for currentRuleIndex, currentRule := range dnsRules { for ; currentRuleIndex < len(r.dnsRules); currentRuleIndex++ {
currentRule := r.dnsRules[currentRuleIndex]
if currentRule.WithAddressLimit() && !isAddressQuery { if currentRule.WithAddressLimit() && !isAddressQuery {
continue continue
} }
metadata.ResetRuleCache() metadata.ResetRuleCache()
if currentRule.Match(metadata) { if currentRule.Match(metadata) {
displayRuleIndex := currentRuleIndex
if displayRuleIndex != -1 {
displayRuleIndex += displayRuleIndex + 1
}
ruleDescription := currentRule.String() ruleDescription := currentRule.String()
if ruleDescription != "" { if ruleDescription != "" {
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] ", currentRule, " => ", currentRule.Action()) r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] ", currentRule, " => ", currentRule.Action())
} else { } else {
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action()) r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] => ", currentRule.Action())
} }
switch action := currentRule.Action().(type) { switch action := currentRule.Action().(type) {
case *R.RuleActionDNSRoute: case *R.RuleActionDNSRoute:
@ -91,7 +87,7 @@ func (r *Router) matchDNS(ctx context.Context, allowFakeIP bool, ruleIndex int,
} else { } else {
options.Strategy = r.defaultDomainStrategy options.Strategy = r.defaultDomainStrategy
} }
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action()) r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] => ", currentRule.Action())
return transport, options, currentRule, currentRuleIndex return transport, options, currentRule, currentRuleIndex
case *R.RuleActionDNSRouteOptions: case *R.RuleActionDNSRouteOptions:
if action.DisableCache { if action.DisableCache {
@ -103,14 +99,13 @@ func (r *Router) matchDNS(ctx context.Context, allowFakeIP bool, ruleIndex int,
if action.ClientSubnet.IsValid() { if action.ClientSubnet.IsValid() {
options.ClientSubnet = action.ClientSubnet options.ClientSubnet = action.ClientSubnet
} }
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action()) r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] => ", currentRule.Action())
case *R.RuleActionReject: case *R.RuleActionReject:
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action()) r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] => ", currentRule.Action())
return nil, options, currentRule, currentRuleIndex return nil, options, currentRule, currentRuleIndex
} }
} }
} }
}
if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded { if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded {
options.Strategy = domainStrategy options.Strategy = domainStrategy
} else { } else {
@ -132,7 +127,6 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
} }
return &responseMessage, nil return &responseMessage, nil
} }
r.dnsLogger.DebugContext(ctx, "exchange ", formatQuestion(message.Question[0].String()))
var ( var (
response *mDNS.Msg response *mDNS.Msg
cached bool cached bool
@ -173,14 +167,11 @@ func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, er
} }
} }
} }
r.dnsLogger.DebugContext(ctx, "exchange ", formatQuestion(message.Question[0].String()), " via ", transport.Name())
if rule != nil && rule.WithAddressLimit() { if rule != nil && rule.WithAddressLimit() {
addressLimit = true addressLimit = true
response, err = r.dnsClient.ExchangeWithResponseCheck(dnsCtx, transport, message, options, func(response *mDNS.Msg) bool { response, err = r.dnsClient.ExchangeWithResponseCheck(dnsCtx, transport, message, options, func(responseAddrs []netip.Addr) bool {
addresses, addrErr := dns.MessageToAddresses(response) metadata.DestinationAddresses = responseAddrs
if addrErr != nil {
return false
}
metadata.DestinationAddresses = addresses
return rule.MatchAddressLimit(metadata) return rule.MatchAddressLimit(metadata)
}) })
} else { } else {