33 lines
723 B
Go
33 lines
723 B
Go
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)
|
|
}
|
|
|