5
5
import re
6
6
import textwrap
7
7
import threading
8
+ import time
8
9
from datetime import datetime
9
10
from functools import cache
10
11
from ipaddress import IPv4Address , IPv4Interface
@@ -374,20 +375,37 @@ def resolve(self, request: DNSRecord, handler: DNSHandler) -> DNSRecord | None:
374
375
reply = request .reply ()
375
376
found = False
376
377
378
+ LOG .debug (
379
+ "[%s] received dns query: %s (type %s)" ,
380
+ request .header .id ,
381
+ request .q .qname ,
382
+ QTYPE .get (request .q .qtype , "??" ),
383
+ )
384
+
377
385
try :
378
386
if not self ._skip_local_resolution (request ):
379
387
found = self ._resolve_name (request , reply , handler .client_address )
380
388
except Exception as e :
381
389
LOG .info ("Unable to get DNS result: %s" , e )
382
390
383
391
if found :
392
+ LOG .debug ("[%s] found name locally" , request .header .id )
384
393
return reply
385
394
395
+ LOG .debug ("[%s] name not found locally, querying upstream" , request .header .id )
386
396
# If we did not find a matching record in our local zones, we forward to our upstream dns
387
397
try :
398
+ start_time = time .perf_counter ()
388
399
req_parsed = dns .message .from_wire (bytes (request .pack ()))
389
400
r = dns .query .udp (req_parsed , self .upstream_dns , timeout = REQUEST_TIMEOUT_SECS )
390
401
result = self ._map_response_dnspython_to_dnslib (r )
402
+ end_time = time .perf_counter ()
403
+ LOG .debug (
404
+ "[%s] name resolved upstream in %d ms (rcode %s)" ,
405
+ request .header .id ,
406
+ (end_time - start_time ) / 1000.0 ,
407
+ RCODE .get (result .header .rcode , "??" ),
408
+ )
391
409
return result
392
410
except Exception as e :
393
411
LOG .info (
@@ -401,11 +419,12 @@ def resolve(self, request: DNSRecord, handler: DNSHandler) -> DNSRecord | None:
401
419
if not reply .rr and reply .header .get_rcode == RCODE .NOERROR :
402
420
# setting this return code will cause commands like 'host' to try the next nameserver
403
421
reply .header .set_rcode (RCODE .SERVFAIL )
422
+ LOG .debug ("[%s] failed to resolve name" , request .header .id )
404
423
return None
405
424
406
425
return reply
407
426
408
- def _skip_local_resolution (self , request ) -> bool :
427
+ def _skip_local_resolution (self , request : DNSRecord ) -> bool :
409
428
"""
410
429
Check whether we should skip local resolution for the given request, and directly contact upstream
411
430
@@ -415,6 +434,7 @@ def _skip_local_resolution(self, request) -> bool:
415
434
request_name = to_str (str (request .q .qname ))
416
435
for p in self .skip_patterns :
417
436
if re .match (p , request_name ):
437
+ LOG .debug ("[%s] skipping local resolution of query" , request .header .id )
418
438
return True
419
439
return False
420
440
@@ -449,7 +469,7 @@ def _resolve_name(
449
469
self , request : DNSRecord , reply : DNSRecord , client_address : ClientAddress
450
470
) -> bool :
451
471
if alias_found := self ._resolve_alias (request , reply , client_address ):
452
- LOG .debug ("Alias found: %s" , request .q .qname )
472
+ LOG .debug ("[%s] alias found: %s" , request . header . id , request .q .qname )
453
473
return alias_found
454
474
return self ._resolve_name_from_zones (request , reply , client_address )
455
475
@@ -671,7 +691,7 @@ def do_run(self):
671
691
self .servers = self ._get_servers ()
672
692
for server in self .servers :
673
693
server .start_thread ()
674
- LOG .debug ("DNS Server started" )
694
+ LOG .info ("DNS Server started" )
675
695
for server in self .servers :
676
696
server .thread .join ()
677
697
@@ -780,7 +800,7 @@ def get_available_dns_server():
780
800
pass
781
801
782
802
if result :
783
- LOG .debug ("Determined fallback dns: %s" , result )
803
+ LOG .info ("Determined fallback dns: %s" , result )
784
804
else :
785
805
LOG .info (
786
806
"Unable to determine fallback DNS. Please check if '%s' is reachable by your configured DNS servers"
@@ -864,10 +884,10 @@ def start_server(upstream_dns: str, host: str, port: int = config.DNS_PORT):
864
884
865
885
if DNS_SERVER :
866
886
# already started - bail
867
- LOG .debug ("DNS servers are already started. Avoid starting again." )
887
+ LOG .info ("DNS servers are already started. Avoid starting again." )
868
888
return
869
889
870
- LOG .debug ("Starting DNS servers (tcp/udp port %s on %s)..." % (port , host ))
890
+ LOG .info ("Starting DNS servers (tcp/udp port %s on %s)..." % (port , host ))
871
891
dns_server = DnsServer (port , protocols = ["tcp" , "udp" ], host = host , upstream_dns = upstream_dns )
872
892
873
893
for name in NAME_PATTERNS_POINTING_TO_LOCALSTACK :
@@ -904,7 +924,7 @@ def stop_servers():
904
924
905
925
def start_dns_server_as_sudo (port : int ):
906
926
global DNS_SERVER
907
- LOG .debug (
927
+ LOG .info (
908
928
"Starting the DNS on its privileged port (%s) needs root permissions. Trying to start DNS with sudo." ,
909
929
config .DNS_PORT ,
910
930
)
@@ -929,7 +949,7 @@ def start_dns_server(port: int, asynchronous: bool = False, standalone: bool = F
929
949
930
950
# check if DNS server is disabled
931
951
if not config .use_custom_dns ():
932
- LOG .debug ("Not starting DNS. DNS_ADDRESS=%s" , config .DNS_ADDRESS )
952
+ LOG .info ("Not starting DNS. DNS_ADDRESS=%s" , config .DNS_ADDRESS )
933
953
return
934
954
935
955
upstream_dns = get_fallback_dns_server ()
@@ -949,7 +969,7 @@ def start_dns_server(port: int, asynchronous: bool = False, standalone: bool = F
949
969
return
950
970
951
971
if standalone :
952
- LOG .debug ("Already in standalone mode and port binding still fails." )
972
+ LOG .warning ("Already in standalone mode and port binding still fails." )
953
973
return
954
974
955
975
start_dns_server_as_sudo (port )
0 commit comments