chore: add getIPs function
This commit is contained in:
92
app.go
92
app.go
@ -29,6 +29,14 @@ const (
|
|||||||
TB
|
TB
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cert string
|
cert string
|
||||||
key string
|
key string
|
||||||
@ -43,16 +51,18 @@ func init() {
|
|||||||
flag.StringVar(&cert, "cert", "", "give me a certificate")
|
flag.StringVar(&cert, "cert", "", "give me a certificate")
|
||||||
flag.StringVar(&key, "key", "", "give me a key")
|
flag.StringVar(&key, "key", "", "give me a key")
|
||||||
flag.StringVar(&ca, "cacert", "", "give me a CA chain, enforces mutual TLS")
|
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")
|
flag.StringVar(&name, "name", os.Getenv("WHOAMI_NAME"), "give me a name")
|
||||||
}
|
}
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
type Data struct {
|
||||||
ReadBufferSize: 1024,
|
Hostname string `json:"hostname,omitempty"`
|
||||||
WriteBufferSize: 1024,
|
IP []string `json:"ip,omitempty"`
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
Headers http.Header `json:"headers,omitempty"`
|
||||||
return true
|
URL string `json:"url,omitempty"`
|
||||||
},
|
Host string `json:"host,omitempty"`
|
||||||
|
Method string `json:"method,omitempty"`
|
||||||
|
Name string `json:"name,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -213,21 +223,9 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
_, _ = fmt.Fprintln(w, "Hostname:", hostname)
|
_, _ = fmt.Fprintln(w, "Hostname:", hostname)
|
||||||
|
|
||||||
ifaces, _ := net.Interfaces()
|
for _, ip := range getIPs() {
|
||||||
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)
|
_, _ = fmt.Fprintln(w, "IP:", ip)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
_, _ = fmt.Fprintln(w, "RemoteAddr:", req.RemoteAddr)
|
_, _ = fmt.Fprintln(w, "RemoteAddr:", req.RemoteAddr)
|
||||||
if err := req.Write(w); err != nil {
|
if err := req.Write(w); err != nil {
|
||||||
@ -239,17 +237,9 @@ func whoamiHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
func apiHandler(w http.ResponseWriter, req *http.Request) {
|
func apiHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
|
|
||||||
data := struct {
|
data := Data{
|
||||||
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"`
|
|
||||||
}{
|
|
||||||
Hostname: hostname,
|
Hostname: hostname,
|
||||||
IP: []string{},
|
IP: getIPs(),
|
||||||
Headers: req.Header,
|
Headers: req.Header,
|
||||||
URL: req.URL.RequestURI(),
|
URL: req.URL.RequestURI(),
|
||||||
Host: req.Host,
|
Host: req.Host,
|
||||||
@ -257,24 +247,6 @@ func apiHandler(w http.ResponseWriter, req *http.Request) {
|
|||||||
Name: name,
|
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")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
if err := json.NewEncoder(w).Encode(data); err != nil {
|
if err := json.NewEncoder(w).Encode(data); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
@ -319,3 +291,27 @@ func getEnv(key, fallback string) string {
|
|||||||
}
|
}
|
||||||
return value
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user