diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 118 |
1 files changed, 118 insertions, 0 deletions
@@ -0,0 +1,118 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "bytes" | ||
5 | "fmt" | ||
6 | "github.com/miekg/dns" | ||
7 | "net/http" | ||
8 | "text/template" | ||
9 | "time" | ||
10 | ) | ||
11 | |||
12 | type Zone struct { | ||
13 | Name string | ||
14 | View string | ||
15 | } | ||
16 | |||
17 | var SECRETS = map[string]string{ | ||
18 | "crute-me-internal.": "", | ||
19 | "crute-me-external.": "", | ||
20 | "crute-us-internal.": "", | ||
21 | "crute-us-external.": "", | ||
22 | "crute-org-internal.": "", | ||
23 | "crute-org-external.": "", | ||
24 | "faldowski-com-internal.": "", | ||
25 | "faldowski-com-external.": "", | ||
26 | "softgroupcorp-com-internal.": "", | ||
27 | "softgroupcorp-com-external.": "", | ||
28 | } | ||
29 | |||
30 | func getValue(v interface{}) string { | ||
31 | switch i := v.(type) { | ||
32 | case *dns.SOA: | ||
33 | return fmt.Sprintf("%s %s %d %d %d %d %d", i.Ns, i.Mbox, i.Serial, i.Refresh, i.Retry, i.Expire, i.Minttl) | ||
34 | case *dns.A: | ||
35 | return fmt.Sprintf("%s", i.A) | ||
36 | case *dns.CNAME: | ||
37 | return fmt.Sprintf("%s", i.Target) | ||
38 | case *dns.AAAA: | ||
39 | return fmt.Sprintf("%s", i.AAAA) | ||
40 | case *dns.MX: | ||
41 | return fmt.Sprintf("%d %s", i.Preference, i.Mx) | ||
42 | case *dns.TXT: | ||
43 | b := &bytes.Buffer{} | ||
44 | for _, t := range i.Txt { | ||
45 | fmt.Fprintf(b, "\"%s\"", t) // [] | ||
46 | } | ||
47 | return b.String() | ||
48 | case *dns.PTR: | ||
49 | return fmt.Sprintf("%s", i.Ptr) | ||
50 | case *dns.NS: | ||
51 | return fmt.Sprintf("%s", i.Ns) | ||
52 | case *dns.SRV: | ||
53 | return fmt.Sprintf("%d %d %d %s", i.Priority, i.Weight, i.Port, i.Target) | ||
54 | case *dns.SPF: | ||
55 | b := &bytes.Buffer{} | ||
56 | for _, t := range i.Txt { | ||
57 | fmt.Fprintf(b, "\"%s\"", t) // [] | ||
58 | } | ||
59 | return b.String() | ||
60 | default: | ||
61 | return "UNKNOWN" | ||
62 | } | ||
63 | } | ||
64 | |||
65 | func getDns() chan *dns.Envelope { | ||
66 | c := &dns.Transfer{} | ||
67 | c.TsigSecret = SECRETS | ||
68 | |||
69 | m := &dns.Msg{} | ||
70 | m.SetAxfr("crute.me.") | ||
71 | m.SetTsig("crute-me-internal.", dns.HmacSHA256, 300, time.Now().Unix()) | ||
72 | |||
73 | in, err := c.In(m, "172.31.46.225:53") | ||
74 | if err != nil { | ||
75 | fmt.Printf("Error: %s\n", err.Error()) | ||
76 | return nil | ||
77 | } | ||
78 | |||
79 | return in | ||
80 | } | ||
81 | |||
82 | func dnsClass(rrh *dns.RR_Header) string { | ||
83 | return dns.Class(rrh.Class).String() | ||
84 | } | ||
85 | |||
86 | func dnsType(rrh *dns.RR_Header) string { | ||
87 | return dns.Type(rrh.Rrtype).String() | ||
88 | } | ||
89 | |||
90 | func dnsTTL(rrh *dns.RR_Header) string { | ||
91 | t := rrh.Ttl | ||
92 | |||
93 | if t/86400 > 1 { | ||
94 | return fmt.Sprintf("%d days", t/86400) | ||
95 | } else if t/3600 > 1 { | ||
96 | return fmt.Sprintf("%d hours", t/3600) | ||
97 | } else if t/60 > 1 { | ||
98 | return fmt.Sprintf("%d minutes", t/60) | ||
99 | } else { | ||
100 | return fmt.Sprintf("%d seconds", t) | ||
101 | } | ||
102 | } | ||
103 | |||
104 | func handler(w http.ResponseWriter, r *http.Request) { | ||
105 | fm := template.FuncMap{ | ||
106 | "getValue": getValue, | ||
107 | "dnsClass": dnsClass, | ||
108 | "dnsType": dnsType, | ||
109 | "dnsTTL": dnsTTL, | ||
110 | } | ||
111 | t, _ := template.New("").Funcs(fm).ParseFiles("dns.html") | ||
112 | t.ExecuteTemplate(w, "dns.html", getDns()) | ||
113 | } | ||
114 | |||
115 | func main() { | ||
116 | http.HandleFunc("/", handler) | ||
117 | http.ListenAndServe(":8080", nil) | ||
118 | } | ||