diff --git a/whoami.go b/whoami.go new file mode 100644 index 0000000..b3a00ad --- /dev/null +++ b/whoami.go @@ -0,0 +1,32 @@ +package main + +import ( + "net/http" + "net" + "strings" + "encoding/base32" +) + +// DomainFromIP derives a meshname subdomain for the authoritative DNS server address +func DomainFromIP(target net.IP) string { + host := strings.ToLower(base32.StdEncoding.EncodeToString(target)[0:26]) + tld := ".mesh.cat" + domain := strings.Join([]string{host, tld}, "") + return domain +} + +func handler(w http.ResponseWriter, r *http.Request) { + ip, port, err := net.SplitHostPort(r.RemoteAddr) + _, _ = port, err + parsedIP := net.ParseIP(ip) + // return domain for IPv6 only + if parsedIP.To4() == nil { + w.Write([]byte(DomainFromIP(parsedIP))) + } +} + +func main() { + http.HandleFunc("/", handler) + http.ListenAndServe(":3000", nil) +} +