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>
149 lines
3.7 KiB
Go
149 lines
3.7 KiB
Go
// Package output renders ExploreDNS traversal results for human consumption
|
|
// or machine processing.
|
|
//
|
|
// Two formats are supported:
|
|
//
|
|
// - 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.
|
|
package output
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"gitea.hansenits.com.au/hits/ExploreDNS/internal/traverse"
|
|
)
|
|
|
|
type Format int
|
|
|
|
const (
|
|
FormatText Format = iota
|
|
FormatJSON
|
|
)
|
|
|
|
type Config struct {
|
|
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
|
|
ShowVersions bool
|
|
ShowAllStats bool
|
|
ShowResults bool
|
|
ShowSummaryResults bool
|
|
Verbose bool
|
|
Quiet bool
|
|
Color bool
|
|
Debug int
|
|
|
|
// Fingerprints maps server IP strings to their version.bind version strings.
|
|
// Populated by RunTraversal when ShowVersions and ShowServers are both true.
|
|
Fingerprints map[string]string
|
|
}
|
|
|
|
func DefaultConfig() *Config {
|
|
return &Config{
|
|
Format: FormatText,
|
|
Fast: true,
|
|
UDPSize: 2048,
|
|
Retries: 2,
|
|
MaxDepth: 20,
|
|
AllowTCP: true,
|
|
ShowProgress: true,
|
|
ShowResolves: false,
|
|
ShowServers: false,
|
|
ShowVersions: true,
|
|
ShowAllStats: false,
|
|
ShowResults: true,
|
|
ShowSummaryResults: true,
|
|
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
|
|
WriteSummary(root *traverse.Referral, servers map[string][]string) error
|
|
Flush() error
|
|
}
|
|
|
|
func NewFormatter(cfg *Config, w io.Writer) Formatter {
|
|
if cfg == nil {
|
|
cfg = DefaultConfig()
|
|
}
|
|
if w == nil {
|
|
w = os.Stdout
|
|
}
|
|
if cfg.Format == FormatJSON {
|
|
return newJSONFormatter(cfg, w)
|
|
}
|
|
return newTextFormatter(cfg, w)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
return &traverse.TraverserHooks{
|
|
OnEvent: func(event traverse.TraversalEvent) {
|
|
if event.IsResolve {
|
|
if cfg.ShowResolves {
|
|
logErr("WriteResolve", formatter.WriteResolve(event))
|
|
}
|
|
return
|
|
}
|
|
logErr("WriteProgress", formatter.WriteProgress(event))
|
|
},
|
|
}
|
|
}
|