From 59ca7934d3ede5b40a31c98424ba9e3c7ed9f171 Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Tue, 18 Jul 2017 03:36:57 +0000 Subject: Initial import --- dns.html | 85 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 dns.html create mode 100644 main.go diff --git a/dns.html b/dns.html new file mode 100644 index 0000000..8a44f6d --- /dev/null +++ b/dns.html @@ -0,0 +1,85 @@ + + + Test Page + + + +

DNS Manager

+ Zone: + View: + Records: +
+ Search: + +
+ + +

+ + + + + + + + + {{ range $r := . }} + {{ range $_, $rr := $r.RR }} + + {{ $h := $rr.Header }} + + + + + + + {{ end }} + {{ end }} +
NameRR TypeTTLRR DataAction
{{ $h.Name }}{{ dnsType $h }}{{ dnsTTL $h }}{{ getValue $rr }} + Delete | + Update +
+ + diff --git a/main.go b/main.go new file mode 100644 index 0000000..777a2d7 --- /dev/null +++ b/main.go @@ -0,0 +1,118 @@ +package main + +import ( + "bytes" + "fmt" + "github.com/miekg/dns" + "net/http" + "text/template" + "time" +) + +type Zone struct { + Name string + View string +} + +var SECRETS = map[string]string{ + "crute-me-internal.": "", + "crute-me-external.": "", + "crute-us-internal.": "", + "crute-us-external.": "", + "crute-org-internal.": "", + "crute-org-external.": "", + "faldowski-com-internal.": "", + "faldowski-com-external.": "", + "softgroupcorp-com-internal.": "", + "softgroupcorp-com-external.": "", +} + +func getValue(v interface{}) string { + switch i := v.(type) { + case *dns.SOA: + return fmt.Sprintf("%s %s %d %d %d %d %d", i.Ns, i.Mbox, i.Serial, i.Refresh, i.Retry, i.Expire, i.Minttl) + case *dns.A: + return fmt.Sprintf("%s", i.A) + case *dns.CNAME: + return fmt.Sprintf("%s", i.Target) + case *dns.AAAA: + return fmt.Sprintf("%s", i.AAAA) + case *dns.MX: + return fmt.Sprintf("%d %s", i.Preference, i.Mx) + case *dns.TXT: + b := &bytes.Buffer{} + for _, t := range i.Txt { + fmt.Fprintf(b, "\"%s\"", t) // [] + } + return b.String() + case *dns.PTR: + return fmt.Sprintf("%s", i.Ptr) + case *dns.NS: + return fmt.Sprintf("%s", i.Ns) + case *dns.SRV: + return fmt.Sprintf("%d %d %d %s", i.Priority, i.Weight, i.Port, i.Target) + case *dns.SPF: + b := &bytes.Buffer{} + for _, t := range i.Txt { + fmt.Fprintf(b, "\"%s\"", t) // [] + } + return b.String() + default: + return "UNKNOWN" + } +} + +func getDns() chan *dns.Envelope { + c := &dns.Transfer{} + c.TsigSecret = SECRETS + + m := &dns.Msg{} + m.SetAxfr("crute.me.") + m.SetTsig("crute-me-internal.", dns.HmacSHA256, 300, time.Now().Unix()) + + in, err := c.In(m, "172.31.46.225:53") + if err != nil { + fmt.Printf("Error: %s\n", err.Error()) + return nil + } + + return in +} + +func dnsClass(rrh *dns.RR_Header) string { + return dns.Class(rrh.Class).String() +} + +func dnsType(rrh *dns.RR_Header) string { + return dns.Type(rrh.Rrtype).String() +} + +func dnsTTL(rrh *dns.RR_Header) string { + t := rrh.Ttl + + if t/86400 > 1 { + return fmt.Sprintf("%d days", t/86400) + } else if t/3600 > 1 { + return fmt.Sprintf("%d hours", t/3600) + } else if t/60 > 1 { + return fmt.Sprintf("%d minutes", t/60) + } else { + return fmt.Sprintf("%d seconds", t) + } +} + +func handler(w http.ResponseWriter, r *http.Request) { + fm := template.FuncMap{ + "getValue": getValue, + "dnsClass": dnsClass, + "dnsType": dnsType, + "dnsTTL": dnsTTL, + } + t, _ := template.New("").Funcs(fm).ParseFiles("dns.html") + t.ExecuteTemplate(w, "dns.html", getDns()) +} + +func main() { + http.HandleFunc("/", handler) + http.ListenAndServe(":8080", nil) +} -- cgit v1.2.3