|
| 1 | +package output |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/TMHSDigital/subenum/internal/dns" |
| 9 | +) |
| 10 | + |
| 11 | +// SummarySchema versions the Summary object. Fields may be added without a |
| 12 | +// bump; renaming or removing one, or changing its meaning, bumps it (#70). |
| 13 | +const SummarySchema = 1 |
| 14 | + |
| 15 | +// Summary is the machine-readable run-quality report: what was scanned, how |
| 16 | +// many queries were sent, how every lookup ended, and a verdict on how far the |
| 17 | +// results can be trusted. It is written by -stats and, in jsonl format, as the |
| 18 | +// last line of the output. |
| 19 | +type Summary struct { |
| 20 | + Type string `json:"type"` // always "summary" |
| 21 | + Schema int `json:"schema"` |
| 22 | + Tool string `json:"tool"` |
| 23 | + Version string `json:"version"` |
| 24 | + Started time.Time `json:"started"` |
| 25 | + DurationMs int64 `json:"duration_ms"` |
| 26 | + Simulated bool `json:"simulated"` |
| 27 | + Seed uint64 `json:"seed,omitempty"` |
| 28 | + Resolver string `json:"resolver,omitempty"` |
| 29 | + RateLimit int `json:"rate_limit"` |
| 30 | + QueriesSent int64 `json:"queries_sent"` |
| 31 | + AchievedQPS float64 `json:"achieved_qps"` |
| 32 | + Verdict string `json:"verdict"` |
| 33 | + Reason string `json:"reason"` |
| 34 | + Targets []TargetSummary `json:"targets"` |
| 35 | +} |
| 36 | + |
| 37 | +// TargetSummary is one scanned domain's part of a Summary. |
| 38 | +type TargetSummary struct { |
| 39 | + Domain string `json:"domain"` |
| 40 | + Status string `json:"status"` // ok, failed, skipped, interrupted, not_run |
| 41 | + Error string `json:"error,omitempty"` |
| 42 | + Processed int64 `json:"processed"` |
| 43 | + Total int64 `json:"total"` |
| 44 | + Found int64 `json:"found"` |
| 45 | + Outcomes Outcomes `json:"outcomes"` |
| 46 | + QueriesSent int64 `json:"queries_sent"` |
| 47 | + SkippedCap int64 `json:"skipped_by_cap"` |
| 48 | + Wildcard bool `json:"wildcard"` |
| 49 | + Fingerprint []dns.Record `json:"wildcard_fingerprint,omitempty"` |
| 50 | + Verdict string `json:"verdict"` |
| 51 | + Reason string `json:"reason"` |
| 52 | +} |
| 53 | + |
| 54 | +// Outcomes counts how each lookup ended. SERVFAIL and other errors Go's |
| 55 | +// resolver does not distinguish are counted under Other. |
| 56 | +type Outcomes struct { |
| 57 | + Resolved int64 `json:"resolved"` |
| 58 | + NXDomain int64 `json:"nxdomain"` |
| 59 | + Timeout int64 `json:"timeout"` |
| 60 | + Refused int64 `json:"refused"` |
| 61 | + Other int64 `json:"other"` |
| 62 | + WildcardFiltered int64 `json:"wildcard_filtered"` |
| 63 | +} |
| 64 | + |
| 65 | +// Summary writes s as the final line of jsonl output (stdout and the -o |
| 66 | +// file). Other formats ignore it; use -stats for those. |
| 67 | +func (w *Writer) Summary(s Summary) { |
| 68 | + if w.format != FormatJSONL { |
| 69 | + return |
| 70 | + } |
| 71 | + w.mu.Lock() |
| 72 | + defer w.mu.Unlock() |
| 73 | + data, err := json.Marshal(s) |
| 74 | + if err != nil { |
| 75 | + _, _ = fmt.Fprintf(w.errOut(), "Error: encoding summary: %v\n", err) |
| 76 | + return |
| 77 | + } |
| 78 | + if w.stdout { |
| 79 | + w.clearProgressLocked() |
| 80 | + fmt.Printf("%s\n", data) |
| 81 | + } |
| 82 | + if w.outWriter != nil { |
| 83 | + _, _ = fmt.Fprintf(w.outWriter, "%s\n", data) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// WriteSummaryFile writes s as indented JSON to path, atomically. |
| 88 | +func WriteSummaryFile(path string, s Summary) error { |
| 89 | + f, err := CreateFile(path) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + data, err := json.MarshalIndent(s, "", " ") |
| 94 | + if err != nil { |
| 95 | + _ = f.Close(false) |
| 96 | + return err |
| 97 | + } |
| 98 | + _, werr := f.Write(append(data, '\n')) |
| 99 | + if cerr := f.Close(werr == nil); werr == nil { |
| 100 | + werr = cerr |
| 101 | + } |
| 102 | + return werr |
| 103 | +} |
0 commit comments