dnskit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dnskit/__init__.py +9 -0
- dnskit/__main__.py +5 -0
- dnskit/cli/__init__.py +1 -0
- dnskit/cli/app.py +955 -0
- dnskit/cli/render.py +1334 -0
- dnskit/core/__init__.py +349 -0
- dnskit/core/adapters.py +521 -0
- dnskit/core/batch.py +211 -0
- dnskit/core/compare.py +168 -0
- dnskit/core/config.py +248 -0
- dnskit/core/connectivity.py +139 -0
- dnskit/core/diagnosis.py +100 -0
- dnskit/core/dnssec.py +767 -0
- dnskit/core/doctor.py +836 -0
- dnskit/core/integrity.py +232 -0
- dnskit/core/local.py +725 -0
- dnskit/core/lookup.py +218 -0
- dnskit/core/mail.py +1346 -0
- dnskit/core/nameservers.py +223 -0
- dnskit/core/outside.py +113 -0
- dnskit/core/propagation.py +277 -0
- dnskit/core/reach.py +255 -0
- dnskit/core/reverse.py +61 -0
- dnskit/core/serialize.py +64 -0
- dnskit/core/trace.py +591 -0
- dnskit/core/watch.py +167 -0
- dnskit/py.typed +0 -0
- dnskit-0.1.0.dist-info/METADATA +362 -0
- dnskit-0.1.0.dist-info/RECORD +32 -0
- dnskit-0.1.0.dist-info/WHEEL +4 -0
- dnskit-0.1.0.dist-info/entry_points.txt +2 -0
- dnskit-0.1.0.dist-info/licenses/LICENSE +21 -0
dnskit/core/trace.py
ADDED
|
@@ -0,0 +1,591 @@
|
|
|
1
|
+
"""Follow a name's delegation from the root servers down, like `dig +trace`.
|
|
2
|
+
|
|
3
|
+
Each level's nameservers are asked directly, without recursion: the root servers refer to
|
|
4
|
+
the top-level domain's servers, those refer to the domain's own nameservers, and those give
|
|
5
|
+
the answer. When direct queries are blocked (a VPN or firewall that allows DNS only to the
|
|
6
|
+
network's own server), the delegation is followed through that resolver instead.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import time
|
|
10
|
+
from collections.abc import Sequence
|
|
11
|
+
from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait
|
|
12
|
+
from dataclasses import dataclass, replace
|
|
13
|
+
from enum import StrEnum
|
|
14
|
+
|
|
15
|
+
import dns.exception
|
|
16
|
+
import dns.flags
|
|
17
|
+
import dns.message
|
|
18
|
+
import dns.name
|
|
19
|
+
import dns.query
|
|
20
|
+
import dns.rcode
|
|
21
|
+
import dns.rdatatype
|
|
22
|
+
|
|
23
|
+
from dnskit.core.diagnosis import Diagnosis
|
|
24
|
+
from dnskit.core.lookup import LookupStatus, _is_no_route, _query_one, _validate_rtype
|
|
25
|
+
from dnskit.core.nameservers import system_nameservers
|
|
26
|
+
from dnskit.core.outside import _DENIED_ERRNOS, DnsBlocking, blocked_diagnosis, check_blocking
|
|
27
|
+
from dnskit.core.reach import parse_target
|
|
28
|
+
|
|
29
|
+
# The 13 root server identities (IANA root hints), as (name, IPv4 address).
|
|
30
|
+
ROOT_SERVERS: tuple[tuple[str, str], ...] = (
|
|
31
|
+
("a.root-servers.net.", "198.41.0.4"),
|
|
32
|
+
("b.root-servers.net.", "170.247.170.2"),
|
|
33
|
+
("c.root-servers.net.", "192.33.4.12"),
|
|
34
|
+
("d.root-servers.net.", "199.7.91.13"),
|
|
35
|
+
("e.root-servers.net.", "192.203.230.10"),
|
|
36
|
+
("f.root-servers.net.", "192.5.5.241"),
|
|
37
|
+
("g.root-servers.net.", "192.112.36.4"),
|
|
38
|
+
("h.root-servers.net.", "198.97.190.53"),
|
|
39
|
+
("i.root-servers.net.", "192.36.148.17"),
|
|
40
|
+
("j.root-servers.net.", "192.58.128.30"),
|
|
41
|
+
("k.root-servers.net.", "193.0.14.129"),
|
|
42
|
+
("l.root-servers.net.", "199.7.83.42"),
|
|
43
|
+
("m.root-servers.net.", "202.12.27.33"),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# Per query. Each level's servers are asked at once, so a dead level costs one timeout.
|
|
47
|
+
TRACE_TIMEOUT_S = 2.0
|
|
48
|
+
# How many of a level's nameservers are asked at once; the first usable reply is used.
|
|
49
|
+
SERVERS_PER_LEVEL = 3
|
|
50
|
+
# Real delegations are a handful of levels deep; more means a referral loop.
|
|
51
|
+
MAX_STEPS = 16
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class StepStatus(StrEnum):
|
|
55
|
+
REFERRAL = "referral" # pointed to the next level's nameservers
|
|
56
|
+
ANSWER = "answer" # gave the records (or a CNAME)
|
|
57
|
+
NXDOMAIN = "nxdomain" # the name doesn't exist
|
|
58
|
+
NO_ANSWER = "no_answer" # the name exists but has no records of this type
|
|
59
|
+
FAILED = "failed" # no server at this level gave a usable reply
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class FailReason(StrEnum):
|
|
63
|
+
NO_RESPONSE = "no_response" # timed out, no route, or blocked before leaving
|
|
64
|
+
REFUSED = "refused"
|
|
65
|
+
SERVFAIL = "servfail"
|
|
66
|
+
LAME = "lame" # answered, but with neither the records nor a referral
|
|
67
|
+
NO_ADDRESS = "no_address" # the next level's nameservers have no address
|
|
68
|
+
TOO_DEEP = "too_deep" # referral loop
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class TraceMode(StrEnum):
|
|
72
|
+
DIRECT = "direct" # each level's servers were asked directly, from the root down
|
|
73
|
+
VIA_RESOLVER = "via_resolver" # direct queries blocked: asked a resolver about each level
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(frozen=True)
|
|
77
|
+
class Server:
|
|
78
|
+
name: str # e.g. "a.root-servers.net."; the address again for a resolver
|
|
79
|
+
address: str
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@dataclass(frozen=True)
|
|
83
|
+
class ServerFailure:
|
|
84
|
+
server: Server
|
|
85
|
+
reason: FailReason
|
|
86
|
+
error: str # in words, e.g. "timed out", "REFUSED"
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass(frozen=True)
|
|
90
|
+
class TraceStep:
|
|
91
|
+
zone: str # the zone whose nameservers were asked, "." for the root
|
|
92
|
+
status: StepStatus
|
|
93
|
+
server: Server | None = None # who gave the reply; None when the step failed
|
|
94
|
+
latency_ms: float | None = None
|
|
95
|
+
referral_zone: str | None = None # REFERRAL: the next zone down
|
|
96
|
+
referral_servers: tuple[str, ...] = () # REFERRAL: its nameserver names
|
|
97
|
+
records: tuple[str, ...] = () # ANSWER: the values, e.g. "93.184.215.14", "CNAME x."
|
|
98
|
+
failures: tuple[ServerFailure, ...] = () # servers at this level that didn't help
|
|
99
|
+
reason: FailReason | None = None # FAILED: why, summed up over `failures`
|
|
100
|
+
note: str = "" # e.g. that the nameservers' addresses had to be looked up
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
@dataclass(frozen=True)
|
|
104
|
+
class TraceReport:
|
|
105
|
+
name: str # fully qualified, with the trailing dot
|
|
106
|
+
rtype: str
|
|
107
|
+
mode: TraceMode
|
|
108
|
+
steps: tuple[TraceStep, ...]
|
|
109
|
+
elapsed_ms: float
|
|
110
|
+
timeout: float
|
|
111
|
+
# Set when the root servers didn't respond: why (VPN, firewall, offline).
|
|
112
|
+
blocking: DnsBlocking | None = None
|
|
113
|
+
resolver: str | None = None # VIA_RESOLVER: the resolver that was asked
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def final(self) -> TraceStep:
|
|
117
|
+
return self.steps[-1]
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def succeeded(self) -> bool:
|
|
121
|
+
return self.final.status is StepStatus.ANSWER
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def trace(target: str, rtype: str = "A", timeout: float = TRACE_TIMEOUT_S) -> TraceReport:
|
|
125
|
+
"""Follow `target`'s delegation from the root servers down to its records.
|
|
126
|
+
|
|
127
|
+
An invalid name or record type raises ValueError; everything else is reported on the
|
|
128
|
+
steps. If no root server responds, `check_blocking` finds out why, and when this
|
|
129
|
+
computer's own DNS works the trace is redone through it (TraceMode.VIA_RESOLVER).
|
|
130
|
+
"""
|
|
131
|
+
name = dns.name.from_text(parse_target(target)).to_text()
|
|
132
|
+
rtype = _validate_rtype(rtype)
|
|
133
|
+
start = time.perf_counter()
|
|
134
|
+
|
|
135
|
+
steps = _trace_direct(name, rtype, timeout)
|
|
136
|
+
blocking = None
|
|
137
|
+
first = steps[0]
|
|
138
|
+
if first.status is StepStatus.FAILED and first.reason is FailReason.NO_RESPONSE:
|
|
139
|
+
blocking = check_blocking()
|
|
140
|
+
if blocking.system_works:
|
|
141
|
+
resolver = blocking.working_nameservers[0]
|
|
142
|
+
return TraceReport(
|
|
143
|
+
name,
|
|
144
|
+
rtype,
|
|
145
|
+
TraceMode.VIA_RESOLVER,
|
|
146
|
+
tuple(_trace_via_resolver(name, rtype, resolver, timeout)),
|
|
147
|
+
elapsed_ms=(time.perf_counter() - start) * 1000,
|
|
148
|
+
timeout=timeout,
|
|
149
|
+
blocking=blocking,
|
|
150
|
+
resolver=resolver,
|
|
151
|
+
)
|
|
152
|
+
return TraceReport(
|
|
153
|
+
name,
|
|
154
|
+
rtype,
|
|
155
|
+
TraceMode.DIRECT,
|
|
156
|
+
tuple(steps),
|
|
157
|
+
elapsed_ms=(time.perf_counter() - start) * 1000,
|
|
158
|
+
timeout=timeout,
|
|
159
|
+
blocking=blocking,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
# --- Asking one server --------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@dataclass(frozen=True)
|
|
167
|
+
class _Reply:
|
|
168
|
+
server: Server
|
|
169
|
+
response: dns.message.Message | None
|
|
170
|
+
latency_ms: float
|
|
171
|
+
failure: ServerFailure | None = None # set when there's no response
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _send(query: dns.message.Message, address: str, timeout: float) -> dns.message.Message:
|
|
175
|
+
"""One query to one server, over UDP, retried over TCP if the reply was truncated."""
|
|
176
|
+
response, _ = dns.query.udp_with_fallback(query, address, timeout=timeout)
|
|
177
|
+
return response
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _ask(server: Server, name: str, rtype: str, timeout: float, recursive: bool) -> _Reply:
|
|
181
|
+
query = dns.message.make_query(name, rtype, use_edns=0, payload=1232)
|
|
182
|
+
if not recursive:
|
|
183
|
+
query.flags &= ~dns.flags.RD
|
|
184
|
+
start = time.perf_counter()
|
|
185
|
+
|
|
186
|
+
def failed(error: str) -> _Reply:
|
|
187
|
+
latency = (time.perf_counter() - start) * 1000
|
|
188
|
+
return _Reply(server, None, latency, ServerFailure(server, FailReason.NO_RESPONSE, error))
|
|
189
|
+
|
|
190
|
+
try:
|
|
191
|
+
response = _send(query, server.address, timeout)
|
|
192
|
+
except dns.exception.Timeout:
|
|
193
|
+
return failed("timed out")
|
|
194
|
+
except OSError as exc:
|
|
195
|
+
if exc.errno in _DENIED_ERRNOS or getattr(exc, "winerror", None) in _DENIED_ERRNOS:
|
|
196
|
+
return failed("blocked by a firewall on this computer")
|
|
197
|
+
return failed("no route to it" if _is_no_route(exc) else str(exc))
|
|
198
|
+
except dns.exception.DNSException as exc:
|
|
199
|
+
return failed(str(exc) or type(exc).__name__)
|
|
200
|
+
return _Reply(server, response, (time.perf_counter() - start) * 1000)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
# --- Direct trace -------------------------------------------------------------------------
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _trace_direct(name: str, rtype: str, timeout: float) -> list[TraceStep]:
|
|
207
|
+
steps: list[TraceStep] = []
|
|
208
|
+
zone = "."
|
|
209
|
+
servers = [Server(n, a) for n, a in ROOT_SERVERS]
|
|
210
|
+
for _ in range(MAX_STEPS):
|
|
211
|
+
step, response = _ask_level(zone, servers, name, rtype, timeout)
|
|
212
|
+
steps.append(step)
|
|
213
|
+
if step.status is not StepStatus.REFERRAL or response is None:
|
|
214
|
+
return steps
|
|
215
|
+
servers, note = _next_servers(step, response, timeout)
|
|
216
|
+
zone = step.referral_zone or zone
|
|
217
|
+
if not servers:
|
|
218
|
+
names = ", ".join(step.referral_servers)
|
|
219
|
+
steps.append(
|
|
220
|
+
TraceStep(
|
|
221
|
+
zone,
|
|
222
|
+
StepStatus.FAILED,
|
|
223
|
+
reason=FailReason.NO_ADDRESS,
|
|
224
|
+
note=f"none of its nameservers ({names}) has an address",
|
|
225
|
+
)
|
|
226
|
+
)
|
|
227
|
+
return steps
|
|
228
|
+
if note:
|
|
229
|
+
steps[-1] = replace(step, note=note)
|
|
230
|
+
steps.append(TraceStep(zone, StepStatus.FAILED, reason=FailReason.TOO_DEEP))
|
|
231
|
+
return steps
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _ask_level(
|
|
235
|
+
zone: str, servers: Sequence[Server], name: str, rtype: str, timeout: float
|
|
236
|
+
) -> tuple[TraceStep, dns.message.Message | None]:
|
|
237
|
+
"""Ask a few of `zone`'s servers at once; the first usable reply makes the step."""
|
|
238
|
+
candidates = list(servers[:SERVERS_PER_LEVEL])
|
|
239
|
+
failures: list[ServerFailure] = []
|
|
240
|
+
pool = ThreadPoolExecutor(max_workers=len(candidates))
|
|
241
|
+
try:
|
|
242
|
+
pending = {pool.submit(_ask, s, name, rtype, timeout, False) for s in candidates}
|
|
243
|
+
while pending:
|
|
244
|
+
done, pending = wait(pending, return_when=FIRST_COMPLETED)
|
|
245
|
+
for future in done:
|
|
246
|
+
reply = future.result()
|
|
247
|
+
outcome = _interpret(zone, name, rtype, reply)
|
|
248
|
+
if isinstance(outcome, ServerFailure):
|
|
249
|
+
failures.append(outcome)
|
|
250
|
+
continue
|
|
251
|
+
return replace(outcome, failures=tuple(failures)), reply.response
|
|
252
|
+
finally:
|
|
253
|
+
pool.shutdown(wait=False, cancel_futures=True)
|
|
254
|
+
order = {s: i for i, s in enumerate(candidates)}
|
|
255
|
+
failures.sort(key=lambda f: order.get(f.server, 0))
|
|
256
|
+
return (
|
|
257
|
+
TraceStep(zone, StepStatus.FAILED, failures=tuple(failures), reason=_reason(failures)),
|
|
258
|
+
None,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _interpret(zone: str, name: str, rtype: str, reply: _Reply) -> TraceStep | ServerFailure:
|
|
263
|
+
"""What one server's reply means for the trace: a step, or why it was no use."""
|
|
264
|
+
if reply.response is None:
|
|
265
|
+
assert reply.failure is not None
|
|
266
|
+
return reply.failure
|
|
267
|
+
response = reply.response
|
|
268
|
+
server, latency = reply.server, reply.latency_ms
|
|
269
|
+
rcode = response.rcode()
|
|
270
|
+
if rcode == dns.rcode.NXDOMAIN:
|
|
271
|
+
return TraceStep(zone, StepStatus.NXDOMAIN, server, latency)
|
|
272
|
+
if rcode != dns.rcode.NOERROR:
|
|
273
|
+
text = dns.rcode.to_text(rcode)
|
|
274
|
+
reason = {"REFUSED": FailReason.REFUSED, "SERVFAIL": FailReason.SERVFAIL}.get(
|
|
275
|
+
text, FailReason.LAME
|
|
276
|
+
)
|
|
277
|
+
return ServerFailure(server, reason, text)
|
|
278
|
+
|
|
279
|
+
records = _answer_values(response, name, rtype)
|
|
280
|
+
if records:
|
|
281
|
+
return TraceStep(zone, StepStatus.ANSWER, server, latency, records=records)
|
|
282
|
+
|
|
283
|
+
qname = dns.name.from_text(name)
|
|
284
|
+
zone_name = dns.name.from_text(zone)
|
|
285
|
+
for rrset in response.authority:
|
|
286
|
+
if (
|
|
287
|
+
rrset.rdtype == dns.rdatatype.NS
|
|
288
|
+
and rrset.name != zone_name
|
|
289
|
+
and rrset.name.is_subdomain(zone_name)
|
|
290
|
+
and qname.is_subdomain(rrset.name)
|
|
291
|
+
):
|
|
292
|
+
return TraceStep(
|
|
293
|
+
zone,
|
|
294
|
+
StepStatus.REFERRAL,
|
|
295
|
+
server,
|
|
296
|
+
latency,
|
|
297
|
+
referral_zone=rrset.name.to_text(),
|
|
298
|
+
referral_servers=tuple(sorted(rdata.target.to_text() for rdata in rrset)),
|
|
299
|
+
)
|
|
300
|
+
authoritative = bool(response.flags & dns.flags.AA) or any(
|
|
301
|
+
rrset.rdtype == dns.rdatatype.SOA for rrset in response.authority
|
|
302
|
+
)
|
|
303
|
+
if authoritative:
|
|
304
|
+
return TraceStep(zone, StepStatus.NO_ANSWER, server, latency)
|
|
305
|
+
return ServerFailure(server, FailReason.LAME, "answered with neither records nor a referral")
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _answer_values(response: dns.message.Message, name: str, rtype: str) -> tuple[str, ...]:
|
|
309
|
+
"""The records answering `name`/`rtype`, plus any CNAMEs on the way."""
|
|
310
|
+
wanted = dns.rdatatype.from_text(rtype)
|
|
311
|
+
values = []
|
|
312
|
+
for rrset in response.answer:
|
|
313
|
+
if rrset.rdtype == wanted:
|
|
314
|
+
values += [rdata.to_text() for rdata in rrset]
|
|
315
|
+
elif rrset.rdtype == dns.rdatatype.CNAME:
|
|
316
|
+
values += [f"CNAME {rdata.target}" for rdata in rrset]
|
|
317
|
+
return tuple(values)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _next_servers(
|
|
321
|
+
step: TraceStep, response: dns.message.Message, timeout: float
|
|
322
|
+
) -> tuple[list[Server], str]:
|
|
323
|
+
"""The referred-to nameservers with IPv4 addresses: glue first, else looked up."""
|
|
324
|
+
glue: dict[str, list[str]] = {}
|
|
325
|
+
for rrset in response.additional:
|
|
326
|
+
if rrset.rdtype == dns.rdatatype.A:
|
|
327
|
+
glue.setdefault(rrset.name.to_text(), []).extend(r.address for r in rrset)
|
|
328
|
+
servers = [Server(ns, glue[ns][0]) for ns in step.referral_servers if glue.get(ns)]
|
|
329
|
+
if servers:
|
|
330
|
+
return servers, ""
|
|
331
|
+
|
|
332
|
+
# No glue (the nameservers are in another domain): look their addresses up normally.
|
|
333
|
+
names = list(step.referral_servers[:SERVERS_PER_LEVEL])
|
|
334
|
+
if not names:
|
|
335
|
+
return [], ""
|
|
336
|
+
with ThreadPoolExecutor(max_workers=len(names)) as pool:
|
|
337
|
+
addresses = list(pool.map(lambda ns: _lookup_address(ns, timeout), names))
|
|
338
|
+
servers = [Server(ns, address) for ns, address in zip(names, addresses, strict=True) if address]
|
|
339
|
+
return servers, "no glue: looked up the nameservers' addresses separately"
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def _lookup_address(ns: str, timeout: float) -> str | None:
|
|
343
|
+
"""`ns`'s first IPv4 address, through this computer's DNS servers."""
|
|
344
|
+
nameservers = system_nameservers()
|
|
345
|
+
if not nameservers:
|
|
346
|
+
return None
|
|
347
|
+
result = _query_one(ns, "A", nameservers, timeout)
|
|
348
|
+
if result.status is not LookupStatus.OK or not result.records:
|
|
349
|
+
return None
|
|
350
|
+
return result.records[0].value
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _reason(failures: Sequence[ServerFailure]) -> FailReason:
|
|
354
|
+
"""One reason for a whole level: the most telling one any server gave."""
|
|
355
|
+
priority = [FailReason.LAME, FailReason.REFUSED, FailReason.SERVFAIL, FailReason.NO_RESPONSE]
|
|
356
|
+
reasons = {f.reason for f in failures}
|
|
357
|
+
return next((r for r in priority if r in reasons), FailReason.NO_RESPONSE)
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
# --- Through a resolver -------------------------------------------------------------------
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
def _trace_via_resolver(name: str, rtype: str, resolver: str, timeout: float) -> list[TraceStep]:
|
|
364
|
+
"""Each level's nameservers as `resolver` sees them, then the records from it.
|
|
365
|
+
|
|
366
|
+
For every parent of `name` (com., example.com., ...) an NS answer for exactly that name
|
|
367
|
+
marks a delegation. This shows the chain, though not how each nameserver answers.
|
|
368
|
+
"""
|
|
369
|
+
server = Server(resolver, resolver)
|
|
370
|
+
qname = dns.name.from_text(name)
|
|
371
|
+
steps: list[TraceStep] = []
|
|
372
|
+
zone = "."
|
|
373
|
+
for depth in range(2, len(qname) + 1):
|
|
374
|
+
suffix = qname.split(depth)[1]
|
|
375
|
+
reply = _ask(server, suffix.to_text(), "NS", timeout, recursive=True)
|
|
376
|
+
if reply.response is None:
|
|
377
|
+
assert reply.failure is not None
|
|
378
|
+
steps.append(_failed_via(zone, reply.failure))
|
|
379
|
+
return steps
|
|
380
|
+
rcode = reply.response.rcode()
|
|
381
|
+
if rcode == dns.rcode.NXDOMAIN:
|
|
382
|
+
steps.append(TraceStep(zone, StepStatus.NXDOMAIN, server, reply.latency_ms))
|
|
383
|
+
return steps
|
|
384
|
+
if rcode != dns.rcode.NOERROR:
|
|
385
|
+
text = dns.rcode.to_text(rcode)
|
|
386
|
+
reason = FailReason.SERVFAIL if text == "SERVFAIL" else FailReason.REFUSED
|
|
387
|
+
steps.append(_failed_via(zone, ServerFailure(server, reason, text)))
|
|
388
|
+
return steps
|
|
389
|
+
answer = reply.response.answer
|
|
390
|
+
ns = next((r for r in answer if r.rdtype == dns.rdatatype.NS and r.name == suffix), None)
|
|
391
|
+
if ns is not None:
|
|
392
|
+
steps.append(
|
|
393
|
+
TraceStep(
|
|
394
|
+
zone,
|
|
395
|
+
StepStatus.REFERRAL,
|
|
396
|
+
server,
|
|
397
|
+
reply.latency_ms,
|
|
398
|
+
referral_zone=suffix.to_text(),
|
|
399
|
+
referral_servers=tuple(sorted(r.target.to_text() for r in ns)),
|
|
400
|
+
)
|
|
401
|
+
)
|
|
402
|
+
zone = suffix.to_text()
|
|
403
|
+
elif any(r.rdtype == dns.rdatatype.CNAME and r.name == suffix for r in answer):
|
|
404
|
+
break # an alias: the final query shows where it points
|
|
405
|
+
|
|
406
|
+
reply = _ask(server, name, rtype, timeout, recursive=True)
|
|
407
|
+
if reply.response is None:
|
|
408
|
+
assert reply.failure is not None
|
|
409
|
+
steps.append(_failed_via(zone, reply.failure))
|
|
410
|
+
return steps
|
|
411
|
+
rcode = reply.response.rcode()
|
|
412
|
+
if rcode == dns.rcode.NXDOMAIN:
|
|
413
|
+
steps.append(TraceStep(zone, StepStatus.NXDOMAIN, server, reply.latency_ms))
|
|
414
|
+
elif rcode != dns.rcode.NOERROR:
|
|
415
|
+
text = dns.rcode.to_text(rcode)
|
|
416
|
+
reason = FailReason.SERVFAIL if text == "SERVFAIL" else FailReason.REFUSED
|
|
417
|
+
steps.append(_failed_via(zone, ServerFailure(server, reason, text)))
|
|
418
|
+
elif records := _answer_values(reply.response, name, rtype):
|
|
419
|
+
steps.append(TraceStep(zone, StepStatus.ANSWER, server, reply.latency_ms, records=records))
|
|
420
|
+
else:
|
|
421
|
+
steps.append(TraceStep(zone, StepStatus.NO_ANSWER, server, reply.latency_ms))
|
|
422
|
+
return steps
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def _failed_via(zone: str, failure: ServerFailure) -> TraceStep:
|
|
426
|
+
return TraceStep(zone, StepStatus.FAILED, failures=(failure,), reason=failure.reason)
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
# --- Explanation --------------------------------------------------------------------------
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
def level_name(zone: str) -> str:
|
|
433
|
+
"""How to refer to a level of the DNS tree in a sentence."""
|
|
434
|
+
if zone == ".":
|
|
435
|
+
return "the root"
|
|
436
|
+
labels = zone.rstrip(".").split(".")
|
|
437
|
+
if len(labels) == 1:
|
|
438
|
+
return f"the .{labels[0]} top-level domain"
|
|
439
|
+
return zone.rstrip(".")
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def _sentence_start(level: str) -> str:
|
|
443
|
+
"""`level` at the start of a sentence: "The root", but a domain name stays lowercase."""
|
|
444
|
+
return "T" + level[1:] if level.startswith("the ") else level
|
|
445
|
+
|
|
446
|
+
|
|
447
|
+
def explain_trace(report: TraceReport) -> Diagnosis | None:
|
|
448
|
+
"""Where the trace ended, in plain English. None when it reached the records.
|
|
449
|
+
|
|
450
|
+
The blocked-queries explanation for a trace done through a resolver is separate
|
|
451
|
+
(`trace_blocked_diagnosis`), since that trace can still succeed.
|
|
452
|
+
"""
|
|
453
|
+
step = report.final
|
|
454
|
+
name = report.name.rstrip(".")
|
|
455
|
+
zone = step.zone.rstrip(".") or "."
|
|
456
|
+
asked = (
|
|
457
|
+
f"the resolver {report.resolver}"
|
|
458
|
+
if report.mode is TraceMode.VIA_RESOLVER
|
|
459
|
+
else f"{zone}'s nameserver {step.server.name.rstrip('.')}"
|
|
460
|
+
if step.server
|
|
461
|
+
else f"{zone}'s nameservers"
|
|
462
|
+
)
|
|
463
|
+
match step.status:
|
|
464
|
+
case StepStatus.ANSWER | StepStatus.REFERRAL:
|
|
465
|
+
return None
|
|
466
|
+
case StepStatus.NXDOMAIN:
|
|
467
|
+
return Diagnosis(
|
|
468
|
+
title="The name doesn't exist",
|
|
469
|
+
explanation=f"{_sentence_start(asked)} answered that {name} doesn't "
|
|
470
|
+
"exist (NXDOMAIN)."
|
|
471
|
+
+ (
|
|
472
|
+
" It's the authority for that part of DNS, so this is the final word, "
|
|
473
|
+
"not a caching problem."
|
|
474
|
+
if report.mode is TraceMode.DIRECT
|
|
475
|
+
else ""
|
|
476
|
+
),
|
|
477
|
+
suggestion=_nxdomain_suggestion(step.zone, name),
|
|
478
|
+
)
|
|
479
|
+
case StepStatus.NO_ANSWER:
|
|
480
|
+
return Diagnosis(
|
|
481
|
+
title=f"No {report.rtype} records",
|
|
482
|
+
explanation=f"{name} exists, but has no {report.rtype} records ({asked} "
|
|
483
|
+
"answered with none).",
|
|
484
|
+
suggestion=f"If you expected one, add it at the DNS host for {zone}. "
|
|
485
|
+
f"`dnskit lookup {name}` shows which record types it has.",
|
|
486
|
+
)
|
|
487
|
+
return _failed_diagnosis(report, step)
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def _nxdomain_suggestion(zone: str, name: str) -> str:
|
|
491
|
+
"""What to check, depending on which level said the name doesn't exist."""
|
|
492
|
+
if zone == ".":
|
|
493
|
+
tld = name.rsplit(".", 1)[-1]
|
|
494
|
+
return f"Check the spelling: .{tld} isn't a top-level domain that exists."
|
|
495
|
+
if len(zone.rstrip(".").split(".")) == 1:
|
|
496
|
+
return (
|
|
497
|
+
f"Check the spelling. The domain isn't registered: {level_name(zone)} has no "
|
|
498
|
+
"record of it. If it should be, check its registration with a WHOIS lookup."
|
|
499
|
+
)
|
|
500
|
+
return (
|
|
501
|
+
"Check the spelling. If you just created the record, check that it's saved at the DNS "
|
|
502
|
+
f"host for {zone.rstrip('.')}."
|
|
503
|
+
)
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
def trace_blocked_diagnosis(report: TraceReport) -> Diagnosis | None:
|
|
507
|
+
"""Why the root servers didn't answer, when that was checked."""
|
|
508
|
+
if report.blocking is None:
|
|
509
|
+
return None
|
|
510
|
+
alternative = ""
|
|
511
|
+
if report.mode is TraceMode.VIA_RESOLVER:
|
|
512
|
+
alternative = (
|
|
513
|
+
f"Meanwhile, the trace shown was done through {report.resolver} instead: it shows "
|
|
514
|
+
"each level's nameservers, but not how each of them answers or how fast."
|
|
515
|
+
)
|
|
516
|
+
return blocked_diagnosis(report.blocking, "the root servers", alternative)
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
def _failed_diagnosis(report: TraceReport, step: TraceStep) -> Diagnosis | None:
|
|
520
|
+
zone = step.zone.rstrip(".") or "."
|
|
521
|
+
level = level_name(step.zone)
|
|
522
|
+
index = report.steps.index(step)
|
|
523
|
+
parent = level_name(report.steps[index - 1].zone) if index else None
|
|
524
|
+
servers = "; ".join(f"{f.server.name.rstrip('.')} {f.error}" for f in step.failures)
|
|
525
|
+
detail = f" ({servers})" if servers else ""
|
|
526
|
+
|
|
527
|
+
if step.zone == "." and report.blocking is not None and report.mode is TraceMode.DIRECT:
|
|
528
|
+
return trace_blocked_diagnosis(report)
|
|
529
|
+
if report.mode is TraceMode.VIA_RESOLVER:
|
|
530
|
+
return Diagnosis(
|
|
531
|
+
title=f"The resolver couldn't follow {level}",
|
|
532
|
+
explanation=f"{report.resolver} failed while asking about {level}{detail}.",
|
|
533
|
+
suggestion="Try again in a minute. If it keeps failing, the domain's own DNS may "
|
|
534
|
+
"be broken: disconnect the VPN or change networks and run the trace directly.",
|
|
535
|
+
)
|
|
536
|
+
match step.reason:
|
|
537
|
+
case FailReason.NO_ADDRESS:
|
|
538
|
+
return Diagnosis(
|
|
539
|
+
title=f"{_sentence_start(level)}'s nameservers have no address",
|
|
540
|
+
explanation=f"{parent} says {zone} is served by "
|
|
541
|
+
f"{', '.join(n.rstrip('.') for n in report.steps[index - 1].referral_servers)}, "
|
|
542
|
+
"but none of those names could be looked up, so nobody can reach them.",
|
|
543
|
+
suggestion=f"The nameservers registered for {zone} at its registrar are "
|
|
544
|
+
"misspelled or no longer exist. Correct them at the registrar.",
|
|
545
|
+
)
|
|
546
|
+
case FailReason.TOO_DEEP:
|
|
547
|
+
return Diagnosis(
|
|
548
|
+
title="Too many referrals",
|
|
549
|
+
explanation=f"After {MAX_STEPS} levels the nameservers were still referring "
|
|
550
|
+
"the query onwards, which means the delegation loops.",
|
|
551
|
+
suggestion=f"Check the NS records of {zone} and its parent zones at the DNS "
|
|
552
|
+
"host and registrar.",
|
|
553
|
+
)
|
|
554
|
+
case FailReason.REFUSED | FailReason.LAME:
|
|
555
|
+
return Diagnosis(
|
|
556
|
+
title=f"{zone}'s nameservers aren't serving it (lame delegation)",
|
|
557
|
+
explanation=f"{parent} says {zone} is served by these nameservers, but they "
|
|
558
|
+
f"don't answer for it{detail}. This usually happens after moving DNS to a new "
|
|
559
|
+
"host while the registrar still lists the old nameservers. Everyone looking "
|
|
560
|
+
f"up {report.name.rstrip('.')} gets errors, not just you.",
|
|
561
|
+
suggestion=f"Make the nameservers listed for {zone} at the registrar match the "
|
|
562
|
+
"DNS host that actually has the zone.",
|
|
563
|
+
)
|
|
564
|
+
case FailReason.SERVFAIL:
|
|
565
|
+
return Diagnosis(
|
|
566
|
+
title=f"{zone}'s nameservers returned a server failure",
|
|
567
|
+
explanation=f"The nameservers for {zone} replied with SERVFAIL{detail}: they "
|
|
568
|
+
"have a problem loading or serving the zone.",
|
|
569
|
+
suggestion=f"Contact the DNS host for {zone}; the problem is on their side.",
|
|
570
|
+
)
|
|
571
|
+
within = f" within {report.timeout:g} s"
|
|
572
|
+
if step.zone == ".":
|
|
573
|
+
return Diagnosis(
|
|
574
|
+
title="The root servers didn't respond",
|
|
575
|
+
explanation=f"None of the root servers asked replied{within}{detail}.",
|
|
576
|
+
suggestion="Check your connection and try again.",
|
|
577
|
+
)
|
|
578
|
+
where = (
|
|
579
|
+
"This is usually a brief network problem, since top-level domain servers are very reliable."
|
|
580
|
+
if len(zone.split(".")) == 1
|
|
581
|
+
else f"The levels above worked, so your connection is fine: {zone}'s own nameservers "
|
|
582
|
+
"are down or unreachable, or a firewall in front of them drops queries. Everyone "
|
|
583
|
+
f"looking up {report.name.rstrip('.')} has the same problem."
|
|
584
|
+
)
|
|
585
|
+
return Diagnosis(
|
|
586
|
+
title=f"{_sentence_start(level)}'s nameservers didn't respond",
|
|
587
|
+
explanation=f"None of the nameservers for {zone} that were asked replied{within}"
|
|
588
|
+
f"{detail}. {where}",
|
|
589
|
+
suggestion=f"Try again in a few minutes. If it keeps failing, contact the owner of "
|
|
590
|
+
f"{zone} or their DNS provider.",
|
|
591
|
+
)
|