Brijesh's Git Server — dns-record-checker @ a458291afd4658066119df4c36443ac3a63b54ee

DNS record checker - dns.brijesh.dev

main.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 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,
	})
}