summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2017-07-18 03:36:57 +0000
committerMike Crute <mike@crute.us>2017-07-18 03:36:57 +0000
commit59ca7934d3ede5b40a31c98424ba9e3c7ed9f171 (patch)
tree8266205b06b3fae37516083baaffe994a7da1d73
downloadgo_ddns_manager-59ca7934d3ede5b40a31c98424ba9e3c7ed9f171.tar.bz2
go_ddns_manager-59ca7934d3ede5b40a31c98424ba9e3c7ed9f171.tar.xz
go_ddns_manager-59ca7934d3ede5b40a31c98424ba9e3c7ed9f171.zip
Initial import
-rw-r--r--dns.html85
-rw-r--r--main.go118
2 files changed, 203 insertions, 0 deletions
diff --git a/dns.html b/dns.html
new file mode 100644
index 0000000..8a44f6d
--- /dev/null
+++ b/dns.html
@@ -0,0 +1,85 @@
1<html>
2 <head>
3 <title>Test Page</title>
4 <style type="text/css">
5 html {
6 text-align: center;
7 }
8 table {
9 border: 1px solid #999;
10 border-collapse: collapse;
11 margin: auto;
12 }
13 td, th {
14 padding: 0.5em;
15 border: 1px solid #999;
16 }
17 th {
18 background: #ccc;
19 }
20 .dns-type {
21 text-align: center;
22 }
23 </style>
24 </head>
25 <body>
26 <h1>DNS Manager</h1>
27 <b>Zone:</b> <select>
28 <option>crute.me</option>
29 <option>crute.us</option>
30 <option>crute.org</option>
31 <option>faldowski.com</option>
32 <option>softgroupcorp.com</option>
33 <option>16.172.in-addr.arpa</option>
34 <option>31.172.in-addr.arpa</option>
35 </select>
36 <b>View:</b> <select>
37 <option>Internal</option>
38 <option>External</option>
39 </select>
40 <b>Records:</b> <select>
41 <option>ANY</option>
42 <option>A</option>
43 <option>SOA</option>
44 <option>CNAME</option>
45 <option>AAAA</option>
46 <option>MX</option>
47 <option>TXT</option>
48 <option>PTR</option>
49 <option>NS</option>
50 <option>SRV</option>
51 <option>SPF</option>
52 </select>
53 <br />
54 <b>Search:</b> <input type="text" />
55 <input type="submit" value="Search" />
56 <br />
57 <input type="submit" value="Add Record" />
58
59 <br /><br />
60 <table>
61 <tr>
62 <th>Name</th>
63 <th>RR Type</th>
64 <th>TTL</th>
65 <th>RR Data</th>
66 <th>Action</th>
67 </tr>
68 {{ range $r := . }}
69 {{ range $_, $rr := $r.RR }}
70 <tr>
71 {{ $h := $rr.Header }}
72 <td>{{ $h.Name }}</td>
73 <td class="dns-type">{{ dnsType $h }}</td>
74 <td title="{{ $h.Ttl}}">{{ dnsTTL $h }}</td>
75 <td>{{ getValue $rr }}</td>
76 <td>
77 <a href="#">Delete</a> |
78 <a href="#">Update</a>
79 </td>
80 </tr>
81 {{ end }}
82 {{ end }}
83 </table>
84 </body>
85</html>
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..777a2d7
--- /dev/null
+++ b/main.go
@@ -0,0 +1,118 @@
1package main
2
3import (
4 "bytes"
5 "fmt"
6 "github.com/miekg/dns"
7 "net/http"
8 "text/template"
9 "time"
10)
11
12type Zone struct {
13 Name string
14 View string
15}
16
17var 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
30func 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
65func 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
82func dnsClass(rrh *dns.RR_Header) string {
83 return dns.Class(rrh.Class).String()
84}
85
86func dnsType(rrh *dns.RR_Header) string {
87 return dns.Type(rrh.Rrtype).String()
88}
89
90func 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
104func 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
115func main() {
116 http.HandleFunc("/", handler)
117 http.ListenAndServe(":8080", nil)
118}