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>
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
package output
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
|
|
"gitea.hansenits.com.au/hits/ExploreDNS/internal/fingerprint"
|
|
"gitea.hansenits.com.au/hits/ExploreDNS/internal/traverse"
|
|
)
|
|
|
|
// RunTraversal drives one traversal and streams its output through the
|
|
// formatter. It returns the synthetic root referral whose Stats aggregate
|
|
// every leaf outcome.
|
|
func RunTraversal(ctx context.Context, traverser *traverse.Traverser, cfg *Config, formatter Formatter, domain string) (*traverse.Referral, error) {
|
|
if traverser == nil {
|
|
return nil, fmt.Errorf("traverser is required")
|
|
}
|
|
if cfg == nil {
|
|
cfg = DefaultConfig()
|
|
}
|
|
if formatter == nil {
|
|
formatter = NewFormatter(cfg, nil)
|
|
}
|
|
|
|
traverser.SetHooks(AttachHooks(cfg, formatter))
|
|
|
|
// Discover the roots up front so the header can report the initial root;
|
|
// Run reuses the memoised discovery.
|
|
roots, err := traverser.Roots(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := formatter.WriteHeader(roots); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
root, err := traverser.Run(ctx, domain)
|
|
if err != nil {
|
|
return root, err
|
|
}
|
|
|
|
servers := traverser.ServersEncountered()
|
|
|
|
// Fingerprint servers when both ShowVersions and ShowServers are enabled.
|
|
// Gating on ShowServers avoids unnecessary network calls when versions
|
|
// would not be displayed anyway.
|
|
if cfg.ShowVersions && cfg.ShowServers {
|
|
cfg.Fingerprints = fingerprint.New().FingerprintAll(ctx, collectUniqueServerIPs(servers))
|
|
}
|
|
|
|
if err := formatter.WriteSummary(root, servers); err != nil {
|
|
return root, err
|
|
}
|
|
if err := formatter.Flush(); err != nil {
|
|
return root, err
|
|
}
|
|
|
|
return root, nil
|
|
}
|
|
|
|
// collectUniqueServerIPs returns the set of unique server IPs encountered.
|
|
func collectUniqueServerIPs(servers map[string][]string) []net.IP {
|
|
seen := make(map[string]bool)
|
|
var ips []net.IP
|
|
for _, addrs := range servers {
|
|
for _, addr := range addrs {
|
|
if seen[addr] {
|
|
continue
|
|
}
|
|
seen[addr] = true
|
|
if ip := net.ParseIP(addr); ip != nil {
|
|
ips = append(ips, ip)
|
|
}
|
|
}
|
|
}
|
|
return ips
|
|
}
|