|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 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" |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/fatih/color" |
| 11 | + "github.com/juju/ansiterm" |
| 12 | + "github.com/miekg/dns" |
13 | 13 | )
|
14 | 14 |
|
15 | 15 | // printRecords prints the DNS records to the terminal.
|
16 | 16 | func printRecords(domainName string, messages []*dns.Msg) {
|
17 |
| - w := ansiterm.NewTabWriter(os.Stdout, 8, 8, 4, ' ', 0) |
18 |
| - w.SetColorCapable(true) |
| 17 | + w := ansiterm.NewTabWriter(os.Stdout, 8, 8, 4, ' ', 0) |
| 18 | + w.SetColorCapable(true) |
19 | 19 |
|
20 |
| - domainColored := color.HiBlueString(domainName) |
| 20 | + domainColored := color.HiBlueString(domainName) |
21 | 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)) |
| 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 | 26 |
|
27 |
| - switch rec := answer.(type) { |
28 |
| - case *dns.A: |
29 |
| - printARecord(w, queryType, domainColored, formattedTTL, rec) |
| 27 | + switch rec := answer.(type) { |
| 28 | + case *dns.A: |
| 29 | + printARecord(w, queryType, domainColored, formattedTTL, rec) |
30 | 30 |
|
31 |
| - case *dns.AAAA: |
32 |
| - printAAAARecord(w, queryType, domainColored, formattedTTL, rec) |
| 31 | + case *dns.AAAA: |
| 32 | + printAAAARecord(w, queryType, domainColored, formattedTTL, rec) |
33 | 33 |
|
34 |
| - case *dns.CNAME: |
35 |
| - printCNAMERecord(w, queryType, domainColored, formattedTTL, rec) |
| 34 | + case *dns.CNAME: |
| 35 | + printCNAMERecord(w, queryType, domainColored, formattedTTL, rec) |
36 | 36 |
|
37 |
| - case *dns.MX: |
38 |
| - printMXRecord(w, queryType, domainColored, formattedTTL, rec) |
| 37 | + case *dns.MX: |
| 38 | + printMXRecord(w, queryType, domainColored, formattedTTL, rec) |
39 | 39 |
|
40 |
| - case *dns.TXT: |
41 |
| - printTXTRecord(w, queryType, domainColored, formattedTTL, rec) |
| 40 | + case *dns.TXT: |
| 41 | + printTXTRecord(w, queryType, domainColored, formattedTTL, rec) |
42 | 42 |
|
43 |
| - case *dns.NS: |
44 |
| - printNSRecord(w, queryType, domainColored, formattedTTL, rec) |
| 43 | + case *dns.NS: |
| 44 | + printNSRecord(w, queryType, domainColored, formattedTTL, rec) |
45 | 45 |
|
46 |
| - case *dns.SOA: |
47 |
| - printSOARecord(w, queryType, domainColored, formattedTTL, rec) |
| 46 | + case *dns.SOA: |
| 47 | + printSOARecord(w, queryType, domainColored, formattedTTL, rec) |
48 | 48 |
|
49 |
| - case *dns.PTR: |
50 |
| - printPTRRecord(w, queryType, domainColored, formattedTTL, rec) |
| 49 | + case *dns.PTR: |
| 50 | + printPTRRecord(w, queryType, domainColored, formattedTTL, rec) |
51 | 51 |
|
52 |
| - default: |
53 |
| - fmt.Fprintf(os.Stderr, "Unknown record type: %s\n", queryType) |
54 |
| - } |
55 |
| - } |
56 |
| - } |
| 52 | + default: |
| 53 | + fmt.Fprintf(os.Stderr, "Unknown record type: %s\n", queryType) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
57 | 57 |
|
58 |
| - w.Flush() |
| 58 | + w.Flush() |
59 | 59 | }
|
60 | 60 |
|
61 | 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())) |
| 62 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.A.String())) |
63 | 63 | }
|
64 | 64 |
|
65 | 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())) |
| 66 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.AAAA.String())) |
67 | 67 | }
|
68 | 68 |
|
69 | 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)) |
| 70 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Target)) |
71 | 71 | }
|
72 | 72 |
|
73 | 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)) |
| 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 | 76 | }
|
77 | 77 |
|
78 | 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)) |
| 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 | 81 | }
|
82 | 82 |
|
83 | 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)) |
| 84 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Ns)) |
85 | 85 | }
|
86 | 86 |
|
87 | 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) |
| 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 | 90 | }
|
91 | 91 |
|
92 | 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)) |
| 93 | + fmt.Fprintf(w, "%s\t%s.\t%s\t%s\n", color.HiYellowString(queryType), domain, ttl, color.HiWhiteString(rec.Ptr)) |
94 | 94 | }
|
95 | 95 |
|
96 | 96 | // formatTTL converts TTL to a more readable format (hours, minutes, seconds).
|
97 | 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) |
| 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 | 109 | }
|
110 |
| - |
0 commit comments