tlscheck 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.
- tlscheck/__init__.py +12 -0
- tlscheck/__main__.py +5 -0
- tlscheck/app.py +676 -0
- tlscheck/app.tcss +43 -0
- tlscheck/domains.py +224 -0
- tlscheck/models.py +118 -0
- tlscheck/probe.py +999 -0
- tlscheck-0.1.0.dist-info/METADATA +128 -0
- tlscheck-0.1.0.dist-info/RECORD +11 -0
- tlscheck-0.1.0.dist-info/WHEEL +4 -0
- tlscheck-0.1.0.dist-info/entry_points.txt +3 -0
tlscheck/__init__.py
ADDED
tlscheck/__main__.py
ADDED
tlscheck/app.py
ADDED
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
"""Textual TUI for tlscheck.
|
|
2
|
+
|
|
3
|
+
Run with ``uv run python -m tlscheck`` or the ``tlscheck`` script. The app
|
|
4
|
+
loads targets from an INI file, scans them concurrently in worker threads and
|
|
5
|
+
streams each result into a :class:`~textual.widgets.DataTable`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import argparse
|
|
11
|
+
import time
|
|
12
|
+
from collections.abc import Callable
|
|
13
|
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from rich.text import Text
|
|
18
|
+
from textual import on, work
|
|
19
|
+
from textual.app import App, ComposeResult
|
|
20
|
+
from textual.binding import Binding
|
|
21
|
+
from textual.containers import Horizontal, Vertical, VerticalScroll
|
|
22
|
+
from textual.coordinate import Coordinate
|
|
23
|
+
from textual.widgets import DataTable, Footer, Header, ProgressBar, Static
|
|
24
|
+
|
|
25
|
+
from tlscheck.domains import Domain, DomainFileError, load_domains
|
|
26
|
+
from tlscheck.models import VERSIONS, ProbeResult
|
|
27
|
+
|
|
28
|
+
# column key, header label, width
|
|
29
|
+
COLUMNS: tuple[tuple[str, str, int], ...] = (
|
|
30
|
+
("domain", "Domain", 21),
|
|
31
|
+
("grade", "Grade", 5),
|
|
32
|
+
("cipher", "Cipher", 21),
|
|
33
|
+
("kex", "Key Exchange", 14),
|
|
34
|
+
("http", "HTTP", 4),
|
|
35
|
+
("hsts", "HSTS", 4),
|
|
36
|
+
("pq", "PQ", 2),
|
|
37
|
+
("cert", "Cert", 4),
|
|
38
|
+
("versions", "Versions", 15),
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
GRADE_STYLES = {
|
|
42
|
+
"A+": "bold green",
|
|
43
|
+
"A": "green",
|
|
44
|
+
"A-": "green",
|
|
45
|
+
"B+": "yellow",
|
|
46
|
+
"B": "yellow",
|
|
47
|
+
"B-": "orange1",
|
|
48
|
+
"C+": "orange1",
|
|
49
|
+
"C": "orange1",
|
|
50
|
+
"D": "bold red",
|
|
51
|
+
"E": "red",
|
|
52
|
+
"F": "bold red",
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _run_probe(
|
|
57
|
+
host: str,
|
|
58
|
+
port: int,
|
|
59
|
+
label: str = "",
|
|
60
|
+
auth: tuple[str, str] | None = None,
|
|
61
|
+
path: str = "/",
|
|
62
|
+
) -> ProbeResult:
|
|
63
|
+
"""Call the probe engine, importing it lazily.
|
|
64
|
+
|
|
65
|
+
``probe.py`` may not exist yet (it is developed in parallel) and the TUI
|
|
66
|
+
must stay importable without it, hence the local import.
|
|
67
|
+
"""
|
|
68
|
+
from tlscheck.probe import probe
|
|
69
|
+
|
|
70
|
+
try:
|
|
71
|
+
return probe(host, port, 8.0, label, auth, path)
|
|
72
|
+
except Exception as exc: # probe() promises not to raise; be defensive
|
|
73
|
+
return ProbeResult(
|
|
74
|
+
host=host,
|
|
75
|
+
port=port,
|
|
76
|
+
label=label,
|
|
77
|
+
ok=False,
|
|
78
|
+
error=f"probe crashed: {exc!r}",
|
|
79
|
+
grade="F",
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _failure(domain: Domain, exc: BaseException) -> ProbeResult:
|
|
84
|
+
return ProbeResult(
|
|
85
|
+
host=domain.host,
|
|
86
|
+
port=domain.port,
|
|
87
|
+
label=domain.label,
|
|
88
|
+
ok=False,
|
|
89
|
+
error=f"worker failed: {exc!r}",
|
|
90
|
+
grade="F",
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _grade_text(grade: str | None) -> Text:
|
|
95
|
+
style = GRADE_STYLES.get((grade or "").upper(), "dim")
|
|
96
|
+
return Text(grade or "?", style=style)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _version_text(result: ProbeResult) -> Text:
|
|
100
|
+
"""Render the 1.3/1.2/1.1/1.0 matrix in fixed 3-char slots.
|
|
101
|
+
|
|
102
|
+
Fixed slots keep the columns aligned across rows (a 1.2-only host shows
|
|
103
|
+
'1.2' exactly under the 1.2 slot), and legacy protocols are red.
|
|
104
|
+
"""
|
|
105
|
+
text = Text()
|
|
106
|
+
for index, version in enumerate(VERSIONS):
|
|
107
|
+
if index:
|
|
108
|
+
text.append(" ", style="dim")
|
|
109
|
+
short = version.removeprefix("TLSv")
|
|
110
|
+
entry = result.versions.get(version)
|
|
111
|
+
if entry is not None and entry.supported:
|
|
112
|
+
if short in ("1.1", "1.0"):
|
|
113
|
+
text.append(short, style="bold red")
|
|
114
|
+
elif short == "1.3":
|
|
115
|
+
text.append(short, style="bold green")
|
|
116
|
+
else:
|
|
117
|
+
text.append(short, style="green")
|
|
118
|
+
else:
|
|
119
|
+
text.append(" · ", style="dim")
|
|
120
|
+
return text
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _short_cipher(name: str | None) -> str:
|
|
124
|
+
"""Compact cipher names so they fit the table column.
|
|
125
|
+
|
|
126
|
+
The detail pane always shows the full negotiated name.
|
|
127
|
+
"""
|
|
128
|
+
if not name:
|
|
129
|
+
return ""
|
|
130
|
+
upper = name.upper()
|
|
131
|
+
if any(tag in upper for tag in ("GCM", "CCM", "POLY1305")):
|
|
132
|
+
for suffix in ("-SHA384", "-SHA256", "-SHA", "_SHA384", "_SHA256", "_SHA"):
|
|
133
|
+
if name.endswith(suffix):
|
|
134
|
+
name = name[: -len(suffix)]
|
|
135
|
+
break
|
|
136
|
+
if len(name) > 22 and "CHACHA20-POLY1305" in name.upper():
|
|
137
|
+
name = name.upper().replace("CHACHA20-POLY1305", "CHACHA20")
|
|
138
|
+
return name
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _cert_text(result: ProbeResult) -> Text:
|
|
142
|
+
if not result.ok:
|
|
143
|
+
return Text("—", style="dim")
|
|
144
|
+
days = result.cert_days_left
|
|
145
|
+
if days is None:
|
|
146
|
+
return Text("?", style="dim")
|
|
147
|
+
prefix = "!" if result.cert_trusted is False else ""
|
|
148
|
+
if days < 0:
|
|
149
|
+
return Text(f"{prefix}exp", style="bold red")
|
|
150
|
+
if days <= 14:
|
|
151
|
+
style = "bold red"
|
|
152
|
+
elif days <= 30:
|
|
153
|
+
style = "orange1"
|
|
154
|
+
else:
|
|
155
|
+
style = "green"
|
|
156
|
+
if result.cert_trusted is False:
|
|
157
|
+
style = "red"
|
|
158
|
+
return Text(f"{prefix}{days}d", style=style)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _http_text(result: ProbeResult) -> Text:
|
|
162
|
+
status = result.http_status
|
|
163
|
+
if status is None:
|
|
164
|
+
return Text("—" if not result.ok else "?", style="dim")
|
|
165
|
+
note = result.http_note or ""
|
|
166
|
+
if status == 401:
|
|
167
|
+
style = "orange1"
|
|
168
|
+
elif 200 <= status < 300:
|
|
169
|
+
style = "green" if note.startswith("basic auth ok") else "dim"
|
|
170
|
+
elif status == 403:
|
|
171
|
+
style = "orange1"
|
|
172
|
+
else:
|
|
173
|
+
style = "dim"
|
|
174
|
+
return Text(str(status), style=style)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _human_age(seconds: int | None) -> str:
|
|
178
|
+
if seconds is None:
|
|
179
|
+
return "?"
|
|
180
|
+
if seconds == 0:
|
|
181
|
+
return "0"
|
|
182
|
+
days = seconds // 86400
|
|
183
|
+
if days >= 365:
|
|
184
|
+
return f"{days // 365}y"
|
|
185
|
+
if days >= 30:
|
|
186
|
+
return f"{days // 30}mo"
|
|
187
|
+
if days >= 1:
|
|
188
|
+
return f"{days}d"
|
|
189
|
+
return f"{seconds}s"
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _hsts_text(result: ProbeResult) -> Text:
|
|
193
|
+
if result.http_status is None:
|
|
194
|
+
return Text("—", style="dim")
|
|
195
|
+
if not result.hsts_header:
|
|
196
|
+
return Text("no", style="orange1")
|
|
197
|
+
age = result.hsts_max_age
|
|
198
|
+
if age is None:
|
|
199
|
+
return Text("?", style="orange1")
|
|
200
|
+
if age == 0:
|
|
201
|
+
return Text("0", style="red")
|
|
202
|
+
days = age // 86400
|
|
203
|
+
return Text(_human_age(age), style="green" if days >= 180 else "orange1")
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _domain_text(domain: Domain) -> Text:
|
|
207
|
+
text = Text(domain.label or domain.host, style="bold")
|
|
208
|
+
if domain.label and domain.label != domain.host:
|
|
209
|
+
text.append("\n" + domain.host, style="dim")
|
|
210
|
+
return text
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
class TlsCheckApp(App[None]):
|
|
214
|
+
"""Scan a list of TLS endpoints and tabulate the results."""
|
|
215
|
+
|
|
216
|
+
CSS_PATH = "app.tcss"
|
|
217
|
+
TITLE = "tlscheck"
|
|
218
|
+
SUB_TITLE = "TLS endpoint inventory"
|
|
219
|
+
|
|
220
|
+
BINDINGS = [
|
|
221
|
+
Binding("r", "rescan_all", "Rescan all"),
|
|
222
|
+
Binding("s", "rescan_selected", "Rescan row"),
|
|
223
|
+
Binding("l", "reload_domains", "Reload INI"),
|
|
224
|
+
Binding("tab", "focus_toggle", "Focus", priority=True),
|
|
225
|
+
Binding("enter", "focus_detail", "Detail", show=False, priority=True),
|
|
226
|
+
Binding("q", "quit", "Quit"),
|
|
227
|
+
]
|
|
228
|
+
|
|
229
|
+
def __init__(self, domains_path: str | Path = Path("domains.ini")) -> None:
|
|
230
|
+
super().__init__()
|
|
231
|
+
self.domains_path = Path(domains_path)
|
|
232
|
+
self._domains: list[Domain] = []
|
|
233
|
+
self._results: dict[str, ProbeResult] = {}
|
|
234
|
+
self._table_keys: set[str] = set()
|
|
235
|
+
self._selected_key: str | None = None
|
|
236
|
+
self._generation = 0
|
|
237
|
+
self._load_error: str | None = None
|
|
238
|
+
|
|
239
|
+
# ---------------------------------------------------------------- layout
|
|
240
|
+
|
|
241
|
+
def compose(self) -> ComposeResult:
|
|
242
|
+
yield Header(show_clock=True)
|
|
243
|
+
with Horizontal(id="body"):
|
|
244
|
+
yield DataTable(id="domains", zebra_stripes=True, cursor_type="row")
|
|
245
|
+
with VerticalScroll(id="detail-pane", can_focus=True):
|
|
246
|
+
yield Static("Loading domains…", id="detail")
|
|
247
|
+
with Vertical(id="status-bar"):
|
|
248
|
+
yield ProgressBar(
|
|
249
|
+
id="progress", total=1, show_percentage=False, show_eta=False
|
|
250
|
+
)
|
|
251
|
+
yield Static("starting…", id="status")
|
|
252
|
+
yield Footer()
|
|
253
|
+
|
|
254
|
+
def on_mount(self) -> None:
|
|
255
|
+
table = self.query_one("#domains", DataTable)
|
|
256
|
+
loaded = self._reload(initial=True)
|
|
257
|
+
table.focus()
|
|
258
|
+
if loaded and self._domains:
|
|
259
|
+
self._start_scan()
|
|
260
|
+
|
|
261
|
+
@on(DataTable.RowHighlighted, "#domains")
|
|
262
|
+
def _row_highlighted(self, event: DataTable.RowHighlighted) -> None:
|
|
263
|
+
"""Keep the detail pane in sync with the cursor, not just the scan."""
|
|
264
|
+
if event.row_key is not None:
|
|
265
|
+
self._show_detail(str(event.row_key.value))
|
|
266
|
+
|
|
267
|
+
# ------------------------------------------------------------- INI state
|
|
268
|
+
|
|
269
|
+
def _reload(self, *, initial: bool = False) -> bool:
|
|
270
|
+
"""(Re)read the domain file. Returns False on a load error."""
|
|
271
|
+
try:
|
|
272
|
+
domains = load_domains(self.domains_path)
|
|
273
|
+
except DomainFileError as exc:
|
|
274
|
+
self._load_error = str(exc)
|
|
275
|
+
self._set_status(f"cannot load {self.domains_path}: {exc}")
|
|
276
|
+
self.notify(
|
|
277
|
+
f"cannot load {self.domains_path}: {exc}",
|
|
278
|
+
severity="error",
|
|
279
|
+
timeout=10,
|
|
280
|
+
)
|
|
281
|
+
if initial:
|
|
282
|
+
self._domains = []
|
|
283
|
+
self._results.clear()
|
|
284
|
+
self._rebuild_table()
|
|
285
|
+
self._show_detail(None)
|
|
286
|
+
return False
|
|
287
|
+
|
|
288
|
+
self._load_error = None
|
|
289
|
+
self._domains = domains
|
|
290
|
+
self._results.clear()
|
|
291
|
+
self._rebuild_table()
|
|
292
|
+
if not domains:
|
|
293
|
+
self._set_status(f"no enabled domains in {self.domains_path}")
|
|
294
|
+
self.notify("no enabled domains found", severity="warning")
|
|
295
|
+
return True
|
|
296
|
+
|
|
297
|
+
def _rebuild_table(self) -> None:
|
|
298
|
+
table = self.query_one("#domains", DataTable)
|
|
299
|
+
table.clear(columns=True)
|
|
300
|
+
self._table_keys.clear()
|
|
301
|
+
for column_key, label, width in COLUMNS:
|
|
302
|
+
table.add_column(label, key=column_key, width=width)
|
|
303
|
+
for domain in self._domains:
|
|
304
|
+
table.add_row(
|
|
305
|
+
_domain_text(domain),
|
|
306
|
+
_grade_text(None),
|
|
307
|
+
Text("…", style="dim"),
|
|
308
|
+
Text("", style="dim"),
|
|
309
|
+
Text("", style="dim"),
|
|
310
|
+
Text("", style="dim"),
|
|
311
|
+
Text("?", style="dim"),
|
|
312
|
+
Text("—", style="dim"),
|
|
313
|
+
Text("· · · ·", style="dim"),
|
|
314
|
+
key=domain.key,
|
|
315
|
+
)
|
|
316
|
+
self._table_keys.add(domain.key)
|
|
317
|
+
if self._domains:
|
|
318
|
+
table.cursor_coordinate = Coordinate(0, 0)
|
|
319
|
+
self._show_detail(self._domains[0].key)
|
|
320
|
+
else:
|
|
321
|
+
self._selected_key = None
|
|
322
|
+
self._show_detail(None)
|
|
323
|
+
|
|
324
|
+
# --------------------------------------------------------------- scanning
|
|
325
|
+
|
|
326
|
+
def _start_scan(self, targets: list[Domain] | None = None) -> None:
|
|
327
|
+
if not self._domains:
|
|
328
|
+
self._set_status("no domains to scan")
|
|
329
|
+
return
|
|
330
|
+
batch = list(targets) if targets is not None else list(self._domains)
|
|
331
|
+
if not batch:
|
|
332
|
+
return
|
|
333
|
+
self._generation += 1
|
|
334
|
+
self.query_one(ProgressBar).update(total=len(batch), progress=0)
|
|
335
|
+
self._set_status(f"scanning 0/{len(batch)}")
|
|
336
|
+
self._scan(batch, self._generation)
|
|
337
|
+
|
|
338
|
+
@work(thread=True, exclusive=True, group="scan")
|
|
339
|
+
def _scan(self, targets: list[Domain], generation: int) -> None:
|
|
340
|
+
"""Worker: probe every target, streaming each result to the UI."""
|
|
341
|
+
started = time.monotonic()
|
|
342
|
+
total = len(targets)
|
|
343
|
+
done = 0
|
|
344
|
+
try:
|
|
345
|
+
with ThreadPoolExecutor(
|
|
346
|
+
max_workers=min(6, total), thread_name_prefix="tlscheck"
|
|
347
|
+
) as pool:
|
|
348
|
+
futures = {
|
|
349
|
+
pool.submit(_run_probe, d.host, d.port, d.label, d.auth, d.path): d
|
|
350
|
+
for d in targets
|
|
351
|
+
}
|
|
352
|
+
for future in as_completed(futures):
|
|
353
|
+
domain = futures[future]
|
|
354
|
+
try:
|
|
355
|
+
result = future.result()
|
|
356
|
+
except Exception as exc: # never let one host kill the scan
|
|
357
|
+
result = _failure(domain, exc)
|
|
358
|
+
done += 1
|
|
359
|
+
elapsed = time.monotonic() - started
|
|
360
|
+
if not self._call_ui(
|
|
361
|
+
self._apply_result, generation, result, done, total, elapsed
|
|
362
|
+
):
|
|
363
|
+
return
|
|
364
|
+
except Exception as exc:
|
|
365
|
+
self._call_ui(self._scan_failed, generation, exc)
|
|
366
|
+
return
|
|
367
|
+
self._call_ui(self._scan_done, generation, done, time.monotonic() - started)
|
|
368
|
+
|
|
369
|
+
def _call_ui(self, callback: Callable[..., Any], *args: Any) -> bool:
|
|
370
|
+
"""Schedule a UI callback from the worker thread; False if the app is gone."""
|
|
371
|
+
try:
|
|
372
|
+
self.call_from_thread(callback, *args)
|
|
373
|
+
except Exception:
|
|
374
|
+
return False
|
|
375
|
+
return True
|
|
376
|
+
|
|
377
|
+
# --------------------------------------------------------- UI callbacks
|
|
378
|
+
|
|
379
|
+
def _apply_result(
|
|
380
|
+
self,
|
|
381
|
+
generation: int,
|
|
382
|
+
result: ProbeResult,
|
|
383
|
+
done: int,
|
|
384
|
+
total: int,
|
|
385
|
+
elapsed: float,
|
|
386
|
+
) -> None:
|
|
387
|
+
if generation != self._generation:
|
|
388
|
+
return
|
|
389
|
+
key = f"{result.host}:{result.port}"
|
|
390
|
+
self._results[key] = result
|
|
391
|
+
self._update_row(result)
|
|
392
|
+
self.query_one(ProgressBar).update(progress=done, total=total)
|
|
393
|
+
self._set_status(f"scanning {done}/{total}")
|
|
394
|
+
if key == self._selected_key:
|
|
395
|
+
self._show_detail(key)
|
|
396
|
+
|
|
397
|
+
def _scan_done(self, generation: int, count: int, elapsed: float) -> None:
|
|
398
|
+
if generation != self._generation:
|
|
399
|
+
return
|
|
400
|
+
progress = self.query_one(ProgressBar)
|
|
401
|
+
progress.update(progress=count, total=max(count, 1))
|
|
402
|
+
if count:
|
|
403
|
+
self._set_status(f"done in {elapsed:.1f}s · {count} host(s)")
|
|
404
|
+
else:
|
|
405
|
+
self._set_status("done · nothing scanned")
|
|
406
|
+
|
|
407
|
+
def _scan_failed(self, generation: int, exc: BaseException) -> None:
|
|
408
|
+
if generation != self._generation:
|
|
409
|
+
return
|
|
410
|
+
message = f"scan failed: {exc!r}"
|
|
411
|
+
self._set_status(message)
|
|
412
|
+
self.notify(message, severity="error", timeout=10)
|
|
413
|
+
|
|
414
|
+
def _update_row(self, result: ProbeResult) -> None:
|
|
415
|
+
key = f"{result.host}:{result.port}"
|
|
416
|
+
cells = (
|
|
417
|
+
_domain_text_for_result(result),
|
|
418
|
+
_grade_text(result.grade),
|
|
419
|
+
Text(_short_cipher(result.best_cipher), style="dim"),
|
|
420
|
+
Text(result.best_kex or "", style="bold" if result.pq else ""),
|
|
421
|
+
_http_text(result),
|
|
422
|
+
_hsts_text(result),
|
|
423
|
+
Text("yes", style="bold green")
|
|
424
|
+
if (result.pq or result.pq_groups)
|
|
425
|
+
else Text("no", style="dim"),
|
|
426
|
+
_cert_text(result),
|
|
427
|
+
_version_text(result),
|
|
428
|
+
)
|
|
429
|
+
table = self.query_one("#domains", DataTable)
|
|
430
|
+
if key in self._table_keys:
|
|
431
|
+
for (column_key, _, _), value in zip(COLUMNS, cells):
|
|
432
|
+
table.update_cell(key, column_key, value)
|
|
433
|
+
else:
|
|
434
|
+
table.add_row(*cells, key=key)
|
|
435
|
+
self._table_keys.add(key)
|
|
436
|
+
|
|
437
|
+
def _set_status(self, message: str) -> None:
|
|
438
|
+
self.query_one("#status", Static).update(Text(message, style="dim"))
|
|
439
|
+
|
|
440
|
+
# ---------------------------------------------------------------- detail
|
|
441
|
+
|
|
442
|
+
def _detail_renderable(self, key: str | None) -> Text:
|
|
443
|
+
text = Text()
|
|
444
|
+
if key is None:
|
|
445
|
+
if self._load_error:
|
|
446
|
+
text.append("Could not load domains\n", style="bold red")
|
|
447
|
+
text.append(self._load_error, style="red")
|
|
448
|
+
text.append(
|
|
449
|
+
f"\n\nFix {self.domains_path} and press l to reload.\n",
|
|
450
|
+
style="dim",
|
|
451
|
+
)
|
|
452
|
+
else:
|
|
453
|
+
text.append("No domains\n", style="bold")
|
|
454
|
+
text.append(
|
|
455
|
+
f"{self.domains_path} has no enabled entries.\n", style="dim"
|
|
456
|
+
)
|
|
457
|
+
text.append("Edit the file and press l to reload.\n", style="dim")
|
|
458
|
+
return text
|
|
459
|
+
|
|
460
|
+
domain = next((d for d in self._domains if d.key == key), None)
|
|
461
|
+
result = self._results.get(key)
|
|
462
|
+
if domain is None and result is None:
|
|
463
|
+
text.append("Unknown selection", style="dim")
|
|
464
|
+
return text
|
|
465
|
+
|
|
466
|
+
label = domain.label if domain else (result.name if result else key)
|
|
467
|
+
host = domain.host if domain else (result.host if result else key)
|
|
468
|
+
port = domain.port if domain else (result.port if result else 443)
|
|
469
|
+
|
|
470
|
+
text.append(label + "\n", style="bold underline")
|
|
471
|
+
text.append(f"{host}:{port}\n\n", style="dim")
|
|
472
|
+
|
|
473
|
+
if result is None:
|
|
474
|
+
text.append("scanning…\n", style="italic yellow")
|
|
475
|
+
return text
|
|
476
|
+
|
|
477
|
+
self._detail_row(text, "Grade", result.grade or "?", _grade_style(result.grade))
|
|
478
|
+
if result.ok:
|
|
479
|
+
self._detail_row(text, "Status", "ok")
|
|
480
|
+
else:
|
|
481
|
+
self._detail_row(text, "Status", "error", "red")
|
|
482
|
+
text.append("\n")
|
|
483
|
+
|
|
484
|
+
self._detail_row(text, "Best TLS", result.best_version or "none")
|
|
485
|
+
self._detail_row(text, "Cipher", result.best_cipher or "—")
|
|
486
|
+
kex = result.best_kex or "—"
|
|
487
|
+
self._detail_row(text, "Key exch.", kex)
|
|
488
|
+
groups = ", ".join(result.pq_groups) if result.pq_groups else "none"
|
|
489
|
+
self._detail_row(
|
|
490
|
+
text,
|
|
491
|
+
"PQ groups",
|
|
492
|
+
groups,
|
|
493
|
+
"bold green" if (result.pq or result.pq_groups) else "dim",
|
|
494
|
+
)
|
|
495
|
+
if result.http_status is not None:
|
|
496
|
+
note = f" {result.http_note}" if result.http_note else ""
|
|
497
|
+
if 200 <= result.http_status < 300:
|
|
498
|
+
http_style = "green"
|
|
499
|
+
elif result.http_status in (401, 403):
|
|
500
|
+
http_style = "orange1"
|
|
501
|
+
else:
|
|
502
|
+
http_style = ""
|
|
503
|
+
self._detail_row(text, "HTTP", f"{result.http_status}{note}", http_style)
|
|
504
|
+
elif result.http_note:
|
|
505
|
+
self._detail_row(text, "HTTP", result.http_note, "dim")
|
|
506
|
+
if result.hsts_header:
|
|
507
|
+
bits = []
|
|
508
|
+
if result.hsts_max_age is not None:
|
|
509
|
+
bits.append(_human_age(result.hsts_max_age))
|
|
510
|
+
if result.hsts_include_subdomains:
|
|
511
|
+
bits.append("subdomains")
|
|
512
|
+
if result.hsts_preload:
|
|
513
|
+
bits.append("preload")
|
|
514
|
+
style = "green" if (result.hsts_max_age or 0) > 0 else "orange1"
|
|
515
|
+
self._detail_row(text, "HSTS", ", ".join(bits) or "header present", style)
|
|
516
|
+
elif result.http_status is not None:
|
|
517
|
+
self._detail_row(text, "HSTS", "missing", "orange1")
|
|
518
|
+
if result.duration_s:
|
|
519
|
+
self._detail_row(text, "Duration", f"{result.duration_s:.2f}s")
|
|
520
|
+
if result.scanned_at:
|
|
521
|
+
self._detail_row(text, "Scanned", result.scanned_at)
|
|
522
|
+
text.append("\n")
|
|
523
|
+
|
|
524
|
+
text.append("Versions\n", style="bold")
|
|
525
|
+
for version in VERSIONS:
|
|
526
|
+
entry = result.versions.get(version)
|
|
527
|
+
short = version.removeprefix("TLSv")
|
|
528
|
+
if entry is None:
|
|
529
|
+
text.append(f" {short:<4} ", style="dim")
|
|
530
|
+
text.append("— no data\n", style="dim")
|
|
531
|
+
continue
|
|
532
|
+
if entry.supported:
|
|
533
|
+
legacy = short in ("1.1", "1.0")
|
|
534
|
+
style = "bold red" if legacy else "green"
|
|
535
|
+
text.append(f" {short:<4} ", style=style)
|
|
536
|
+
text.append("yes\n", style=style)
|
|
537
|
+
text.append(f" {entry.cipher or '—'}\n", style="")
|
|
538
|
+
if entry.kex:
|
|
539
|
+
text.append(f" {entry.kex}", style="")
|
|
540
|
+
if entry.kex_bits:
|
|
541
|
+
text.append(f" ({entry.kex_bits} bit)", style="dim")
|
|
542
|
+
if entry.pq:
|
|
543
|
+
text.append(" PQ", style="bold green")
|
|
544
|
+
text.append("\n")
|
|
545
|
+
else:
|
|
546
|
+
text.append(f" {short:<4} ", style="dim")
|
|
547
|
+
text.append("no", style="red")
|
|
548
|
+
if entry.error:
|
|
549
|
+
text.append(f"\n {entry.error}", style="dim")
|
|
550
|
+
text.append("\n")
|
|
551
|
+
text.append("\n")
|
|
552
|
+
|
|
553
|
+
text.append("Certificate\n", style="bold")
|
|
554
|
+
if result.cert_subject or result.cert_issuer or result.cert_not_after:
|
|
555
|
+
self._detail_row(text, "subject", result.cert_subject or "—")
|
|
556
|
+
self._detail_row(text, "issuer", result.cert_issuer or "—")
|
|
557
|
+
expiry = result.cert_not_after or "—"
|
|
558
|
+
if result.cert_days_left is not None:
|
|
559
|
+
expiry += f" ({result.cert_days_left} days)"
|
|
560
|
+
style = ""
|
|
561
|
+
if result.cert_days_left is not None and result.cert_days_left <= 30:
|
|
562
|
+
style = "orange1"
|
|
563
|
+
self._detail_row(text, "expires", expiry, style)
|
|
564
|
+
if result.cert_trusted is None:
|
|
565
|
+
trust = "unknown"
|
|
566
|
+
elif result.cert_trusted:
|
|
567
|
+
trust = "yes"
|
|
568
|
+
else:
|
|
569
|
+
trust = "NO"
|
|
570
|
+
self._detail_row(
|
|
571
|
+
text, "trusted", trust, "green" if result.cert_trusted else "red"
|
|
572
|
+
)
|
|
573
|
+
else:
|
|
574
|
+
text.append(" no certificate information\n", style="dim")
|
|
575
|
+
text.append("\n")
|
|
576
|
+
|
|
577
|
+
text.append("Flags\n", style="bold")
|
|
578
|
+
if result.flags:
|
|
579
|
+
for flag in result.flags:
|
|
580
|
+
text.append(f" · {flag}\n", style="orange1")
|
|
581
|
+
else:
|
|
582
|
+
text.append(" none\n", style="dim")
|
|
583
|
+
|
|
584
|
+
if result.error:
|
|
585
|
+
text.append("\nError\n", style="bold red")
|
|
586
|
+
text.append(result.error + "\n", style="red")
|
|
587
|
+
|
|
588
|
+
return text
|
|
589
|
+
|
|
590
|
+
@staticmethod
|
|
591
|
+
def _detail_row(text: Text, label: str, value: str, style: str = "") -> None:
|
|
592
|
+
text.append(f"{label:<10}", style="dim")
|
|
593
|
+
text.append(str(value) + "\n", style=style)
|
|
594
|
+
|
|
595
|
+
def _show_detail(self, key: str | None) -> None:
|
|
596
|
+
self._selected_key = key
|
|
597
|
+
self.query_one("#detail", Static).update(self._detail_renderable(key))
|
|
598
|
+
|
|
599
|
+
# -------------------------------------------------------------- bindings
|
|
600
|
+
|
|
601
|
+
def _current_domain(self) -> Domain | None:
|
|
602
|
+
if not self._domains:
|
|
603
|
+
return None
|
|
604
|
+
table = self.query_one("#domains", DataTable)
|
|
605
|
+
row = table.cursor_row
|
|
606
|
+
if 0 <= row < len(self._domains):
|
|
607
|
+
return self._domains[row]
|
|
608
|
+
return None
|
|
609
|
+
|
|
610
|
+
def action_rescan_all(self) -> None:
|
|
611
|
+
if not self._domains:
|
|
612
|
+
self.notify("no domains loaded", severity="warning")
|
|
613
|
+
return
|
|
614
|
+
self._start_scan()
|
|
615
|
+
self.notify(f"rescanning {len(self._domains)} domain(s)")
|
|
616
|
+
|
|
617
|
+
def action_rescan_selected(self) -> None:
|
|
618
|
+
domain = self._current_domain()
|
|
619
|
+
if domain is None:
|
|
620
|
+
self.notify("no row selected", severity="warning")
|
|
621
|
+
return
|
|
622
|
+
self._start_scan([domain])
|
|
623
|
+
self.notify(f"rescanning {domain.label}")
|
|
624
|
+
|
|
625
|
+
def action_reload_domains(self) -> None:
|
|
626
|
+
if not self._reload(initial=False):
|
|
627
|
+
return
|
|
628
|
+
self.notify(f"loaded {len(self._domains)} domain(s)")
|
|
629
|
+
if self._domains:
|
|
630
|
+
self._start_scan()
|
|
631
|
+
|
|
632
|
+
def action_focus_toggle(self) -> None:
|
|
633
|
+
table = self.query_one("#domains", DataTable)
|
|
634
|
+
detail = self.query_one("#detail-pane", VerticalScroll)
|
|
635
|
+
if self.focused is table:
|
|
636
|
+
detail.focus()
|
|
637
|
+
else:
|
|
638
|
+
table.focus()
|
|
639
|
+
|
|
640
|
+
def action_focus_detail(self) -> None:
|
|
641
|
+
self.query_one("#detail-pane", VerticalScroll).focus()
|
|
642
|
+
|
|
643
|
+
|
|
644
|
+
def _grade_style(grade: str | None) -> str:
|
|
645
|
+
return GRADE_STYLES.get((grade or "").upper(), "dim")
|
|
646
|
+
|
|
647
|
+
|
|
648
|
+
def _domain_text_for_result(result: ProbeResult) -> Text:
|
|
649
|
+
text = Text(result.name or result.host, style="bold")
|
|
650
|
+
if result.label and result.label != result.host:
|
|
651
|
+
text.append("\n" + result.host, style="dim")
|
|
652
|
+
return text
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
656
|
+
parser = argparse.ArgumentParser(
|
|
657
|
+
prog="tlscheck",
|
|
658
|
+
description="Scan TLS endpoints and display their protocol, cipher and PQ support in a TUI.",
|
|
659
|
+
)
|
|
660
|
+
parser.add_argument(
|
|
661
|
+
"--domains",
|
|
662
|
+
type=Path,
|
|
663
|
+
default=Path("domains.ini"),
|
|
664
|
+
metavar="PATH",
|
|
665
|
+
help="INI file with one [host] section per target (default: domains.ini)",
|
|
666
|
+
)
|
|
667
|
+
return parser
|
|
668
|
+
|
|
669
|
+
|
|
670
|
+
def main(argv: list[str] | None = None) -> None:
|
|
671
|
+
args = build_parser().parse_args(argv)
|
|
672
|
+
TlsCheckApp(domains_path=args.domains).run()
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
if __name__ == "__main__":
|
|
676
|
+
main()
|