feat: rework engine and CLI for dnstraverse parity
Port the traversal engine to the Ruby dnstraverse model so behaviour and output match dns.squish.net: - dns: single RD=0 query path (RD=1 only for upstream root discovery), per-run packet cache, EDNS0 512-fallback with warnings, UDP->TCP on truncation; fix --retries 0 and --root-server IP-literal handling; drop all hardcoded 127.0.0.1:53 resolvers - traverse: hierarchical per-branch InfoCache, 7-step response classification with the full 10-status vocabulary, bailiwick partitioning, strictly-deeper lame-referral rule, refid grammar with .0 resolve subtrees and childset digits, per-IP branching at 1/n weight, cache-based glue resolution with noglue/loop dead ends, CNAME restarts from the deepest cached zone, fast-mode memoization, probability aggregation with Ruby-identical stats keys (sums to 1.0) - output: byte-for-byte reference text format pinned by a golden test, reference CLI defaults, working --quiet/--show-X=false, TTY-aware colour, deduplicated deterministic JSON - web: adapt API/SPA to the new engine, SSE events carry refid/status, fix subscribe/snapshot duplicate-event race and a statusCls TDZ bug, align SPA type list with the backend - delete the old engine and dead code (net -4,350 lines) Verified against live runs of the reference Ruby engine across five domains (answers, NXDOMAIN, null MX, CNAME restart, glueless resolve) with no divergences beyond the documented typo fixes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
af15c9c2d4
commit
d71c7fbef2
@@ -1,10 +1,11 @@
|
||||
// Package output renders ExploreDNS traversal results for human consumption or
|
||||
// machine processing.
|
||||
// Package output renders ExploreDNS traversal results for human consumption
|
||||
// or machine processing.
|
||||
//
|
||||
// Two formats are supported:
|
||||
//
|
||||
// - FormatText — a coloured hierarchical tree (default)
|
||||
// - FormatJSON — a JSON array of traversal results
|
||||
// - FormatText — dnstraverse-style header/progress/results/summary text
|
||||
// (default)
|
||||
// - FormatJSON — a single JSON document with the aggregated results
|
||||
//
|
||||
// Create a Formatter via NewFormatter and call RunTraversal to drive the
|
||||
// traversal engine and stream output incrementally.
|
||||
@@ -26,9 +27,19 @@ const (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Format Format
|
||||
Domain string
|
||||
QueryType string
|
||||
Format Format
|
||||
Domain string
|
||||
QueryType string
|
||||
|
||||
// Engine settings echoed in the header block (bin/dnstraverse).
|
||||
Fast bool
|
||||
AllRootServers bool
|
||||
UDPSize int
|
||||
Retries int
|
||||
MaxDepth int
|
||||
AllowTCP bool
|
||||
AlwaysTCP bool
|
||||
|
||||
ShowProgress bool
|
||||
ShowResolves bool
|
||||
ShowServers bool
|
||||
@@ -49,22 +60,50 @@ type Config struct {
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Format: FormatText,
|
||||
Fast: true,
|
||||
UDPSize: 2048,
|
||||
Retries: 2,
|
||||
MaxDepth: 20,
|
||||
AllowTCP: true,
|
||||
ShowProgress: true,
|
||||
ShowResolves: true,
|
||||
ShowServers: true,
|
||||
ShowResolves: false,
|
||||
ShowServers: false,
|
||||
ShowVersions: true,
|
||||
ShowAllStats: true,
|
||||
ShowAllStats: false,
|
||||
ShowResults: true,
|
||||
ShowSummaryResults: true,
|
||||
Color: os.Getenv("NO_COLOR") == "",
|
||||
Color: ColorEnabled(os.Stdout),
|
||||
}
|
||||
}
|
||||
|
||||
// ColorEnabled reports whether colour output should be used for w: only when
|
||||
// NO_COLOR is unset and w is a terminal.
|
||||
func ColorEnabled(w io.Writer) bool {
|
||||
if os.Getenv("NO_COLOR") != "" {
|
||||
return false
|
||||
}
|
||||
f, ok := w.(*os.File)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return info.Mode()&os.ModeCharDevice != 0
|
||||
}
|
||||
|
||||
// Formatter renders traversal progress and the aggregated results. The header
|
||||
// is written once before the run, progress arrives via the traverser hooks,
|
||||
// and the aggregated leaves and the servers seen arrive once the run
|
||||
// completed.
|
||||
type Formatter interface {
|
||||
// WriteHeader renders the pre-run header block from the discovered roots
|
||||
// (suppressed entirely by --quiet in text mode).
|
||||
WriteHeader(roots []traverse.StartServer) error
|
||||
WriteProgress(event traverse.TraversalEvent) error
|
||||
WriteResolve(event traverse.TraversalEvent) error
|
||||
WriteResult(result traverse.TraversalResult) error
|
||||
WriteSummary(results []traverse.TraversalResult) error
|
||||
WriteSummary(root *traverse.Referral, servers map[string][]string) error
|
||||
Flush() error
|
||||
}
|
||||
|
||||
@@ -85,6 +124,11 @@ func AttachHooks(cfg *Config, formatter Formatter) *traverse.TraverserHooks {
|
||||
if cfg == nil || formatter == nil {
|
||||
return nil
|
||||
}
|
||||
if !cfg.ShowProgress {
|
||||
// Ruby registers no progress callbacks at all without show-progress;
|
||||
// resolve display additionally requires show-resolves.
|
||||
return nil
|
||||
}
|
||||
logErr := func(context string, err error) {
|
||||
if err != nil && cfg.Debug > 0 {
|
||||
fmt.Fprintf(os.Stderr, "Debug: formatter %s: %v\n", context, err)
|
||||
@@ -92,15 +136,13 @@ func AttachHooks(cfg *Config, formatter Formatter) *traverse.TraverserHooks {
|
||||
}
|
||||
return &traverse.TraverserHooks{
|
||||
OnEvent: func(event traverse.TraversalEvent) {
|
||||
switch {
|
||||
case event.IsResolve && cfg.ShowResolves:
|
||||
logErr("WriteResolve", formatter.WriteResolve(event))
|
||||
case !event.IsResolve && cfg.ShowProgress:
|
||||
logErr("WriteProgress", formatter.WriteProgress(event))
|
||||
}
|
||||
if event.Stage == traverse.EventComplete && cfg.ShowAllStats {
|
||||
logErr("WriteResult", formatter.WriteResult(event.Result))
|
||||
if event.IsResolve {
|
||||
if cfg.ShowResolves {
|
||||
logErr("WriteResolve", formatter.WriteResolve(event))
|
||||
}
|
||||
return
|
||||
}
|
||||
logErr("WriteProgress", formatter.WriteProgress(event))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user