create DNS checker of my own
Brijesh ops@brijesh.dev
Thu, 25 Jul 2024 23:50:50 +0530
3 files changed,
180 insertions(+),
0 deletions(-)
A
main.go
@@ -0,0 +1,70 @@
+package main + +import ( + "html/template" + "net" + "net/http" +) + +type DNSRecord struct { + Domain string `json:"domain"` + ARecord []net.IP `json:"a_record"` + CNAME string `json:"cname_record"` + MXRecord []string `json:"mx_record"` + NSRecord []string `json:"ns_record"` + TXTRecord []string `json:"txt_record"` +} + +var templates *template.Template + +func main() { + templates = template.Must(template.ParseGlob("templates/*.html")) + http.HandleFunc("/", indexHandler) + http.HandleFunc("/lookup", dnsLookupHandler) + http.ListenAndServe(":8080", nil) +} + +func indexHandler(w http.ResponseWriter, r *http.Request) { + templates.ExecuteTemplate(w, "index.html", nil) +} + +func dnsLookupHandler(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } else if r.Method != http.MethodPost { + http.Error(w, "invalid request method", http.StatusMethodNotAllowed) + return + } + + domain := r.FormValue("domain") + + RecordTypeA, _ := net.LookupIP(domain) + RecordTypeCNAME, _ := net.LookupCNAME(domain) + RecordTypeMX, _ := net.LookupMX(domain) + RecordTypeNS, _ := net.LookupNS(domain) + RecordTypeTXT, _ := net.LookupTXT(domain) + + NSHosts := []string{} + for _, ns := range RecordTypeNS { + NSHosts = append(NSHosts, ns.Host) + } + + MXHosts := []string{} + for _, mx := range RecordTypeMX { + MXHosts = append(MXHosts, mx.Host) + } + + dnsRecord := &DNSRecord{ + Domain: domain, + ARecord: RecordTypeA, + CNAME: RecordTypeCNAME, + MXRecord: MXHosts, + NSRecord: NSHosts, + TXTRecord: RecordTypeTXT, + } + + templates.ExecuteTemplate(w, "index.html", map[string]any{ + "DNSRecord": dnsRecord, + }) +}
A
templates/index.html
@@ -0,0 +1,107 @@
+<!doctype html> +<html lang="en"> + <head> + <title>DNS Lookup</title> + <script src="https://cdn.tailwindcss.com"></script> + </head> + <body class="m-4 lg:m-6"> + <h1 class="text-xl mb-3">DNS Lookup</h1> + <form action="/lookup" method="post" class="flex"> + <input + class="px-2 py-1 rounded border border-zinc-200 focus:border-zinc-300 w-full lg:w-72" + placeholder="Domain" + id="domain" + name="domain" + required + /> + <button + class="px-2 py-1 rounded bg-zinc-700 text-white hover:bg-zinc-900 focus:outline-none ml-2" + type="submit" + > + Lookup + </button> + </form> + + <div id="query-message" class="mt-4 text-red-700 hidden"> + Query submitted, please wait for a few seconds. + </div> + + <div id="results"> + {{ with .DNSRecord }} + <h2 class="mt-4 mb-2">Records for {{ .Domain }}</h2> + + <details open class="mb-4"> + <summary>A record</summary> + {{ if eq (len .ARecord) 0 }} + <p class="text-zinc-400">Not available</p> + {{ else }} + <ul class="list-none"> + {{ range .ARecord }} + <li>{{ . }}</li> + {{ end }} + </ul> + {{ end }} + </details> + + <details open class="mb-4"> + <summary>CNAME record</summary> + {{ if eq .CNAME "" }} + <p class="text-zinc-400">Not available</p> + {{ else }} + <p>{{ .CNAME }}</p> + {{ end }} + </details> + + <details open class="mb-4"> + <summary>MX record</summary> + {{ if eq (len .MXRecord) 0 }} + <p class="text-zinc-400">Not available</p> + {{ else }} + <ul class="list-none"> + {{ range .MXRecord }} + <li>{{ . }}</li> + {{ end }} + </ul> + {{ end }} + </details> + + <details open class="mb-4"> + <summary>NS record</summary> + {{ if eq (len .NSRecord) 0 }} + <p class="text-zinc-400">Not available</p> + {{ else }} + <ul class="list-none"> + {{ range .NSRecord }} + <li>{{ . }}</li> + {{ end }} + </ul> + {{ end }} + </details> + + <details open class="mb-4"> + <summary>TXT record</summary> + {{ if eq (len .TXTRecord) 0 }} + <p class="text-zinc-400">Not available</p> + {{ else }} + <ul class="list-none"> + {{ range .TXTRecord }} + <li>{{ . }}</li> + {{ end }} + </ul> + {{ end }} + </details> + {{ end }} + </div> + + <script> + const form = document.querySelector("form"); + const message = document.getElementById("query-message"); + const results = document.getElementById("results"); + + form.addEventListener("submit", () => { + message.classList.remove("hidden"); + results.classList.add("hidden"); + }); + </script> + </body> +</html>