create WHOIS checker
Brijesh ops@brijesh.dev
Fri, 26 Jul 2024 11:05:31 +0530
4 files changed,
127 insertions(+),
0 deletions(-)
A
go.mod
@@ -0,0 +1,8 @@
+module whois + +go 1.22.4 + +require ( + github.com/likexian/whois v1.15.4 // indirect + golang.org/x/net v0.27.0 // indirect +)
A
go.sum
@@ -0,0 +1,4 @@
+github.com/likexian/whois v1.15.4 h1:r5En62c+S9HKFgJtdh2WsdmRGTcxE4WUtGBdZkSBXmM= +github.com/likexian/whois v1.15.4/go.mod h1:rXFTPcQdNlPQBJCQpPWTSIDGzzmgKBftmhdOOcLpwXk= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
A
main.go
@@ -0,0 +1,66 @@
+// package main +// +// import ( +// "fmt" +// +// "github.com/likexian/whois" +// ) +// +// func main() { +// result, _ := whois.Whois("brijesh.dev") +// +// fmt.Println("Result: ", result) +// } + +package main + +import ( + "html/template" + "net/http" + + "github.com/likexian/whois" +) + +type WHOISRecord struct { + Domain string `json:"domain"` + Result string `json:"result"` +} + +var templates *template.Template + +func main() { + templates = template.Must(template.ParseGlob("templates/*.html")) + http.HandleFunc("/", indexHandler) + http.HandleFunc("/lookup", whoisLookupHandler) + http.ListenAndServe(":8000", nil) +} + +func indexHandler(w http.ResponseWriter, r *http.Request) { + templates.ExecuteTemplate(w, "index.html", nil) +} + +func whoisLookupHandler(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") + result, err := whois.Whois(domain) + if err != nil { + http.Error(w, "Error fetching WHOIS data", http.StatusInternalServerError) + return + } + + whoisRecord := &WHOISRecord{ + Domain: domain, + Result: result, + } + + templates.ExecuteTemplate(w, "index.html", map[string]any{ + "WHOISRecord": whoisRecord, + }) +}
A
templates/index.html
@@ -0,0 +1,49 @@
+<!doctype html> +<html lang="en"> + <head> + <title>WHOIS Lookup</title> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <script src="https://cdn.tailwindcss.com"></script> + </head> + <body class="m-4 lg:m-6"> + <h1 class="text-xl mb-3">WHOIS 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 .WHOISRecord }} + <h2 class="mt-4 mb-2">WHOIS Record for {{ .Domain }}</h2> + <pre>{{ .Result }}</pre> + {{ 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>