mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-27 05:31:54 +08:00
feat(authentication): improve IP logging by extracting real client IP from X-Forwarded-For header
- Added getRealRemoteIP function to retrieve the real client IP address when behind a reverse proxy. - Updated authentication logging to use the extracted IP instead of r.RemoteAddr. - Ensured compatibility for both proxy and non-proxy setups, falling back to r.RemoteAddr when X-Forwarded-For is not present.
This commit is contained in:
committed by
Henrique Dias
parent
189af88bc8
commit
a056e1ba18
+14
-2
@@ -84,9 +84,12 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
if len(h.users) > 0 {
|
if len(h.users) > 0 {
|
||||||
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
|
||||||
|
|
||||||
|
// Retrieve the real client IP address using the updated helper function
|
||||||
|
remoteAddr := getRealRemoteIP(r)
|
||||||
|
|
||||||
// Gets the correct user for this request.
|
// Gets the correct user for this request.
|
||||||
username, password, ok := r.BasicAuth()
|
username, password, ok := r.BasicAuth()
|
||||||
zap.L().Info("login attempt", zap.String("username", username), zap.String("remote_address", r.RemoteAddr))
|
zap.L().Info("login attempt", zap.String("username", username), zap.String("remote_address", remoteAddr))
|
||||||
if !ok {
|
if !ok {
|
||||||
http.Error(w, "Not authorized", http.StatusUnauthorized)
|
http.Error(w, "Not authorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
@@ -99,7 +102,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !h.noPassword && !user.checkPassword(password) {
|
if !h.noPassword && !user.checkPassword(password) {
|
||||||
zap.L().Info("invalid password", zap.String("username", username), zap.String("remote_address", r.RemoteAddr))
|
zap.L().Info("invalid password", zap.String("username", username), zap.String("remote_address", remoteAddr))
|
||||||
http.Error(w, "Not authorized", http.StatusUnauthorized)
|
http.Error(w, "Not authorized", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -159,6 +162,15 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
user.ServeHTTP(w, r)
|
user.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getRealRemoteIP retrieves the client's actual IP address, considering reverse proxies.
|
||||||
|
func getRealRemoteIP(r *http.Request) string {
|
||||||
|
ip := r.Header.Get("X-Forwarded-For")
|
||||||
|
if ip == "" {
|
||||||
|
ip = r.RemoteAddr
|
||||||
|
}
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
|
||||||
type responseWriterNoBody struct {
|
type responseWriterNoBody struct {
|
||||||
http.ResponseWriter
|
http.ResponseWriter
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user