1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

Store client IP address in request context

This commit is contained in:
Frédéric Guillot 2018-09-09 15:15:14 -07:00
parent c1e1506720
commit c9f9dd3262
9 changed files with 46 additions and 19 deletions

View file

@ -18,8 +18,7 @@ func (m *Middleware) BasicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
remoteAddr := request.RealIP(r)
clientIP := request.ClientIP(r)
username, password, authOK := r.BasicAuth()
if !authOK {
logger.Debug("[Middleware:BasicAuth] No authentication headers sent")
@ -28,7 +27,7 @@ func (m *Middleware) BasicAuth(next http.Handler) http.Handler {
}
if err := m.store.CheckPassword(username, password); err != nil {
logger.Error("[Middleware:BasicAuth] [Remote=%v] Invalid username or password: %s", remoteAddr, username)
logger.Error("[Middleware:BasicAuth] [ClientIP=%s] Invalid username or password: %s", clientIP, username)
json.Unauthorized(w)
return
}
@ -41,7 +40,7 @@ func (m *Middleware) BasicAuth(next http.Handler) http.Handler {
}
if user == nil {
logger.Error("[Middleware:BasicAuth] [Remote=%v] User not found: %s", remoteAddr, username)
logger.Error("[Middleware:BasicAuth] [ClientIP=%s] User not found: %s", clientIP, username)
json.Unauthorized(w)
return
}

21
middleware/client_ip.go Normal file
View file

@ -0,0 +1,21 @@
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package middleware // import "miniflux.app/middleware"
import (
"context"
"net/http"
"miniflux.app/http/request"
)
// ClientIP stores in the real client IP address in the context.
func (m *Middleware) ClientIP(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(ctx, request.ClientIPContextKey, request.FindClientIP(r))
next.ServeHTTP(w, r.WithContext(ctx))
})
}

View file

@ -14,7 +14,7 @@ import (
// Logging logs the HTTP request.
func (m *Middleware) Logging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger.Debug("[HTTP] %s %s %s", request.RealIP(r), r.Method, r.RequestURI)
logger.Debug("[HTTP] %s %s %s", request.ClientIP(r), r.Method, r.RequestURI)
next.ServeHTTP(w, r)
})
}