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/cli/app.py
ADDED
|
@@ -0,0 +1,955 @@
|
|
|
1
|
+
"""Typer application: argument parsing and wiring between core and render."""
|
|
2
|
+
|
|
3
|
+
import codecs
|
|
4
|
+
import errno
|
|
5
|
+
import io
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Annotated, Any, NoReturn
|
|
10
|
+
|
|
11
|
+
import typer
|
|
12
|
+
from rich.console import Console
|
|
13
|
+
from rich.live import Live
|
|
14
|
+
from rich.markup import escape
|
|
15
|
+
|
|
16
|
+
from dnskit import __version__
|
|
17
|
+
from dnskit.cli.render import (
|
|
18
|
+
BatchProgress,
|
|
19
|
+
DoctorProgress,
|
|
20
|
+
render_batch,
|
|
21
|
+
render_compare,
|
|
22
|
+
render_config,
|
|
23
|
+
render_dnssec,
|
|
24
|
+
render_doctor,
|
|
25
|
+
render_email,
|
|
26
|
+
render_local,
|
|
27
|
+
render_probes,
|
|
28
|
+
render_propagation,
|
|
29
|
+
render_reach,
|
|
30
|
+
render_report,
|
|
31
|
+
render_reverse,
|
|
32
|
+
render_trace,
|
|
33
|
+
render_watch_end,
|
|
34
|
+
render_watch_sample,
|
|
35
|
+
render_watch_start,
|
|
36
|
+
)
|
|
37
|
+
from dnskit.core import (
|
|
38
|
+
COMPARE_TIMEOUT_S,
|
|
39
|
+
DEFAULT_PARALLEL,
|
|
40
|
+
DEFAULT_PORT,
|
|
41
|
+
DEFAULT_RTYPES,
|
|
42
|
+
DKIM_SELECTORS,
|
|
43
|
+
DOCTOR_TIMEOUT_S,
|
|
44
|
+
MAX_PARALLEL,
|
|
45
|
+
PROBE_TIMEOUT_S,
|
|
46
|
+
PROPAGATION_RESOLVERS,
|
|
47
|
+
PROPAGATION_TIMEOUT_S,
|
|
48
|
+
PUBLIC_RESOLVERS,
|
|
49
|
+
TRACE_TIMEOUT_S,
|
|
50
|
+
WATCH_INTERVAL_S,
|
|
51
|
+
BatchCheck,
|
|
52
|
+
Config,
|
|
53
|
+
ConfigError,
|
|
54
|
+
Diagnosis,
|
|
55
|
+
DnssecStatus,
|
|
56
|
+
LookupStatus,
|
|
57
|
+
ReachVerdict,
|
|
58
|
+
WatchSample,
|
|
59
|
+
cache_path,
|
|
60
|
+
choose_nameserver,
|
|
61
|
+
classify_nameservers,
|
|
62
|
+
compare,
|
|
63
|
+
config_path,
|
|
64
|
+
dnssec_check,
|
|
65
|
+
doctor,
|
|
66
|
+
doctor_verdict,
|
|
67
|
+
email_check,
|
|
68
|
+
explain,
|
|
69
|
+
explain_dnssec,
|
|
70
|
+
explain_propagation,
|
|
71
|
+
explain_reach,
|
|
72
|
+
explain_reverse,
|
|
73
|
+
explain_trace,
|
|
74
|
+
load_config,
|
|
75
|
+
local_check,
|
|
76
|
+
lookup,
|
|
77
|
+
parse_target,
|
|
78
|
+
propagation,
|
|
79
|
+
reach,
|
|
80
|
+
read_domains,
|
|
81
|
+
reverse_lookup,
|
|
82
|
+
run_batch,
|
|
83
|
+
to_data,
|
|
84
|
+
trace,
|
|
85
|
+
watch,
|
|
86
|
+
watch_summary,
|
|
87
|
+
write_template,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
_WINDOWS = sys.platform == "win32"
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class PipeSafeConsole(Console):
|
|
94
|
+
"""A Console that exits quietly when the reader closes the pipe on Windows too.
|
|
95
|
+
|
|
96
|
+
Rich handles `BrokenPipeError` (e.g. `dnskit compare example.com | head -3` on Linux or
|
|
97
|
+
macOS) by sending the rest of stdout to devnull and exiting with code 1. On Windows a
|
|
98
|
+
closed pipe raises `OSError(EINVAL)` instead, which Rich doesn't catch, so it printed a
|
|
99
|
+
traceback and "Exception ignored on flushing sys.stdout". Treat it as a broken pipe.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
def _check_buffer(self) -> None:
|
|
103
|
+
try:
|
|
104
|
+
super()._check_buffer()
|
|
105
|
+
except OSError as exc:
|
|
106
|
+
if _WINDOWS and exc.errno == errno.EINVAL:
|
|
107
|
+
self.on_broken_pipe()
|
|
108
|
+
raise
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
GUIDE_URL = "https://github.com/gladkhumalo/dnskit/blob/main/docs/USER_GUIDE.md"
|
|
112
|
+
|
|
113
|
+
app = typer.Typer(
|
|
114
|
+
help="DNS troubleshooting toolkit: why a name doesn't resolve, a site won't load or email "
|
|
115
|
+
"doesn't arrive, and whether the problem is this computer, its network or the domain.\n\n"
|
|
116
|
+
"Not sure where to start? Run [bold]dnskit doctor example.com[/bold]. Every command "
|
|
117
|
+
"explains what it finds in plain English, takes --json for scripts, and reads its defaults "
|
|
118
|
+
"from a config file (see [bold]dnskit config[/bold]). Each command's --help has examples.",
|
|
119
|
+
epilog=f"Full guide: {GUIDE_URL}",
|
|
120
|
+
no_args_is_help=True,
|
|
121
|
+
rich_markup_mode="rich",
|
|
122
|
+
)
|
|
123
|
+
console = PipeSafeConsole()
|
|
124
|
+
err_console = PipeSafeConsole(stderr=True)
|
|
125
|
+
|
|
126
|
+
ServerOption = Annotated[
|
|
127
|
+
str | None,
|
|
128
|
+
typer.Option(
|
|
129
|
+
"--server",
|
|
130
|
+
"-s",
|
|
131
|
+
help="IP address of the DNS server to ask, instead of the fastest of this computer's.",
|
|
132
|
+
),
|
|
133
|
+
]
|
|
134
|
+
TimeoutOption = Annotated[float, typer.Option(help="Seconds to wait for each answer.")]
|
|
135
|
+
RefreshOption = Annotated[
|
|
136
|
+
bool,
|
|
137
|
+
typer.Option(
|
|
138
|
+
"--refresh", help="Re-probe the system nameservers instead of using the cached choice."
|
|
139
|
+
),
|
|
140
|
+
]
|
|
141
|
+
JsonOption = Annotated[
|
|
142
|
+
bool, typer.Option("--json", help="Print the result as JSON (for scripts) instead.")
|
|
143
|
+
]
|
|
144
|
+
|
|
145
|
+
# The config file, read once per run by `main` (an empty one when there's no file).
|
|
146
|
+
_config: Config | None = None
|
|
147
|
+
|
|
148
|
+
# Setting names in the config file -> the commands' parameter names, where they differ.
|
|
149
|
+
_PARAMETER_NAMES = {"type": "rtype", "selectors": "selector", "json": "as_json"}
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def _epilog(examples: list[str], exit_codes: str) -> str:
|
|
153
|
+
"""The examples and exit codes shown at the end of a command's --help."""
|
|
154
|
+
shown = "\n\n".join(f" {example}" for example in examples)
|
|
155
|
+
return f"[bold]Examples[/bold]\n\n{shown}\n\n[bold]Exit codes:[/bold] {exit_codes}"
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def current_config() -> Config:
|
|
159
|
+
return _config if _config is not None else Config(config_path(), exists=False)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def run() -> None:
|
|
163
|
+
"""The `dnskit` program: the Typer app, plus a quiet exit when the reader goes away.
|
|
164
|
+
|
|
165
|
+
PipeSafeConsole covers dnskit's own output, but --help and Typer's error messages are
|
|
166
|
+
printed by Typer's console, so `dnskit --help | head -3` on Windows still raised
|
|
167
|
+
OSError(EINVAL) with a traceback. Catch it here, where every way in passes.
|
|
168
|
+
"""
|
|
169
|
+
try:
|
|
170
|
+
app(prog_name="dnskit")
|
|
171
|
+
except OSError as exc:
|
|
172
|
+
if _WINDOWS and exc.errno == errno.EINVAL:
|
|
173
|
+
console.on_broken_pipe() # stdout to devnull, exit 1, as Rich does elsewhere
|
|
174
|
+
raise
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _use_utf8_output() -> None:
|
|
178
|
+
"""Make stdout/stderr UTF-8 when they aren't already.
|
|
179
|
+
|
|
180
|
+
On Windows, redirected or piped output falls back to the ANSI code page (cp1252), which
|
|
181
|
+
can't encode ✓, ≠ or box-drawing characters, so Rich crashed with UnicodeEncodeError and
|
|
182
|
+
the exit code became 1. A real console is already UTF-8 and is left alone.
|
|
183
|
+
"""
|
|
184
|
+
for stream in (sys.stdout, sys.stderr):
|
|
185
|
+
if not isinstance(stream, io.TextIOWrapper):
|
|
186
|
+
continue
|
|
187
|
+
try:
|
|
188
|
+
if codecs.lookup(stream.encoding).name != "utf-8":
|
|
189
|
+
stream.reconfigure(encoding="utf-8", errors="replace")
|
|
190
|
+
except (LookupError, OSError, ValueError):
|
|
191
|
+
pass
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _version_callback(value: bool) -> None:
|
|
195
|
+
if value:
|
|
196
|
+
console.print(f"dnskit {__version__}")
|
|
197
|
+
raise typer.Exit()
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
@app.callback()
|
|
201
|
+
def main(
|
|
202
|
+
ctx: typer.Context,
|
|
203
|
+
version: Annotated[
|
|
204
|
+
bool,
|
|
205
|
+
typer.Option(
|
|
206
|
+
"--version", callback=_version_callback, is_eager=True, help="Show version and exit."
|
|
207
|
+
),
|
|
208
|
+
] = False,
|
|
209
|
+
) -> None:
|
|
210
|
+
"""DNS troubleshooting toolkit.
|
|
211
|
+
|
|
212
|
+
Default options can be set in a config file: run `dnskit config` to see where it is.
|
|
213
|
+
"""
|
|
214
|
+
global _config
|
|
215
|
+
_use_utf8_output()
|
|
216
|
+
if ctx.invoked_subcommand == "config":
|
|
217
|
+
return # `dnskit config` reports problems in the file itself
|
|
218
|
+
try:
|
|
219
|
+
_config = load_config()
|
|
220
|
+
except ConfigError as exc:
|
|
221
|
+
err_console.print(
|
|
222
|
+
f"[red]Error in the config file:[/red] {escape(str(exc))}", highlight=False
|
|
223
|
+
)
|
|
224
|
+
raise typer.Exit(2) from exc
|
|
225
|
+
# Click hands each command its section as option defaults; the command line still wins.
|
|
226
|
+
ctx.default_map = {
|
|
227
|
+
command: {_PARAMETER_NAMES.get(name, name): value for name, value in settings.items()}
|
|
228
|
+
for command, settings in _config.settings.items()
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _print_json(payload: dict[str, Any]) -> None:
|
|
233
|
+
typer.echo(json.dumps(to_data(payload), indent=2, ensure_ascii=False))
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def _print_json_line(payload: dict[str, Any]) -> None:
|
|
237
|
+
"""One compact object per line (JSON Lines), for output read as it arrives."""
|
|
238
|
+
typer.echo(json.dumps(to_data(payload), ensure_ascii=False))
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _emit_json(
|
|
242
|
+
command: str,
|
|
243
|
+
report: Any,
|
|
244
|
+
exit_code: int,
|
|
245
|
+
summary: dict[str, Any] | None = None,
|
|
246
|
+
diagnosis: Diagnosis | None = None,
|
|
247
|
+
) -> None:
|
|
248
|
+
"""The `--json` output: what was checked, the short answer, the explanation, everything."""
|
|
249
|
+
_print_json(
|
|
250
|
+
{
|
|
251
|
+
"command": command,
|
|
252
|
+
"dnskit_version": __version__,
|
|
253
|
+
"exit_code": exit_code,
|
|
254
|
+
"summary": summary or {},
|
|
255
|
+
"diagnosis": diagnosis,
|
|
256
|
+
"report": report,
|
|
257
|
+
}
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def _fail(command: str, message: str, as_json: bool, code: int = 2) -> NoReturn:
|
|
262
|
+
"""Invalid input or an unusable setup: say so (as JSON with --json) and exit."""
|
|
263
|
+
if as_json:
|
|
264
|
+
_print_json(
|
|
265
|
+
{
|
|
266
|
+
"command": command,
|
|
267
|
+
"dnskit_version": __version__,
|
|
268
|
+
"exit_code": code,
|
|
269
|
+
"error": message,
|
|
270
|
+
}
|
|
271
|
+
)
|
|
272
|
+
else:
|
|
273
|
+
err_console.print(f"[red]Error:[/red] {escape(message)}", highlight=False)
|
|
274
|
+
raise typer.Exit(code)
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _exit(code: int) -> None:
|
|
278
|
+
if code:
|
|
279
|
+
raise typer.Exit(code)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
@app.command(
|
|
283
|
+
"lookup",
|
|
284
|
+
epilog=_epilog(
|
|
285
|
+
[
|
|
286
|
+
"dnskit lookup example.com",
|
|
287
|
+
"dnskit lookup example.com -t MX",
|
|
288
|
+
"dnskit lookup example.com -s 1.1.1.1 --json",
|
|
289
|
+
],
|
|
290
|
+
"0 the name exists (even with some types empty), 1 the lookup failed or the name doesn't "
|
|
291
|
+
"exist, 2 invalid input.",
|
|
292
|
+
),
|
|
293
|
+
)
|
|
294
|
+
def lookup_command(
|
|
295
|
+
name: Annotated[str, typer.Argument(help="Domain name to query.")],
|
|
296
|
+
rtype: Annotated[
|
|
297
|
+
str | None,
|
|
298
|
+
typer.Option(
|
|
299
|
+
"--type",
|
|
300
|
+
"-t",
|
|
301
|
+
help=f"Single record type to query. Default: {', '.join(DEFAULT_RTYPES)}.",
|
|
302
|
+
),
|
|
303
|
+
] = None,
|
|
304
|
+
server: ServerOption = None,
|
|
305
|
+
timeout: TimeoutOption = 5.0,
|
|
306
|
+
refresh: RefreshOption = False,
|
|
307
|
+
as_json: JsonOption = False,
|
|
308
|
+
) -> None:
|
|
309
|
+
"""Look up DNS records for a name.
|
|
310
|
+
|
|
311
|
+
Queries A, AAAA, MX, CNAME, NS and TXT at once (or one --type) through the fastest of this
|
|
312
|
+
computer's DNS servers, and explains in plain English when a lookup fails.
|
|
313
|
+
"""
|
|
314
|
+
rtypes = [rtype] if rtype else list(DEFAULT_RTYPES)
|
|
315
|
+
try:
|
|
316
|
+
report = lookup(name, rtypes, nameserver=server, timeout=timeout, refresh=refresh)
|
|
317
|
+
except ValueError as exc:
|
|
318
|
+
_fail("lookup", str(exc), as_json)
|
|
319
|
+
|
|
320
|
+
exists = {LookupStatus.OK, LookupStatus.NO_ANSWER}
|
|
321
|
+
code = 0 if any(r.status in exists for r in report.results) else 1
|
|
322
|
+
if as_json:
|
|
323
|
+
failed = report.results[0] if code else None
|
|
324
|
+
diagnosis = explain(failed.status, name, failed.rtype, timeout) if failed else None
|
|
325
|
+
_emit_json("lookup", report, code, {"exists": code == 0}, diagnosis)
|
|
326
|
+
else:
|
|
327
|
+
render_report(report, console, timeout=timeout)
|
|
328
|
+
_exit(code)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
@app.command(
|
|
332
|
+
"reverse",
|
|
333
|
+
epilog=_epilog(
|
|
334
|
+
["dnskit reverse 8.8.8.8", "dnskit reverse 2001:4860:4860::8888"],
|
|
335
|
+
"0 a name was found, 1 none (or the lookup failed), 2 not an IP address.",
|
|
336
|
+
),
|
|
337
|
+
)
|
|
338
|
+
def reverse_command(
|
|
339
|
+
ip: Annotated[str, typer.Argument(help="IPv4 or IPv6 address to look up.")],
|
|
340
|
+
server: ServerOption = None,
|
|
341
|
+
timeout: TimeoutOption = 5.0,
|
|
342
|
+
refresh: RefreshOption = False,
|
|
343
|
+
as_json: JsonOption = False,
|
|
344
|
+
) -> None:
|
|
345
|
+
"""Find the hostname for an IP address (reverse DNS / PTR lookup).
|
|
346
|
+
|
|
347
|
+
Useful for an unknown address in a log, or to check a mail server's reverse name.
|
|
348
|
+
"""
|
|
349
|
+
try:
|
|
350
|
+
report = reverse_lookup(ip, nameserver=server, timeout=timeout, refresh=refresh)
|
|
351
|
+
except ValueError as exc:
|
|
352
|
+
_fail("reverse", str(exc), as_json)
|
|
353
|
+
|
|
354
|
+
code = 0 if report.result.status is LookupStatus.OK else 1
|
|
355
|
+
if as_json:
|
|
356
|
+
hostnames = [r.value for r in report.result.records]
|
|
357
|
+
diagnosis = explain_reverse(report.result.status, report.ip, timeout)
|
|
358
|
+
_emit_json("reverse", report, code, {"hostnames": hostnames}, diagnosis)
|
|
359
|
+
else:
|
|
360
|
+
render_reverse(report, console, timeout=timeout)
|
|
361
|
+
_exit(code)
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
_PUBLIC_NAMES = ", ".join(f"{label} ({ip})" for label, ip in PUBLIC_RESOLVERS)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
@app.command(
|
|
368
|
+
"compare",
|
|
369
|
+
epilog=_epilog(
|
|
370
|
+
["dnskit compare example.com", "dnskit compare example.com -t MX"],
|
|
371
|
+
"0 every resolver that answered agrees, 1 they disagree or none answered, 2 invalid input.",
|
|
372
|
+
),
|
|
373
|
+
help="Compare a record's answer across public and system resolvers.\n\n"
|
|
374
|
+
f"Queries {_PUBLIC_NAMES} (or the resolvers in the config file) and each of this "
|
|
375
|
+
"computer's nameservers in parallel, and highlights any resolver whose answer differs "
|
|
376
|
+
"from the majority.",
|
|
377
|
+
)
|
|
378
|
+
def compare_command(
|
|
379
|
+
name: Annotated[str, typer.Argument(help="Domain name to query.")],
|
|
380
|
+
rtype: Annotated[str, typer.Option("--type", "-t", help="Record type to compare.")] = "A",
|
|
381
|
+
timeout: Annotated[
|
|
382
|
+
float, typer.Option(help="Timeout in seconds for each resolver.")
|
|
383
|
+
] = COMPARE_TIMEOUT_S,
|
|
384
|
+
as_json: JsonOption = False,
|
|
385
|
+
) -> None:
|
|
386
|
+
try:
|
|
387
|
+
report = compare(name, rtype, timeout=timeout, resolvers=current_config().compare_resolvers)
|
|
388
|
+
except ValueError as exc:
|
|
389
|
+
_fail("compare", str(exc), as_json)
|
|
390
|
+
|
|
391
|
+
code = 0 if report.all_agree else 1
|
|
392
|
+
if as_json:
|
|
393
|
+
summary = {
|
|
394
|
+
"all_agree": report.all_agree,
|
|
395
|
+
"has_majority": report.has_majority,
|
|
396
|
+
"distinct_answers": report.distinct_answers,
|
|
397
|
+
}
|
|
398
|
+
_emit_json("compare", report, code, summary)
|
|
399
|
+
else:
|
|
400
|
+
render_compare(report, console)
|
|
401
|
+
_exit(code)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
@app.command(
|
|
405
|
+
"email",
|
|
406
|
+
epilog=_epilog(
|
|
407
|
+
["dnskit email example.com", "dnskit email example.com --selector s2024"],
|
|
408
|
+
"0 no check fails (warnings are fine), 1 a check fails or the domain doesn't exist, 2 "
|
|
409
|
+
"invalid input.",
|
|
410
|
+
),
|
|
411
|
+
help="Check a domain's email DNS setup: MX, SPF, DMARC and DKIM.\n\n"
|
|
412
|
+
"Each check is shown as pass, warn or fail with an explanation and a suggested fix. "
|
|
413
|
+
f"DKIM keys are looked up at the common selectors ({', '.join(DKIM_SELECTORS)}) and at "
|
|
414
|
+
"any given with --selector. ",
|
|
415
|
+
)
|
|
416
|
+
def email_command(
|
|
417
|
+
domain: Annotated[str, typer.Argument(help="Domain to check, such as example.com.")],
|
|
418
|
+
selector: Annotated[
|
|
419
|
+
list[str] | None,
|
|
420
|
+
typer.Option(
|
|
421
|
+
"--selector",
|
|
422
|
+
help="Extra DKIM selector to check (the s= value in an email's DKIM-Signature "
|
|
423
|
+
"header). Repeat it to check several.",
|
|
424
|
+
),
|
|
425
|
+
] = None,
|
|
426
|
+
server: ServerOption = None,
|
|
427
|
+
timeout: TimeoutOption = 5.0,
|
|
428
|
+
refresh: RefreshOption = False,
|
|
429
|
+
as_json: JsonOption = False,
|
|
430
|
+
) -> None:
|
|
431
|
+
try:
|
|
432
|
+
report = email_check(
|
|
433
|
+
domain, selector or (), nameserver=server, timeout=timeout, refresh=refresh
|
|
434
|
+
)
|
|
435
|
+
except ValueError as exc:
|
|
436
|
+
_fail("email", str(exc), as_json)
|
|
437
|
+
|
|
438
|
+
code = 1 if report.failed else 0
|
|
439
|
+
if as_json:
|
|
440
|
+
_emit_json("email", report, code, {"status": report.status, "failed": report.failed})
|
|
441
|
+
else:
|
|
442
|
+
render_email(report, console, timeout=timeout)
|
|
443
|
+
_exit(code)
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
@app.command(
|
|
447
|
+
"resolvers",
|
|
448
|
+
epilog=_epilog(
|
|
449
|
+
["dnskit resolvers", "dnskit resolvers --json"],
|
|
450
|
+
"0 at least one DNS server answered, 1 none did.",
|
|
451
|
+
),
|
|
452
|
+
)
|
|
453
|
+
def resolvers_command(
|
|
454
|
+
timeout: Annotated[float, typer.Option(help="Probe timeout in seconds.")] = 1.0,
|
|
455
|
+
as_json: JsonOption = False,
|
|
456
|
+
) -> None:
|
|
457
|
+
"""Probe the system's nameservers, show which respond, and refresh the cached choice.
|
|
458
|
+
|
|
459
|
+
Tests every DNS server this computer uses, shows how fast each answers, and marks the
|
|
460
|
+
one dnskit will use (the fastest). The choice is remembered for 5 minutes.
|
|
461
|
+
"""
|
|
462
|
+
nameservers = classify_nameservers()
|
|
463
|
+
if not nameservers.usable:
|
|
464
|
+
_fail("resolvers", "No system nameservers configured.", as_json, code=1)
|
|
465
|
+
choice = choose_nameserver(nameservers.usable, use_cache=False, timeout=timeout)
|
|
466
|
+
code = 1 if choice.nameserver is None else 0
|
|
467
|
+
if as_json:
|
|
468
|
+
report = {"choice": choice, "ignored": nameservers.ignored, "cache_path": cache_path()}
|
|
469
|
+
_emit_json("resolvers", report, code, {"chosen": choice.nameserver})
|
|
470
|
+
else:
|
|
471
|
+
render_probes(choice, console, str(cache_path()), nameservers.ignored)
|
|
472
|
+
_exit(code)
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
@app.command(
|
|
476
|
+
"local",
|
|
477
|
+
epilog=_epilog(
|
|
478
|
+
["dnskit local", "dnskit local --timeout 2"],
|
|
479
|
+
"0 DNS works here (warnings are fine), 1 an error: no adapter, no DNS server, or none "
|
|
480
|
+
"answering.",
|
|
481
|
+
),
|
|
482
|
+
)
|
|
483
|
+
def local_command(
|
|
484
|
+
timeout: Annotated[
|
|
485
|
+
float, typer.Option(help="Timeout in seconds for each DNS server's test query.")
|
|
486
|
+
] = PROBE_TIMEOUT_S,
|
|
487
|
+
as_json: JsonOption = False,
|
|
488
|
+
) -> None:
|
|
489
|
+
"""Check this computer's DNS setup: adapters, DNS servers, ghosts and the hosts file.
|
|
490
|
+
|
|
491
|
+
Lists every network adapter and its DNS servers, tests each server, flags ghost servers
|
|
492
|
+
(in the system configuration but on no connected adapter) and hosts file entries that
|
|
493
|
+
override DNS, checks for NXDOMAIN hijacking, DNS interception and broken IPv6, and ends
|
|
494
|
+
with the problems found and how to fix them.
|
|
495
|
+
"""
|
|
496
|
+
report = local_check(timeout=timeout)
|
|
497
|
+
code = 1 if report.has_errors else 0
|
|
498
|
+
if as_json:
|
|
499
|
+
summary = {"has_errors": report.has_errors, "problems": len(report.problems)}
|
|
500
|
+
_emit_json("local", report, code, summary)
|
|
501
|
+
else:
|
|
502
|
+
render_local(report, console)
|
|
503
|
+
_exit(code)
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
# Exit code for `reach` when DNS works but the connection doesn't (1 is a DNS problem).
|
|
507
|
+
EXIT_CONNECTION_FAILED = 3
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
@app.command(
|
|
511
|
+
"reach",
|
|
512
|
+
epilog=_epilog(
|
|
513
|
+
[
|
|
514
|
+
"dnskit reach example.com",
|
|
515
|
+
"dnskit reach https://example.com/page",
|
|
516
|
+
"dnskit reach mail.example.com -p 25",
|
|
517
|
+
],
|
|
518
|
+
"0 reachable, 1 a DNS problem, 3 a connection problem, 2 invalid input.",
|
|
519
|
+
),
|
|
520
|
+
)
|
|
521
|
+
def reach_command(
|
|
522
|
+
target: Annotated[str, typer.Argument(help="Domain name (or URL) to test, e.g. example.com.")],
|
|
523
|
+
port: Annotated[
|
|
524
|
+
int, typer.Option("--port", "-p", min=1, max=65535, help="TCP port to connect to.")
|
|
525
|
+
] = DEFAULT_PORT,
|
|
526
|
+
server: ServerOption = None,
|
|
527
|
+
timeout: Annotated[
|
|
528
|
+
float, typer.Option(help="Timeout in seconds for the lookups and each connection.")
|
|
529
|
+
] = 5.0,
|
|
530
|
+
refresh: RefreshOption = False,
|
|
531
|
+
as_json: JsonOption = False,
|
|
532
|
+
) -> None:
|
|
533
|
+
"""Can this computer reach a site? Resolve it, then connect to it.
|
|
534
|
+
|
|
535
|
+
Looks up the name's IPv4 and IPv6 addresses, tries a TCP connection to the port (443,
|
|
536
|
+
HTTPS, by default) on each, and says whether the problem is DNS, the connection, or
|
|
537
|
+
neither.
|
|
538
|
+
"""
|
|
539
|
+
try:
|
|
540
|
+
report = reach(target, port=port, nameserver=server, timeout=timeout, refresh=refresh)
|
|
541
|
+
except ValueError as exc:
|
|
542
|
+
_fail("reach", str(exc), as_json)
|
|
543
|
+
|
|
544
|
+
code = {ReachVerdict.DNS: 1, ReachVerdict.CONNECTION: EXIT_CONNECTION_FAILED}.get(
|
|
545
|
+
report.verdict, 0
|
|
546
|
+
)
|
|
547
|
+
if as_json:
|
|
548
|
+
diagnosis = explain_reach(report, timeout)
|
|
549
|
+
_emit_json("reach", report, code, {"verdict": report.verdict}, diagnosis)
|
|
550
|
+
else:
|
|
551
|
+
render_reach(report, console, timeout=timeout)
|
|
552
|
+
_exit(code)
|
|
553
|
+
|
|
554
|
+
|
|
555
|
+
@app.command(
|
|
556
|
+
"trace",
|
|
557
|
+
epilog=_epilog(
|
|
558
|
+
["dnskit trace example.com", "dnskit trace example.com -t MX"],
|
|
559
|
+
"0 the records were found, 1 anything else, 2 invalid input.",
|
|
560
|
+
),
|
|
561
|
+
)
|
|
562
|
+
def trace_command(
|
|
563
|
+
name: Annotated[str, typer.Argument(help="Domain name to trace.")],
|
|
564
|
+
rtype: Annotated[str, typer.Option("--type", "-t", help="Record type to ask for.")] = "A",
|
|
565
|
+
timeout: Annotated[
|
|
566
|
+
float, typer.Option(help="Timeout in seconds for each level's nameservers.")
|
|
567
|
+
] = TRACE_TIMEOUT_S,
|
|
568
|
+
as_json: JsonOption = False,
|
|
569
|
+
) -> None:
|
|
570
|
+
"""Follow a name's delegation from the root servers down, like `dig +trace`.
|
|
571
|
+
|
|
572
|
+
Asks the root servers, then each level's nameservers in turn, and shows who referred
|
|
573
|
+
the query where and how long each took. If a level fails, says which one and why. If a
|
|
574
|
+
VPN or firewall blocks direct queries, explains that and follows the delegation through
|
|
575
|
+
this computer's own DNS server instead.
|
|
576
|
+
"""
|
|
577
|
+
try:
|
|
578
|
+
report = trace(name, rtype, timeout=timeout)
|
|
579
|
+
except ValueError as exc:
|
|
580
|
+
_fail("trace", str(exc), as_json)
|
|
581
|
+
|
|
582
|
+
code = 0 if report.succeeded else 1
|
|
583
|
+
if as_json:
|
|
584
|
+
summary = {"succeeded": report.succeeded, "mode": report.mode}
|
|
585
|
+
_emit_json("trace", report, code, summary, explain_trace(report))
|
|
586
|
+
else:
|
|
587
|
+
render_trace(report, console)
|
|
588
|
+
_exit(code)
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
@app.command(
|
|
592
|
+
"dnssec",
|
|
593
|
+
epilog=_epilog(
|
|
594
|
+
["dnskit dnssec example.com", "dnskit dnssec example.com -s 8.8.8.8"],
|
|
595
|
+
"0 it validates, or DNSSEC isn't enabled; 1 it's broken, couldn't be checked, or the name "
|
|
596
|
+
"doesn't exist; 2 invalid input.",
|
|
597
|
+
),
|
|
598
|
+
)
|
|
599
|
+
def dnssec_command(
|
|
600
|
+
name: Annotated[str, typer.Argument(help="Domain name to check.")],
|
|
601
|
+
server: ServerOption = None,
|
|
602
|
+
timeout: TimeoutOption = 5.0,
|
|
603
|
+
refresh: RefreshOption = False,
|
|
604
|
+
as_json: JsonOption = False,
|
|
605
|
+
) -> None:
|
|
606
|
+
"""Check whether DNSSEC is enabled and its chain of trust validates from the root down.
|
|
607
|
+
|
|
608
|
+
Checks the DS and DNSKEY records of every zone from the root to the domain, verifies
|
|
609
|
+
each signature, and explains the result. Also warns when this computer's clock looks
|
|
610
|
+
wrong, since that breaks validation. When this computer's DNS server strips the
|
|
611
|
+
signatures, the check is done through 8.8.8.8 or 1.1.1.1 instead.
|
|
612
|
+
"""
|
|
613
|
+
try:
|
|
614
|
+
report = dnssec_check(name, nameserver=server, timeout=timeout, refresh=refresh)
|
|
615
|
+
except ValueError as exc:
|
|
616
|
+
_fail("dnssec", str(exc), as_json)
|
|
617
|
+
|
|
618
|
+
ok = report.status in (DnssecStatus.SECURE, DnssecStatus.INSECURE)
|
|
619
|
+
code = 0 if ok and not report.name_missing else 1
|
|
620
|
+
if as_json:
|
|
621
|
+
summary = {
|
|
622
|
+
"status": report.status,
|
|
623
|
+
"name_missing": report.name_missing,
|
|
624
|
+
"checked_through": report.nameserver,
|
|
625
|
+
"stripped_by": report.stripped_by,
|
|
626
|
+
}
|
|
627
|
+
_emit_json("dnssec", report, code, summary, explain_dnssec(report))
|
|
628
|
+
else:
|
|
629
|
+
render_dnssec(report, console)
|
|
630
|
+
_exit(code)
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
@app.command(
|
|
634
|
+
"propagation",
|
|
635
|
+
epilog=_epilog(
|
|
636
|
+
["dnskit propagation example.com", "dnskit propagation example.com -t MX"],
|
|
637
|
+
"0 every resolver that answered agrees, 1 they differ or too few could be reached, 2 "
|
|
638
|
+
"invalid input.",
|
|
639
|
+
),
|
|
640
|
+
help="Has a DNS change reached resolvers around the world?\n\n"
|
|
641
|
+
f"Asks {len(PROPAGATION_RESOLVERS)} public resolvers (or the ones in the config file) for "
|
|
642
|
+
"the record: global anycast ones, which answer from the site nearest you, and ones in "
|
|
643
|
+
"fixed countries in the Americas, Europe and Asia. Shows what share give the same "
|
|
644
|
+
"answer.",
|
|
645
|
+
)
|
|
646
|
+
def propagation_command(
|
|
647
|
+
name: Annotated[str, typer.Argument(help="Domain name to check.")],
|
|
648
|
+
rtype: Annotated[str, typer.Option("--type", "-t", help="Record type to check.")] = "A",
|
|
649
|
+
timeout: Annotated[
|
|
650
|
+
float, typer.Option(help="Timeout in seconds for each resolver.")
|
|
651
|
+
] = PROPAGATION_TIMEOUT_S,
|
|
652
|
+
as_json: JsonOption = False,
|
|
653
|
+
) -> None:
|
|
654
|
+
try:
|
|
655
|
+
report = propagation(
|
|
656
|
+
name, rtype, timeout=timeout, resolvers=current_config().propagation_resolvers
|
|
657
|
+
)
|
|
658
|
+
except ValueError as exc:
|
|
659
|
+
_fail("propagation", str(exc), as_json)
|
|
660
|
+
|
|
661
|
+
code = 0 if report.complete else 1
|
|
662
|
+
if as_json:
|
|
663
|
+
summary = {
|
|
664
|
+
"percent": round(report.percent, 1),
|
|
665
|
+
"complete": report.complete,
|
|
666
|
+
"location_based": report.location_based,
|
|
667
|
+
"blocked": report.blocked,
|
|
668
|
+
}
|
|
669
|
+
_emit_json("propagation", report, code, summary, explain_propagation(report))
|
|
670
|
+
else:
|
|
671
|
+
render_propagation(report, console)
|
|
672
|
+
_exit(code)
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
@app.command(
|
|
676
|
+
"doctor",
|
|
677
|
+
epilog=_epilog(
|
|
678
|
+
[
|
|
679
|
+
"dnskit doctor example.com",
|
|
680
|
+
"dnskit doctor example.com --quick",
|
|
681
|
+
"dnskit doctor example.com --json",
|
|
682
|
+
],
|
|
683
|
+
"0 no critical or major issue (minor issues and notes are fine), 1 otherwise, 2 invalid "
|
|
684
|
+
"input.",
|
|
685
|
+
),
|
|
686
|
+
help="Full health check: this computer's DNS setup, then the domain (records, resolvers, "
|
|
687
|
+
"connection, email, DNSSEC), with a score out of 100 and a ranked list of fixes.\n\n"
|
|
688
|
+
"Separates problems with this computer from problems with the domain, and explains each "
|
|
689
|
+
"with a fix. A domain that doesn't exist scores 0. --quick skips the slower checks.",
|
|
690
|
+
)
|
|
691
|
+
def doctor_command(
|
|
692
|
+
name: Annotated[str, typer.Argument(help="Domain name (or URL) to check.")],
|
|
693
|
+
quick: Annotated[
|
|
694
|
+
bool,
|
|
695
|
+
typer.Option(
|
|
696
|
+
"--quick", help="Skip the slower checks: resolver comparison, email and DNSSEC."
|
|
697
|
+
),
|
|
698
|
+
] = False,
|
|
699
|
+
timeout: Annotated[
|
|
700
|
+
float, typer.Option(help="Timeout in seconds for each query of the domain checks.")
|
|
701
|
+
] = DOCTOR_TIMEOUT_S,
|
|
702
|
+
as_json: JsonOption = False,
|
|
703
|
+
) -> None:
|
|
704
|
+
resolvers = current_config().compare_resolvers
|
|
705
|
+
progress = DoctorProgress()
|
|
706
|
+
try:
|
|
707
|
+
if console.is_terminal and not as_json:
|
|
708
|
+
with Live(progress, console=console, transient=True, refresh_per_second=12):
|
|
709
|
+
report = doctor(
|
|
710
|
+
name,
|
|
711
|
+
quick=quick,
|
|
712
|
+
timeout=timeout,
|
|
713
|
+
on_progress=progress.update,
|
|
714
|
+
resolvers=resolvers,
|
|
715
|
+
)
|
|
716
|
+
else:
|
|
717
|
+
report = doctor(name, quick=quick, timeout=timeout, resolvers=resolvers)
|
|
718
|
+
except ValueError as exc:
|
|
719
|
+
_fail("doctor", str(exc), as_json)
|
|
720
|
+
|
|
721
|
+
code = 0 if report.healthy else 1
|
|
722
|
+
if as_json:
|
|
723
|
+
summary = {
|
|
724
|
+
"score": report.score,
|
|
725
|
+
"healthy": report.healthy,
|
|
726
|
+
"name_missing": report.name_missing,
|
|
727
|
+
"issues": len(report.problems),
|
|
728
|
+
"notes": len(report.issues) - len(report.problems),
|
|
729
|
+
}
|
|
730
|
+
_emit_json("doctor", report, code, summary, doctor_verdict(report))
|
|
731
|
+
else:
|
|
732
|
+
render_doctor(report, console)
|
|
733
|
+
_exit(code)
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
@app.command(
|
|
737
|
+
"batch",
|
|
738
|
+
epilog=_epilog(
|
|
739
|
+
[
|
|
740
|
+
"dnskit batch domains.txt",
|
|
741
|
+
"dnskit batch domains.txt --check doctor --quick",
|
|
742
|
+
"dnskit batch - --check email < domains.txt",
|
|
743
|
+
],
|
|
744
|
+
"0 every domain is OK, 1 one isn't or a line isn't a domain, 2 the file can't be read or "
|
|
745
|
+
"has no domains.",
|
|
746
|
+
),
|
|
747
|
+
help="Run lookup, doctor or email on every domain in a text file, then show a summary.\n\n"
|
|
748
|
+
"The file has one domain (or URL) per line; blank lines and # comments are skipped. Use "
|
|
749
|
+
"'-' to read the list from standard input. For doctor, this computer is checked once.",
|
|
750
|
+
)
|
|
751
|
+
def batch_command(
|
|
752
|
+
file: Annotated[str, typer.Argument(help="File with one domain per line, or '-'.")],
|
|
753
|
+
check: Annotated[
|
|
754
|
+
BatchCheck, typer.Option("--check", "-c", help="What to run on each domain.")
|
|
755
|
+
] = BatchCheck.LOOKUP,
|
|
756
|
+
parallel: Annotated[
|
|
757
|
+
int,
|
|
758
|
+
typer.Option(min=1, max=MAX_PARALLEL, help="How many domains to check at the same time."),
|
|
759
|
+
] = DEFAULT_PARALLEL,
|
|
760
|
+
quick: Annotated[
|
|
761
|
+
bool, typer.Option("--quick", help="With --check doctor: skip the slower checks.")
|
|
762
|
+
] = False,
|
|
763
|
+
server: ServerOption = None,
|
|
764
|
+
timeout: Annotated[
|
|
765
|
+
float | None,
|
|
766
|
+
typer.Option(help="Timeout in seconds, as for the command run. Default: its own."),
|
|
767
|
+
] = None,
|
|
768
|
+
as_json: JsonOption = False,
|
|
769
|
+
) -> None:
|
|
770
|
+
try:
|
|
771
|
+
text = sys.stdin.read() if file == "-" else Path(file).read_text(encoding="utf-8-sig")
|
|
772
|
+
except OSError as exc:
|
|
773
|
+
_fail("batch", f"Can't read {file}: {exc.strerror or exc}.", as_json)
|
|
774
|
+
lines = read_domains(text)
|
|
775
|
+
if not any(line.name for line in lines):
|
|
776
|
+
where = "standard input" if file == "-" else file
|
|
777
|
+
_fail("batch", f"No domains found in {where}: put one domain on each line.", as_json)
|
|
778
|
+
|
|
779
|
+
config = current_config()
|
|
780
|
+
options: dict[str, Any] = {
|
|
781
|
+
"timeout": timeout,
|
|
782
|
+
"parallel": parallel,
|
|
783
|
+
"quick": quick,
|
|
784
|
+
"server": server if check is not BatchCheck.DOCTOR else None,
|
|
785
|
+
"selectors": tuple(config.for_command("email").get("selectors", ())),
|
|
786
|
+
"resolvers": config.compare_resolvers,
|
|
787
|
+
}
|
|
788
|
+
total = len({line.name for line in lines if line.name})
|
|
789
|
+
if console.is_terminal and not as_json:
|
|
790
|
+
progress = BatchProgress(total, check)
|
|
791
|
+
with Live(progress, console=console, transient=True, refresh_per_second=12):
|
|
792
|
+
report = run_batch(lines, check, on_done=progress.done, **options)
|
|
793
|
+
else:
|
|
794
|
+
report = run_batch(lines, check, **options)
|
|
795
|
+
|
|
796
|
+
code = 0 if report.all_ok else 1
|
|
797
|
+
if as_json:
|
|
798
|
+
summary = {
|
|
799
|
+
"check": check,
|
|
800
|
+
"domains": len(report.items),
|
|
801
|
+
"ok": len(report.passed),
|
|
802
|
+
"not_ok": len(report.failed),
|
|
803
|
+
"invalid_lines": len(report.invalid),
|
|
804
|
+
}
|
|
805
|
+
_emit_json("batch", report, code, summary)
|
|
806
|
+
else:
|
|
807
|
+
render_batch(report, console)
|
|
808
|
+
_exit(code)
|
|
809
|
+
|
|
810
|
+
|
|
811
|
+
@app.command(
|
|
812
|
+
"watch",
|
|
813
|
+
epilog=_epilog(
|
|
814
|
+
[
|
|
815
|
+
"dnskit watch example.com",
|
|
816
|
+
"dnskit watch example.com --until 203.0.113.80 --interval 60",
|
|
817
|
+
"dnskit watch www.example.com -t CNAME --exit-on-change",
|
|
818
|
+
],
|
|
819
|
+
"0 stopped as asked, 1 --count ran out before --until was reached, 130 Ctrl+C, 2 invalid "
|
|
820
|
+
"input.",
|
|
821
|
+
),
|
|
822
|
+
help="Re-check a record every few seconds and say when the answer changes.\n\n"
|
|
823
|
+
"Useful while waiting for a DNS change to spread. Each check prints the answer; a change "
|
|
824
|
+
"is highlighted with a beep. A failed lookup is shown but never counts as a change. "
|
|
825
|
+
"Stops with Ctrl+C, after --count checks, at the first change with --exit-on-change, or "
|
|
826
|
+
"once the answer includes --until. Answers that only rotate between addresses already "
|
|
827
|
+
"seen (a CDN) aren't changes unless --strict.",
|
|
828
|
+
)
|
|
829
|
+
def watch_command(
|
|
830
|
+
name: Annotated[str, typer.Argument(help="Domain name (or URL) to watch.")],
|
|
831
|
+
rtype: Annotated[str, typer.Option("--type", "-t", help="Record type to watch.")] = "A",
|
|
832
|
+
server: ServerOption = None,
|
|
833
|
+
interval: Annotated[
|
|
834
|
+
float, typer.Option(help="Seconds between checks (at least 1).")
|
|
835
|
+
] = WATCH_INTERVAL_S,
|
|
836
|
+
until: Annotated[
|
|
837
|
+
str | None,
|
|
838
|
+
typer.Option(help="Stop once the answer includes this value, e.g. the new IP address."),
|
|
839
|
+
] = None,
|
|
840
|
+
count: Annotated[int | None, typer.Option(min=1, help="Stop after this many checks.")] = None,
|
|
841
|
+
exit_on_change: Annotated[
|
|
842
|
+
bool, typer.Option("--exit-on-change", help="Stop at the first change.")
|
|
843
|
+
] = False,
|
|
844
|
+
strict: Annotated[
|
|
845
|
+
bool,
|
|
846
|
+
typer.Option(
|
|
847
|
+
"--strict",
|
|
848
|
+
help="Count every change, including rotation between addresses already seen.",
|
|
849
|
+
),
|
|
850
|
+
] = False,
|
|
851
|
+
timeout: TimeoutOption = 5.0,
|
|
852
|
+
as_json: JsonOption = False,
|
|
853
|
+
) -> None:
|
|
854
|
+
try:
|
|
855
|
+
samples = watch(name, rtype, nameserver=server, interval=interval, timeout=timeout)
|
|
856
|
+
except ValueError as exc:
|
|
857
|
+
_fail("watch", str(exc), as_json)
|
|
858
|
+
if not as_json:
|
|
859
|
+
render_watch_start(parse_target(name), rtype.upper(), interval, until, console)
|
|
860
|
+
|
|
861
|
+
seen: list[WatchSample] = []
|
|
862
|
+
changes = 0 # changes worth an alert: any change with --strict, else new answers only
|
|
863
|
+
|
|
864
|
+
def stop(reason: str, code: int) -> NoReturn:
|
|
865
|
+
summary = watch_summary(seen)
|
|
866
|
+
if as_json:
|
|
867
|
+
_print_json_line(
|
|
868
|
+
{
|
|
869
|
+
"command": "watch",
|
|
870
|
+
"event": "end",
|
|
871
|
+
"reason": reason,
|
|
872
|
+
"checks": len(seen),
|
|
873
|
+
"changes": changes,
|
|
874
|
+
"rotations": 0 if strict else summary.rotations,
|
|
875
|
+
"seen": summary.values,
|
|
876
|
+
"exit_code": code,
|
|
877
|
+
}
|
|
878
|
+
)
|
|
879
|
+
else:
|
|
880
|
+
render_watch_end(summary, reason, strict, console)
|
|
881
|
+
raise typer.Exit(code)
|
|
882
|
+
|
|
883
|
+
try:
|
|
884
|
+
for sample in samples:
|
|
885
|
+
seen.append(sample)
|
|
886
|
+
# Rotation between addresses already seen is a CDN at work, not news.
|
|
887
|
+
alert = sample.changed if strict else sample.new_change
|
|
888
|
+
changes += alert
|
|
889
|
+
if as_json:
|
|
890
|
+
event = {
|
|
891
|
+
"command": "watch",
|
|
892
|
+
"event": "check",
|
|
893
|
+
"changed": sample.changed,
|
|
894
|
+
"rotating": sample.rotating and not strict,
|
|
895
|
+
"alert": alert,
|
|
896
|
+
"answered": sample.answered,
|
|
897
|
+
"sample": sample,
|
|
898
|
+
}
|
|
899
|
+
if until:
|
|
900
|
+
event["includes_until"] = sample.matches(until)
|
|
901
|
+
_print_json_line(event)
|
|
902
|
+
else:
|
|
903
|
+
render_watch_sample(sample, console, until, strict)
|
|
904
|
+
if alert:
|
|
905
|
+
console.bell()
|
|
906
|
+
if until and sample.matches(until):
|
|
907
|
+
stop(f"Done: the answer includes {until}", 0)
|
|
908
|
+
if exit_on_change and alert:
|
|
909
|
+
stop("Stopped at the first change", 0)
|
|
910
|
+
if count and len(seen) >= count:
|
|
911
|
+
stop("Stopped", 1 if until else 0)
|
|
912
|
+
except KeyboardInterrupt:
|
|
913
|
+
stop("Stopped", 130)
|
|
914
|
+
|
|
915
|
+
|
|
916
|
+
@app.command(
|
|
917
|
+
"config",
|
|
918
|
+
epilog=_epilog(
|
|
919
|
+
["dnskit config", "dnskit config --init", "dnskit config --path"],
|
|
920
|
+
"0 done, 1 --init found a file already there, 2 the file has a mistake.",
|
|
921
|
+
),
|
|
922
|
+
)
|
|
923
|
+
def config_command(
|
|
924
|
+
init: Annotated[
|
|
925
|
+
bool, typer.Option("--init", help="Create a commented starter config file.")
|
|
926
|
+
] = False,
|
|
927
|
+
show_path: Annotated[
|
|
928
|
+
bool, typer.Option("--path", help="Only print where the config file is.")
|
|
929
|
+
] = False,
|
|
930
|
+
as_json: JsonOption = False,
|
|
931
|
+
) -> None:
|
|
932
|
+
"""Show the config file's location and settings, or create one with --init.
|
|
933
|
+
|
|
934
|
+
The file (TOML) sets default options for each command and can replace the resolvers
|
|
935
|
+
that `compare` and `propagation` ask. Set DNSKIT_CONFIG to use another file.
|
|
936
|
+
"""
|
|
937
|
+
path = config_path()
|
|
938
|
+
if show_path:
|
|
939
|
+
typer.echo(str(path))
|
|
940
|
+
return
|
|
941
|
+
if init:
|
|
942
|
+
try:
|
|
943
|
+
write_template(path)
|
|
944
|
+
except ConfigError as exc:
|
|
945
|
+
_fail("config", str(exc), as_json, code=1)
|
|
946
|
+
console.print(f"Created [bold]{escape(str(path))}[/bold]. Uncomment the settings you want.")
|
|
947
|
+
return
|
|
948
|
+
try:
|
|
949
|
+
config = load_config(path)
|
|
950
|
+
except ConfigError as exc:
|
|
951
|
+
_fail("config", str(exc), as_json)
|
|
952
|
+
if as_json:
|
|
953
|
+
_emit_json("config", config, 0, {"exists": config.exists, "path": path})
|
|
954
|
+
else:
|
|
955
|
+
render_config(config, console)
|