Fix DNS match log

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

View File

@ -45,69 +45,70 @@ 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 (
dnsRules := r.dnsRules currentRuleIndex int
if ruleIndex != -1 { currentRule adapter.DNSRule
dnsRules = dnsRules[ruleIndex+1:] )
if ruleIndex != -1 {
currentRuleIndex = ruleIndex + 1
}
for currentRuleIndex, currentRule = range r.dnsRules[currentRuleIndex:] {
if currentRule.WithAddressLimit() && !isAddressQuery {
continue
} }
for currentRuleIndex, currentRule := range dnsRules { metadata.ResetRuleCache()
if currentRule.WithAddressLimit() && !isAddressQuery { if currentRule.Match(metadata) {
continue displayRuleIndex := currentRuleIndex
if ruleIndex != -1 {
displayRuleIndex += ruleIndex + 1
} }
metadata.ResetRuleCache() ruleDescription := currentRule.String()
if currentRule.Match(metadata) { if ruleDescription != "" {
displayRuleIndex := currentRuleIndex r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] ", currentRule, " => ", currentRule.Action())
if displayRuleIndex != -1 { } else {
displayRuleIndex += displayRuleIndex + 1 r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action())
}
switch action := currentRule.Action().(type) {
case *R.RuleActionDNSRoute:
transport, loaded := r.transportMap[action.Server]
if !loaded {
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[", displayRuleIndex, "] => ", 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[", displayRuleIndex, "] => ", currentRule.Action())
case *R.RuleActionReject:
r.logger.DebugContext(ctx, "match[", displayRuleIndex, "] => ", currentRule.Action())
return nil, options, currentRule, currentRuleIndex
} }
} }
} }