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:
Gary Hansen
2026-07-07 21:42:06 +10:00
co-authored by Claude Fable 5
parent af15c9c2d4
commit d71c7fbef2
53 changed files with 6685 additions and 9366 deletions
+82 -101
View File
@@ -3,11 +3,15 @@ package output
import (
"encoding/json"
"io"
"sort"
"strings"
"gitea.hansenits.com.au/hits/ExploreDNS/internal/dns"
"gitea.hansenits.com.au/hits/ExploreDNS/internal/traverse"
)
// jsonFormatter accumulates the run into a single document emitted once by
// Flush: {domain, qtype, root, results, summary, servers}. Aggregated leaves
// appear exactly once (in results); progress events are not recorded.
type jsonFormatter struct {
cfg *Config
w io.Writer
@@ -15,32 +19,31 @@ type jsonFormatter struct {
}
type jsonDocument struct {
Domain string `json:"domain"`
QueryType string `json:"query_type"`
Progress []jsonProgressEvent `json:"progress,omitempty"`
Resolves []jsonProgressEvent `json:"resolves,omitempty"`
Results []jsonResult `json:"results,omitempty"`
Servers []jsonServer `json:"servers,omitempty"`
Summary jsonSummary `json:"summary,omitempty"`
Domain string `json:"domain"`
QueryType string `json:"qtype"`
Root *jsonRoot `json:"root,omitempty"`
Results []jsonResult `json:"results,omitempty"`
Summary *jsonSummary `json:"summary,omitempty"`
Servers []jsonServer `json:"servers,omitempty"`
}
type jsonProgressEvent struct {
Stage string `json:"stage"`
Depth int `json:"depth"`
Name string `json:"name"`
QType string `json:"qtype"`
Server string `json:"server,omitempty"`
Bailiwick string `json:"bailiwick,omitempty"`
Resolving bool `json:"resolving,omitempty"`
// jsonRoot is the initial root the traversal started from.
type jsonRoot struct {
Name string `json:"name"`
IP string `json:"ip,omitempty"`
}
// jsonResult is one aggregated leaf outcome, emitted exactly once.
type jsonResult struct {
Depth int `json:"depth"`
Probability float64 `json:"probability"`
ResponseType string `json:"response_type"`
Server string `json:"server,omitempty"`
Answers []string `json:"answers,omitempty"`
CNAMEChain []string `json:"cname_chain,omitempty"`
RefID string `json:"refid,omitempty"`
Probability float64 `json:"probability"`
Status string `json:"status"`
Server string `json:"server,omitempty"`
IP string `json:"ip,omitempty"`
Qname string `json:"qname,omitempty"`
Qtype string `json:"qtype,omitempty"`
Answers []string `json:"answers,omitempty"`
Message string `json:"message,omitempty"`
}
type jsonServer struct {
@@ -50,12 +53,11 @@ type jsonServer struct {
}
type jsonSummary struct {
ByType map[string]float64 `json:"by_type,omitempty"`
Answers []jsonAnswerStat `json:"answers,omitempty"`
ByStatus map[string]float64 `json:"by_status,omitempty"`
Answers []jsonAnswerStat `json:"answers,omitempty"`
}
type jsonAnswerStat struct {
RData string `json:"rdata"`
Probability float64 `json:"probability"`
Records []string `json:"records,omitempty"`
}
@@ -71,43 +73,48 @@ func newJSONFormatter(cfg *Config, w io.Writer) *jsonFormatter {
}
}
func (f *jsonFormatter) WriteProgress(event traverse.TraversalEvent) error {
if !f.cfg.ShowProgress {
func (f *jsonFormatter) WriteHeader(roots []traverse.StartServer) error {
if len(roots) == 0 {
return nil
}
f.payload.Progress = append(f.payload.Progress, f.eventToJSON(event))
root := &jsonRoot{Name: roots[0].Name}
if len(roots[0].IPs) > 0 {
root.IP = roots[0].IPs[0]
}
f.payload.Root = root
return nil
}
func (f *jsonFormatter) WriteResolve(event traverse.TraversalEvent) error {
if !f.cfg.ShowResolves {
return nil
}
f.payload.Resolves = append(f.payload.Resolves, f.eventToJSON(event))
// WriteProgress is a no-op: the JSON document contains only the aggregated
// outcome, never per-event duplicates.
func (f *jsonFormatter) WriteProgress(traverse.TraversalEvent) error {
return nil
}
func (f *jsonFormatter) WriteResult(result traverse.TraversalResult) error {
if !f.cfg.ShowAllStats {
return nil
}
f.payload.Results = append(f.payload.Results, f.resultToJSON(result))
func (f *jsonFormatter) WriteResolve(traverse.TraversalEvent) error {
return nil
}
func (f *jsonFormatter) WriteSummary(results []traverse.TraversalResult) error {
if f.cfg.ShowResults {
for _, result := range terminalResults(results) {
f.payload.Results = append(f.payload.Results, f.resultToJSON(result))
func (f *jsonFormatter) WriteSummary(root *traverse.Referral, servers map[string][]string) error {
if root != nil && f.cfg.ShowResults {
// StatsList is sorted by stats key: deterministic ordering.
for _, leaf := range root.StatsList() {
f.payload.Results = append(f.payload.Results, leafToJSON(leaf))
}
}
if f.cfg.ShowServers {
servers := collectServers(results)
for name, ips := range servers {
srv := jsonServer{Name: name, IPs: ips}
names := make([]string, 0, len(servers))
for name := range servers {
names = append(names, name)
}
sort.Slice(names, func(i, j int) bool {
return reverseString(strings.ToLower(names[i])) < reverseString(strings.ToLower(names[j]))
})
for _, name := range names {
srv := jsonServer{Name: name, IPs: servers[name]}
if f.cfg.ShowVersions && f.cfg.Fingerprints != nil {
for _, ip := range ips {
for _, ip := range srv.IPs {
if v := f.cfg.Fingerprints[ip]; v != "" {
srv.Version = v
break
@@ -119,18 +126,19 @@ func (f *jsonFormatter) WriteSummary(results []traverse.TraversalResult) error {
}
if f.cfg.ShowSummaryResults {
stats := ComputeSummary(results)
if stats != nil {
f.payload.Summary = jsonSummary{
ByType: stats.ByType,
if stats := root.SummaryStats(); stats != nil {
summary := &jsonSummary{ByStatus: make(map[string]float64)}
for status, prob := range stats.ByStatus {
summary.ByStatus[string(status)] = prob
}
for _, answer := range stats.Answers {
f.payload.Summary.Answers = append(f.payload.Summary.Answers, jsonAnswerStat{
RData: answer.RData,
Probability: answer.Prob,
Records: answer.RRs,
})
stat := jsonAnswerStat{Probability: answer.Prob}
for _, rr := range answer.RRs {
stat.Records = append(stat.Records, collapseWhitespace(rr.String()))
}
summary.Answers = append(summary.Answers, stat)
}
f.payload.Summary = summary
}
}
@@ -143,56 +151,29 @@ func (f *jsonFormatter) Flush() error {
return enc.Encode(f.payload)
}
func (f *jsonFormatter) eventToJSON(event traverse.TraversalEvent) jsonProgressEvent {
ref := event.Result.Referral
if ref == nil {
return jsonProgressEvent{}
func leafToJSON(leaf *traverse.StatsEntry) jsonResult {
resp := leaf.Response
item := jsonResult{
Probability: leaf.Prob,
Status: string(resp.Status),
IP: resp.IP,
Qname: resp.Qname,
Qtype: traverse.TypeToString(resp.Qtype),
}
item := jsonProgressEvent{
Stage: stageName(event.Stage),
Depth: ref.Depth,
Name: trimDomain(ref.Name),
QType: dns.QNameType(ref.Qtype),
Bailiwick: trimDomain(ref.Bailiwick),
Resolving: !ref.HasAddresses(),
if leaf.Referral != nil {
item.RefID = leaf.Referral.RefID
item.Server = leaf.Referral.Server
}
if event.Result.Response != nil && event.Result.Response.Server != nil {
item.Server = event.Result.Response.Server.String()
} else {
item.Server = referralServerLabel(ref, event.Result.Response)
}
return item
}
func (f *jsonFormatter) resultToJSON(result traverse.TraversalResult) jsonResult {
item := jsonResult{}
if result.Referral != nil {
item.Depth = result.Referral.Depth
item.Probability = result.Referral.Prob
}
if result.Response != nil {
item.ResponseType = result.Response.Type.String()
if result.Response.Server != nil {
item.Server = result.Response.Server.String()
if resp.DQ != nil {
for _, rr := range resp.DQ.Answers {
item.Answers = append(item.Answers, collapseWhitespace(rr.String()))
}
if result.Response.Decoded != nil {
for _, rr := range result.Response.Decoded.Answers {
item.Answers = append(item.Answers, dns.FormatRecord(rr))
}
item.CNAMEChain = append(item.CNAMEChain, result.Response.Decoded.CNAMEChain...)
switch resp.Status {
case traverse.StatusError:
item.Message = resp.DQ.ErrorMessage
case traverse.StatusException:
item.Message = resp.DQ.ExceptionMessage
}
}
return item
}
func stageName(stage traverse.EventStage) string {
switch stage {
case traverse.EventStart:
return "start"
case traverse.EventComplete:
return "complete"
default:
return "unknown"
}
}