add whoami
This commit is contained in:
parent
be529a50d1
commit
4201a704f8
32
whoami.go
Normal file
32
whoami.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user