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,69 +45,64 @@ 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 { currentRuleIndex = ruleIndex + 1
dnsRules = dnsRules[ruleIndex+1:] }
for ; currentRuleIndex < len(r.dnsRules); currentRuleIndex++ {
currentRule := r.dnsRules[currentRuleIndex]
if currentRule.WithAddressLimit() && !isAddressQuery {
continue
} }
for currentRuleIndex, currentRule := range dnsRules { metadata.ResetRuleCache()
if currentRule.WithAddressLimit() && !isAddressQuery { if currentRule.Match(metadata) {
continue ruleDescription := currentRule.String()
if ruleDescription != "" {
r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] ", currentRule, " => ", currentRule.Action())
} else {
r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] => ", currentRule.Action())
} }
metadata.ResetRuleCache() switch action := currentRule.Action().(type) {
if currentRule.Match(metadata) { case *R.RuleActionDNSRoute:
displayRuleIndex := currentRuleIndex transport, loaded := r.transportMap[action.Server]
if displayRuleIndex != -1 { if !loaded {
displayRuleIndex += displayRuleIndex + 1 r.dnsLogger.ErrorContext(ctx, "transport not found: ", action.Server)
continue
} }
ruleDescription := currentRule.String() _, isFakeIP := transport.(adapter.FakeIPTransport)
if ruleDescription != "" { if isFakeIP && !allowFakeIP {
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] ", currentRule, " => ", currentRule.Action()) continue
}
if isFakeIP || action.DisableCache {
options.DisableCache = true
}
if action.RewriteTTL != nil {
options.RewriteTTL = action.RewriteTTL
}
if action.ClientSubnet.IsValid() {
options.ClientSubnet = action.ClientSubnet
}
if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
options.Strategy = domainStrategy
} else { } else {
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action()) options.Strategy = r.defaultDomainStrategy
} }
switch action := currentRule.Action().(type) { r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] => ", currentRule.Action())
case *R.RuleActionDNSRoute: return transport, options, currentRule, currentRuleIndex
transport, loaded := r.transportMap[action.Server] case *R.RuleActionDNSRouteOptions:
if !loaded { if action.DisableCache {
r.dnsLogger.ErrorContext(ctx, "transport not found: ", action.Server) options.DisableCache = true
continue
}
_, isFakeIP := transport.(adapter.FakeIPTransport)
if isFakeIP && !allowFakeIP {
continue
}
if isFakeIP || action.DisableCache {
options.DisableCache = true
}
if action.RewriteTTL != nil {
options.RewriteTTL = action.RewriteTTL
}
if action.ClientSubnet.IsValid() {
options.ClientSubnet = action.ClientSubnet
}
if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
options.Strategy = domainStrategy
} else {
options.Strategy = r.defaultDomainStrategy
}
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action())
return transport, options, currentRule, currentRuleIndex
case *R.RuleActionDNSRouteOptions:
if action.DisableCache {
options.DisableCache = true
}
if action.RewriteTTL != nil {
options.RewriteTTL = action.RewriteTTL
}
if action.ClientSubnet.IsValid() {
options.ClientSubnet = action.ClientSubnet
}
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action())
case *R.RuleActionReject:
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action())
return nil, options, currentRule, currentRuleIndex
} }
if action.RewriteTTL != nil {
options.RewriteTTL = action.RewriteTTL
}
if action.ClientSubnet.IsValid() {
options.ClientSubnet = action.ClientSubnet
}
r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] => ", currentRule.Action())
case *R.RuleActionReject:
r.logger.DebugContext(ctx, "match[", currentRuleIndex, "] => ", currentRule.Action())
return nil, options, currentRule, currentRuleIndex
} }
} }
} }
@ -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 {