|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/miekg/dns" |
| 11 | + "github.com/fatih/color" |
| 12 | + "github.com/juju/ansiterm" |
| 13 | +) |
| 14 | + |
| 15 | +// displayRecords prints the DNS records to the terminal. |
| 16 | +func displayRecords(domainName string, messages []*dns.Msg) { |
| 17 | + w := ansiterm.NewTabWriter(os.Stdout, 8, 8, 4, ' ', 0) |
| 18 | + w.SetColorCapable(true) |
| 19 | + |
| 20 | + domainColored := color.HiBlueString(domainName) |
| 21 | + |
| 22 | + for _, msg := range messages { |
| 23 | + for _, answer := range msg.Answer { |
| 24 | + queryType := dns.TypeToString[answer.Header().Rrtype] |
| 25 | + formattedTTL := color.HiMagentaString(formatTTL(answer.Header().Ttl)) |
| 26 | + |
| 27 | + switch rec := answer.(type) { |
| 28 | + case *dns.A: |
| 29 | + printARecord(w, queryType, domainColored, formattedTTL, rec) |
| 30 | + |
| 31 | + case *dns.AAAA: |
| 32 | + printAAAARecord(w, queryType, domainColored, formattedTTL, rec) |
| 33 | + |
| 34 | + case *dns.CNAME: |
| 35 | + printCNAMERecord(w, queryType, domainColored, formattedTTL, rec) |
| 36 | + |
| 37 | + case *dns.MX: |
| 38 | + printMXRecord(w, queryType, domainColored, formattedTTL, rec) |
| 39 | + |
| 40 | + case *dns.TXT: |
| 41 | + printTXTRecord(w, queryType, domainColored, formattedTTL, rec) |
| 42 | + |
| 43 | + case *dns.NS: |
| 44 | + printNSRecord(w, queryType, domainColored, formattedTTL, rec) |
| 45 | + |
| 46 | + case *dns.SOA: |
| 47 | + printSOARecord(w, queryType, domainColored, formattedTTL, rec) |
| 48 | + |
| 49 | + case *dns.PTR: |
| 50 | + printPTRRecord(w, queryType, domainColored, formattedTTL, rec) |
| 51 | + |
| 52 | + default: |
| 53 | + fmt.Fprintf(os.Stderr, "Unknown record type: %s\n", queryType) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + w.Flush() |
| 59 | +} |
| 60 | + |
| 61 | +func printARecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.A) { |
| 62 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.A.String())) |
| 63 | +} |
| 64 | + |
| 65 | +func printAAAARecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.AAAA) { |
| 66 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.AAAA.String())) |
| 67 | +} |
| 68 | + |
| 69 | +func printCNAMERecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.CNAME) { |
| 70 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Target)) |
| 71 | +} |
| 72 | + |
| 73 | +func printMXRecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.MX) { |
| 74 | + preference := color.HiRedString(strconv.FormatUint(uint64(rec.Preference), 10)) |
| 75 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s %s\n", color.HiYellowString(queryType), domain, ttl, preference, color.HiWhiteString(rec.Mx)) |
| 76 | +} |
| 77 | + |
| 78 | +func printTXTRecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.TXT) { |
| 79 | + txtJoined := strings.Join(rec.Txt, " ") |
| 80 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(txtJoined)) |
| 81 | +} |
| 82 | + |
| 83 | +func printNSRecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.NS) { |
| 84 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Ns)) |
| 85 | +} |
| 86 | + |
| 87 | +func printSOARecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.SOA) { |
| 88 | + primaryNameServer := color.HiRedString(rec.Ns) |
| 89 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s %s\n", color.HiYellowString(queryType), domain, ttl, primaryNameServer, rec.Mbox) |
| 90 | +} |
| 91 | + |
| 92 | +func printPTRRecord(w *ansiterm.TabWriter, queryType, domain, ttl string, rec *dns.PTR) { |
| 93 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Ptr)) |
| 94 | +} |
| 95 | + |
| 96 | +// formatTTL converts TTL to a more readable format (hours, minutes, seconds). |
| 97 | +func formatTTL(ttl uint32) string { |
| 98 | + duration := time.Duration(ttl) * time.Second |
| 99 | + hours := int(duration.Hours()) |
| 100 | + minutes := int(duration.Minutes()) % 60 |
| 101 | + seconds := int(duration.Seconds()) % 60 |
| 102 | + |
| 103 | + if hours > 0 { |
| 104 | + return fmt.Sprintf("%02dh%02dm%02ds", hours, minutes, seconds) |
| 105 | + } else if minutes > 0 { |
| 106 | + return fmt.Sprintf("%02dm%02ds", minutes, seconds) |
| 107 | + } |
| 108 | + return fmt.Sprintf("%02ds", seconds) |
| 109 | +} |
| 110 | + |
0 commit comments