feat(): trace real IP

This commit is contained in:
Nicolas Carlier 2022-07-09 07:54:22 +02:00
parent ab4dc1eedd
commit 50bcb16c71

View File

@ -25,13 +25,9 @@ func Logger(next http.Handler) http.Handler {
if !ok {
requestID = "0"
}
addr := r.RemoteAddr
if i := strings.LastIndex(addr, ":"); i != -1 {
addr = addr[:i]
}
logger.Info.Printf(
"%s - - [%s] %q %d %d %q %q %q",
addr,
getRequestIP(r),
start.Format("02/Jan/2006:15:04:05 -0700"),
fmt.Sprintf("%s %s %s", r.Method, r.URL, r.Proto),
o.status,
@ -45,6 +41,21 @@ func Logger(next http.Handler) http.Handler {
})
}
func getRequestIP(r *http.Request) string {
ip := r.Header.Get("X-Forwarded-For")
if ip == "" {
ip = r.RemoteAddr
}
if comma := strings.Index(ip, ","); comma != -1 {
ip = ip[0:comma]
}
if colon := strings.LastIndex(ip, ":"); colon != -1 {
ip = ip[:colon]
}
return ip
}
type responseObserver struct {
http.ResponseWriter
status int