@@ -185,6 +185,20 @@ func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainS
185
185
cached bool
186
186
err error
187
187
)
188
+ printResult := func () {
189
+ if err != nil {
190
+ if errors .Is (err , dns .ErrResponseRejectedCached ) {
191
+ r .dnsLogger .DebugContext (ctx , "response rejected for " , domain , " (cached)" )
192
+ } else if errors .Is (err , dns .ErrResponseRejected ) {
193
+ r .dnsLogger .DebugContext (ctx , "response rejected for " , domain )
194
+ } else {
195
+ r .dnsLogger .ErrorContext (ctx , E .Cause (err , "lookup failed for " , domain ))
196
+ }
197
+ } else if len (responseAddrs ) == 0 {
198
+ r .dnsLogger .ErrorContext (ctx , "lookup failed for " , domain , ": empty result" )
199
+ err = dns .RCodeNameError
200
+ }
201
+ }
188
202
responseAddrs , cached = r .dnsClient .LookupCache (ctx , domain , strategy )
189
203
if cached {
190
204
if len (responseAddrs ) == 0 {
@@ -196,46 +210,51 @@ func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainS
196
210
ctx , metadata := adapter .ExtendContext (ctx )
197
211
metadata .Destination = M.Socksaddr {}
198
212
metadata .Domain = domain
199
- var (
200
- transport dns.Transport
201
- options dns.QueryOptions
202
- rule adapter.DNSRule
203
- ruleIndex int
204
- )
205
- ruleIndex = - 1
206
- for {
207
- dnsCtx := adapter .OverrideContext (ctx )
208
- var addressLimit bool
209
- transport , options , rule , ruleIndex = r .matchDNS (ctx , false , ruleIndex , true )
210
- if strategy != dns .DomainStrategyAsIS {
211
- options .Strategy = strategy
212
- }
213
- if rule != nil && rule .WithAddressLimit () {
214
- addressLimit = true
215
- responseAddrs , err = r .dnsClient .LookupWithResponseCheck (dnsCtx , transport , domain , options , func (responseAddrs []netip.Addr ) bool {
216
- metadata .DestinationAddresses = responseAddrs
217
- return rule .MatchAddressLimit (metadata )
218
- })
219
- } else {
220
- addressLimit = false
221
- responseAddrs , err = r .dnsClient .Lookup (dnsCtx , transport , domain , options )
213
+ if metadata .DNSServer != "" {
214
+ transport , loaded := r .transportMap [metadata .DNSServer ]
215
+ if ! loaded {
216
+ return nil , E .New ("transport not found: " , metadata .DNSServer )
222
217
}
223
- if err != nil {
224
- if errors .Is (err , dns .ErrResponseRejectedCached ) {
225
- r .dnsLogger .DebugContext (ctx , "response rejected for " , domain , " (cached)" )
226
- } else if errors .Is (err , dns .ErrResponseRejected ) {
227
- r .dnsLogger .DebugContext (ctx , "response rejected for " , domain )
218
+ if strategy == dns .DomainStrategyAsIS {
219
+ if transportDomainStrategy , loaded := r .transportDomainStrategy [transport ]; loaded {
220
+ strategy = transportDomainStrategy
228
221
} else {
229
- r . dnsLogger . ErrorContext ( ctx , E . Cause ( err , "lookup failed for " , domain ))
222
+ strategy = r . defaultDomainStrategy
230
223
}
231
- } else if len (responseAddrs ) == 0 {
232
- r .dnsLogger .ErrorContext (ctx , "lookup failed for " , domain , ": empty result" )
233
- err = dns .RCodeNameError
234
224
}
235
- if ! addressLimit || err == nil {
236
- break
225
+ responseAddrs , err = r .dnsClient .Lookup (ctx , transport , domain , dns.QueryOptions {Strategy : strategy })
226
+ } else {
227
+ var (
228
+ transport dns.Transport
229
+ options dns.QueryOptions
230
+ rule adapter.DNSRule
231
+ ruleIndex int
232
+ )
233
+ ruleIndex = - 1
234
+ for {
235
+ dnsCtx := adapter .OverrideContext (ctx )
236
+ var addressLimit bool
237
+ transport , options , rule , ruleIndex = r .matchDNS (ctx , false , ruleIndex , true )
238
+ if strategy != dns .DomainStrategyAsIS {
239
+ options .Strategy = strategy
240
+ }
241
+ if rule != nil && rule .WithAddressLimit () {
242
+ addressLimit = true
243
+ responseAddrs , err = r .dnsClient .LookupWithResponseCheck (dnsCtx , transport , domain , options , func (responseAddrs []netip.Addr ) bool {
244
+ metadata .DestinationAddresses = responseAddrs
245
+ return rule .MatchAddressLimit (metadata )
246
+ })
247
+ } else {
248
+ addressLimit = false
249
+ responseAddrs , err = r .dnsClient .Lookup (dnsCtx , transport , domain , options )
250
+ }
251
+ if ! addressLimit || err == nil {
252
+ break
253
+ }
254
+ printResult ()
237
255
}
238
256
}
257
+ printResult ()
239
258
if len (responseAddrs ) > 0 {
240
259
r .dnsLogger .InfoContext (ctx , "lookup succeed for " , domain , ": " , strings .Join (F .MapToString (responseAddrs ), " " ))
241
260
}
0 commit comments