From 5a6136c5e7947b44374d92ebca915e17beb27928 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 8 Jul 2023 15:23:16 +0200 Subject: [PATCH] chore: add getIPs function --- app.go | 94 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 49 deletions(-) diff --git a/app.go b/app.go index 0316dfa..a3f966f 100644 --- a/app.go +++ b/app.go @@ -29,6 +29,14 @@ const ( TB ) +var upgrader = websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { + return true + }, +} + var ( cert string key string @@ -43,16 +51,18 @@ func init() { flag.StringVar(&cert, "cert", "", "give me a certificate") flag.StringVar(&key, "key", "", "give me a key") flag.StringVar(&ca, "cacert", "", "give me a CA chain, enforces mutual TLS") - flag.StringVar(&port, "port", getEnv("WHOAMI_PORT_NUMBER", "80"), "give me a port number") + flag.StringVar(&port, "port", getEnv("WHOAMI_PORT_NUMBER", "8080"), "give me a port number") flag.StringVar(&name, "name", os.Getenv("WHOAMI_NAME"), "give me a name") } -var upgrader = websocket.Upgrader{ - ReadBufferSize: 1024, - WriteBufferSize: 1024, - CheckOrigin: func(r *http.Request) bool { - return true - }, +type Data struct { + Hostname string `json:"hostname,omitempty"` + IP []string `json:"ip,omitempty"` + Headers http.Header `json:"headers,omitempty"` + URL string `json:"url,omitempty"` + Host string `json:"host,omitempty"` + Method string `json:"method,omitempty"` + Name string `json:"name,omitempty"` } func main() { @@ -213,20 +223,8 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) { hostname, _ := os.Hostname() _, _ = fmt.Fprintln(w, "Hostname:", hostname) - ifaces, _ := net.Interfaces() - for _, i := range ifaces { - addrs, _ := i.Addrs() - // handle err - for _, addr := range addrs { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP - } - _, _ = fmt.Fprintln(w, "IP:", ip) - } + for _, ip := range getIPs() { + _, _ = fmt.Fprintln(w, "IP:", ip) } _, _ = fmt.Fprintln(w, "RemoteAddr:", req.RemoteAddr) @@ -239,17 +237,9 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) { func apiHandler(w http.ResponseWriter, req *http.Request) { hostname, _ := os.Hostname() - data := struct { - Hostname string `json:"hostname,omitempty"` - IP []string `json:"ip,omitempty"` - Headers http.Header `json:"headers,omitempty"` - URL string `json:"url,omitempty"` - Host string `json:"host,omitempty"` - Method string `json:"method,omitempty"` - Name string `json:"name,omitempty"` - }{ + data := Data{ Hostname: hostname, - IP: []string{}, + IP: getIPs(), Headers: req.Header, URL: req.URL.RequestURI(), Host: req.Host, @@ -257,24 +247,6 @@ func apiHandler(w http.ResponseWriter, req *http.Request) { Name: name, } - ifaces, _ := net.Interfaces() - for _, i := range ifaces { - addrs, _ := i.Addrs() - // handle err - for _, addr := range addrs { - var ip net.IP - switch v := addr.(type) { - case *net.IPNet: - ip = v.IP - case *net.IPAddr: - ip = v.IP - } - if ip != nil { - data.IP = append(data.IP, ip.String()) - } - } - } - w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(data); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) @@ -319,3 +291,27 @@ func getEnv(key, fallback string) string { } return value } + +func getIPs() []string { + var ips []string + + ifaces, _ := net.Interfaces() + for _, i := range ifaces { + addrs, _ := i.Addrs() + // handle err + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + } + if ip != nil { + ips = append(ips, ip.String()) + } + } + } + + return ips +}