41 lines
951 B
Go
41 lines
951 B
Go
package main
|
|
|
|
import (
|
|
"net/http"
|
|
"net"
|
|
"strings"
|
|
"encoding/base32"
|
|
"os"
|
|
)
|
|
|
|
// 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)
|
|
listenPort := "3000"
|
|
// Get the first command-line argument
|
|
if len(os.Args) > 1 {
|
|
listenPort = os.Args[1]
|
|
}
|
|
print("Listening on port:", listenPort, "\n")
|
|
listenPort = strings.Join([]string{":", listenPort}, "")
|
|
http.ListenAndServe(listenPort, nil)
|
|
}
|
|
|