add command line flags
This commit is contained in:
parent
a1d3313299
commit
4b0f083f72
43
whoami.go
43
whoami.go
@ -3,38 +3,49 @@ package main
|
|||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net"
|
"net"
|
||||||
"strings"
|
|
||||||
"encoding/base32"
|
"encoding/base32"
|
||||||
"os"
|
"strings"
|
||||||
|
"flag"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DomainFromIP derives a meshname subdomain for the authoritative DNS server address
|
|
||||||
func DomainFromIP(target net.IP) string {
|
// define tld
|
||||||
|
var tld = flag.String("tld", "mesh.cat", "The top level domain for our network")
|
||||||
|
|
||||||
|
// 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])
|
host := strings.ToLower(base32.StdEncoding.EncodeToString(target)[0:26])
|
||||||
tld := ".mesh.cat"
|
domain := strings.Join([]string{host, ".", *tld, "\n"}, "")
|
||||||
domain := strings.Join([]string{host, tld}, "")
|
|
||||||
return domain
|
return domain
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle HTTP requests
|
||||||
func handler(w http.ResponseWriter, r *http.Request) {
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
ip, port, err := net.SplitHostPort(r.RemoteAddr)
|
ip, port, err := net.SplitHostPort(r.RemoteAddr)
|
||||||
_, _ = port, err
|
_, _ = port, err
|
||||||
parsedIP := net.ParseIP(ip)
|
parsedIP := net.ParseIP(ip)
|
||||||
// return domain for IPv6 only
|
// return domain for IPv6 only
|
||||||
if parsedIP.To4() == nil {
|
if parsedIP.To4() == nil {
|
||||||
w.Write([]byte(DomainFromIP(parsedIP)))
|
w.Write([]byte(domainFromIP(parsedIP)))
|
||||||
|
} else {
|
||||||
|
// set the response status code to 422
|
||||||
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||||
|
w.Write([]byte("ipv4 not supported\n"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// define path
|
||||||
http.HandleFunc("/", handler)
|
http.HandleFunc("/", handler)
|
||||||
listenPort := "3000"
|
// define ip address
|
||||||
// Get the first command-line argument
|
address := flag.String("address", "[::0]", "The interface address to listen on")
|
||||||
if len(os.Args) > 1 {
|
// define port
|
||||||
listenPort = os.Args[1]
|
port := flag.String("port", "8008", "The port to listen on")
|
||||||
}
|
// parse the command-line arguments
|
||||||
print("Listening on port: ", listenPort, "\n")
|
flag.Parse()
|
||||||
listenPort = strings.Join([]string{":", listenPort}, "")
|
// construct listening address
|
||||||
http.ListenAndServe(listenPort, nil)
|
listenAddr := strings.Join([]string{*address, ":", *port}, "")
|
||||||
|
print("Listening on address: ", listenAddr, "\n")
|
||||||
|
// start server
|
||||||
|
http.ListenAndServe(listenAddr, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user