@@ -13,7 +13,7 @@ use std::{
13
13
use std:: { net:: IpAddr , time:: Duration } ;
14
14
15
15
use bloomfilter:: Bloom ;
16
- use log:: warn;
16
+ use log:: { log_enabled , warn} ;
17
17
#[ cfg( feature = "local-dns-relay" ) ]
18
18
use lru_time_cache:: LruCache ;
19
19
use spin:: Mutex ;
@@ -293,9 +293,36 @@ impl Context {
293
293
294
294
/// Perform a DNS resolution
295
295
pub async fn dns_resolve ( & self , host : & str , port : u16 ) -> io:: Result < Vec < SocketAddr > > {
296
- #[ cfg( feature = "local-dns-relay" ) ]
297
- return self . local_dns ( ) . lookup_ip ( host, port) . await ;
298
- #[ cfg( not( feature = "local-dns-relay" ) ) ]
296
+ if log_enabled ! ( log:: Level :: Debug ) {
297
+ use log:: debug;
298
+ use std:: time:: Instant ;
299
+
300
+ let start = Instant :: now ( ) ;
301
+ let result = self . dns_resolve_inner ( host, port) . await ;
302
+ let elapsed = Instant :: now ( ) - start;
303
+ debug ! (
304
+ "DNS resolved {}:{} elapsed: {}.{:03}s, {:?}" ,
305
+ host,
306
+ port,
307
+ elapsed. as_secs( ) ,
308
+ elapsed. subsec_millis( ) ,
309
+ result
310
+ ) ;
311
+ result
312
+ } else {
313
+ self . dns_resolve_inner ( host, port) . await
314
+ }
315
+ }
316
+
317
+ #[ cfg( feature = "local-dns-relay" ) ]
318
+ #[ inline( always) ]
319
+ async fn dns_resolve_inner ( & self , host : & str , port : u16 ) -> io:: Result < Vec < SocketAddr > > {
320
+ self . local_dns ( ) . lookup_ip ( host, port) . await
321
+ }
322
+
323
+ #[ cfg( not( feature = "local-dns-relay" ) ) ]
324
+ #[ inline( always) ]
325
+ async fn dns_resolve_inner ( & self , host : & str , port : u16 ) -> io:: Result < Vec < SocketAddr > > {
299
326
resolve ( self , host, port) . await
300
327
}
301
328
@@ -327,23 +354,23 @@ impl Context {
327
354
pub fn check_client_blocked ( & self , addr : & SocketAddr ) -> bool {
328
355
match self . acl ( ) {
329
356
None => false ,
330
- Some ( ref a) => a. check_client_blocked ( addr) ,
357
+ Some ( a) => a. check_client_blocked ( addr) ,
331
358
}
332
359
}
333
360
334
361
/// Check outbound address ACL (for server)
335
362
pub fn check_outbound_blocked ( & self , addr : & Address ) -> bool {
336
363
match self . acl ( ) {
337
364
None => false ,
338
- Some ( ref a) => a. check_outbound_blocked ( addr) ,
365
+ Some ( a) => a. check_outbound_blocked ( addr) ,
339
366
}
340
367
}
341
368
342
369
/// Check resolved outbound address ACL (for server)
343
370
pub fn check_resolved_outbound_blocked ( & self , addr : & SocketAddr ) -> bool {
344
371
match self . acl ( ) {
345
372
None => false ,
346
- Some ( ref a) => a. check_resolved_outbound_blocked ( addr) ,
373
+ Some ( a) => a. check_resolved_outbound_blocked ( addr) ,
347
374
}
348
375
}
349
376
@@ -354,7 +381,7 @@ impl Context {
354
381
!= match self . acl ( ) {
355
382
// Proxy everything by default
356
383
None => true ,
357
- Some ( ref a) => a. check_ip_in_proxy_list ( addr) ,
384
+ Some ( a) => a. check_ip_in_proxy_list ( addr) ,
358
385
} ;
359
386
let mut reverse_lookup_cache = self . reverse_lookup_cache . lock ( ) ;
360
387
match reverse_lookup_cache. get_mut ( addr) {
@@ -374,10 +401,12 @@ impl Context {
374
401
}
375
402
}
376
403
377
- pub fn acl ( & self ) -> & Option < AccessControl > {
378
- & self . config . acl
404
+ /// Get ACL control instance
405
+ pub fn acl ( & self ) -> Option < & AccessControl > {
406
+ self . config . acl . as_ref ( )
379
407
}
380
408
409
+ /// Get local DNS connector
381
410
#[ cfg( feature = "local-dns-relay" ) ]
382
411
pub fn local_dns ( & self ) -> & LocalUpstream {
383
412
& self . local_dns
@@ -388,21 +417,43 @@ impl Context {
388
417
match self . acl ( ) {
389
418
// Proxy everything by default
390
419
None => false ,
391
- Some ( ref a) => {
392
- #[ cfg( feature = "local-dns-relay" ) ]
393
- {
394
- if let Address :: SocketAddress ( ref saddr) = target {
395
- // do the reverse lookup in our local cache
396
- let mut reverse_lookup_cache = self . reverse_lookup_cache . lock ( ) ;
397
- // if a qname is found
398
- if let Some ( forward) = reverse_lookup_cache. get ( & saddr. ip ( ) ) {
399
- return !* forward;
400
- }
401
- }
420
+ Some ( a) => {
421
+ if log_enabled ! ( log:: Level :: Debug ) {
422
+ use log:: debug;
423
+ use std:: time:: Instant ;
424
+
425
+ let start = Instant :: now ( ) ;
426
+ let r = self . check_target_bypassed_with_acl ( a, target) . await ;
427
+ let elapsed = Instant :: now ( ) - start;
428
+ debug ! (
429
+ "check bypassing {} elapsed {}.{:03}s, result: {}" ,
430
+ target,
431
+ elapsed. as_secs( ) ,
432
+ elapsed. subsec_millis( ) ,
433
+ if r { "bypassed" } else { "proxied" }
434
+ ) ;
435
+ r
436
+ } else {
437
+ self . check_target_bypassed_with_acl ( a, target) . await
438
+ }
439
+ }
440
+ }
441
+ }
442
+
443
+ #[ inline( always) ]
444
+ async fn check_target_bypassed_with_acl ( & self , a : & AccessControl , target : & Address ) -> bool {
445
+ #[ cfg( feature = "local-dns-relay" ) ]
446
+ {
447
+ if let Address :: SocketAddress ( ref saddr) = target {
448
+ // do the reverse lookup in our local cache
449
+ let mut reverse_lookup_cache = self . reverse_lookup_cache . lock ( ) ;
450
+ // if a qname is found
451
+ if let Some ( forward) = reverse_lookup_cache. get ( & saddr. ip ( ) ) {
452
+ return !* forward;
402
453
}
403
- a. check_target_bypassed ( self , target) . await
404
454
}
405
455
}
456
+ a. check_target_bypassed ( self , target) . await
406
457
}
407
458
408
459
/// Get client flow statistics
0 commit comments