@@ -45,69 +45,70 @@ func (r *Router) matchDNS(ctx context.Context, allowFakeIP bool, ruleIndex int,
45
45
panic ("no context" )
46
46
}
47
47
var options dns.QueryOptions
48
- if ruleIndex < len (r .dnsRules ) {
49
- dnsRules := r .dnsRules
50
- if ruleIndex != - 1 {
51
- dnsRules = dnsRules [ruleIndex + 1 :]
48
+ var (
49
+ currentRuleIndex int
50
+ currentRule adapter.DNSRule
51
+ )
52
+ if ruleIndex != - 1 {
53
+ currentRuleIndex = ruleIndex + 1
54
+ }
55
+ for currentRuleIndex , currentRule = range r .dnsRules [currentRuleIndex :] {
56
+ if currentRule .WithAddressLimit () && ! isAddressQuery {
57
+ continue
52
58
}
53
- for currentRuleIndex , currentRule := range dnsRules {
54
- if currentRule .WithAddressLimit () && ! isAddressQuery {
55
- continue
59
+ metadata .ResetRuleCache ()
60
+ if currentRule .Match (metadata ) {
61
+ displayRuleIndex := currentRuleIndex
62
+ if ruleIndex != - 1 {
63
+ displayRuleIndex += ruleIndex + 1
64
+ }
65
+ ruleDescription := currentRule .String ()
66
+ if ruleDescription != "" {
67
+ r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] " , currentRule , " => " , currentRule .Action ())
68
+ } else {
69
+ r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] => " , currentRule .Action ())
56
70
}
57
- metadata .ResetRuleCache ()
58
- if currentRule .Match (metadata ) {
59
- displayRuleIndex := currentRuleIndex
60
- if displayRuleIndex != - 1 {
61
- displayRuleIndex += displayRuleIndex + 1
71
+ switch action := currentRule .Action ().(type ) {
72
+ case * R.RuleActionDNSRoute :
73
+ transport , loaded := r .transportMap [action .Server ]
74
+ if ! loaded {
75
+ r .dnsLogger .ErrorContext (ctx , "transport not found: " , action .Server )
76
+ continue
62
77
}
63
- ruleDescription := currentRule .String ()
64
- if ruleDescription != "" {
65
- r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] " , currentRule , " => " , currentRule .Action ())
78
+ _ , isFakeIP := transport .(adapter.FakeIPTransport )
79
+ if isFakeIP && ! allowFakeIP {
80
+ continue
81
+ }
82
+ if isFakeIP || action .DisableCache {
83
+ options .DisableCache = true
84
+ }
85
+ if action .RewriteTTL != nil {
86
+ options .RewriteTTL = action .RewriteTTL
87
+ }
88
+ if action .ClientSubnet .IsValid () {
89
+ options .ClientSubnet = action .ClientSubnet
90
+ }
91
+ if domainStrategy , dsLoaded := r .transportDomainStrategy [transport ]; dsLoaded {
92
+ options .Strategy = domainStrategy
66
93
} else {
67
- r . logger . DebugContext ( ctx , "match[" , displayRuleIndex , "] => " , currentRule . Action ())
94
+ options . Strategy = r . defaultDomainStrategy
68
95
}
69
- switch action := currentRule .Action ().(type ) {
70
- case * R.RuleActionDNSRoute :
71
- transport , loaded := r .transportMap [action .Server ]
72
- if ! loaded {
73
- r .dnsLogger .ErrorContext (ctx , "transport not found: " , action .Server )
74
- continue
75
- }
76
- _ , isFakeIP := transport .(adapter.FakeIPTransport )
77
- if isFakeIP && ! allowFakeIP {
78
- continue
79
- }
80
- if isFakeIP || action .DisableCache {
81
- options .DisableCache = true
82
- }
83
- if action .RewriteTTL != nil {
84
- options .RewriteTTL = action .RewriteTTL
85
- }
86
- if action .ClientSubnet .IsValid () {
87
- options .ClientSubnet = action .ClientSubnet
88
- }
89
- if domainStrategy , dsLoaded := r .transportDomainStrategy [transport ]; dsLoaded {
90
- options .Strategy = domainStrategy
91
- } else {
92
- options .Strategy = r .defaultDomainStrategy
93
- }
94
- r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] => " , currentRule .Action ())
95
- return transport , options , currentRule , currentRuleIndex
96
- case * R.RuleActionDNSRouteOptions :
97
- if action .DisableCache {
98
- options .DisableCache = true
99
- }
100
- if action .RewriteTTL != nil {
101
- options .RewriteTTL = action .RewriteTTL
102
- }
103
- if action .ClientSubnet .IsValid () {
104
- options .ClientSubnet = action .ClientSubnet
105
- }
106
- r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] => " , currentRule .Action ())
107
- case * R.RuleActionReject :
108
- r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] => " , currentRule .Action ())
109
- return nil , options , currentRule , currentRuleIndex
96
+ r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] => " , currentRule .Action ())
97
+ return transport , options , currentRule , currentRuleIndex
98
+ case * R.RuleActionDNSRouteOptions :
99
+ if action .DisableCache {
100
+ options .DisableCache = true
101
+ }
102
+ if action .RewriteTTL != nil {
103
+ options .RewriteTTL = action .RewriteTTL
104
+ }
105
+ if action .ClientSubnet .IsValid () {
106
+ options .ClientSubnet = action .ClientSubnet
110
107
}
108
+ r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] => " , currentRule .Action ())
109
+ case * R.RuleActionReject :
110
+ r .logger .DebugContext (ctx , "match[" , displayRuleIndex , "] => " , currentRule .Action ())
111
+ return nil , options , currentRule , currentRuleIndex
111
112
}
112
113
}
113
114
}
0 commit comments