device enrollment: fix ip address (#3430)

This commit is contained in:
Caleb Doxsey 2022-06-16 11:30:38 -06:00 committed by GitHub
parent d1037d784a
commit a938a23ea2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 5 deletions

View file

@ -1,6 +1,9 @@
package httputil
import "net/http"
import (
"net"
"net/http"
)
const (
// StatusDeviceUnauthorized is the status code returned when a client's
@ -39,3 +42,16 @@ func StatusText(code int) string {
}
return http.StatusText(code)
}
// GetClientIPAddress gets a client's IP address for an HTTP request.
func GetClientIPAddress(r *http.Request) string {
if ip := r.Header.Get("X-Envoy-External-Address"); ip != "" {
return ip
}
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
return ip
}
return "127.0.0.1"
}