Display correct RemoteIP in logs.
parent
4dd6260ac8
commit
bdf0c07a41
|
@ -3,7 +3,6 @@ package logging
|
||||||
import (
|
import (
|
||||||
logrus_stack "github.com/Gurpartap/logrus-stack"
|
logrus_stack "github.com/Gurpartap/logrus-stack"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/valyala/fasthttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -17,15 +16,6 @@ func Logger() *logrus.Logger {
|
||||||
return logrus.StandardLogger()
|
return logrus.StandardLogger()
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRequestLogger create a new request logger for the given request.
|
|
||||||
func NewRequestLogger(ctx *fasthttp.RequestCtx) *logrus.Entry {
|
|
||||||
return logrus.WithFields(logrus.Fields{
|
|
||||||
"method": string(ctx.Method()),
|
|
||||||
"path": string(ctx.Path()),
|
|
||||||
"remote_ip": ctx.RemoteIP().String(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetLevel set the level of the logger.
|
// SetLevel set the level of the logger.
|
||||||
func SetLevel(level logrus.Level) {
|
func SetLevel(level logrus.Level) {
|
||||||
logrus.SetLevel(level)
|
logrus.SetLevel(level)
|
||||||
|
|
|
@ -7,20 +7,28 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/asaskevich/govalidator"
|
"github.com/asaskevich/govalidator"
|
||||||
"github.com/clems4ever/authelia/internal/session"
|
|
||||||
|
|
||||||
"github.com/clems4ever/authelia/internal/configuration/schema"
|
"github.com/clems4ever/authelia/internal/configuration/schema"
|
||||||
"github.com/clems4ever/authelia/internal/logging"
|
"github.com/clems4ever/authelia/internal/session"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NewRequestLogger create a new request logger for the given request.
|
||||||
|
func NewRequestLogger(ctx *AutheliaCtx) *logrus.Entry {
|
||||||
|
return logrus.WithFields(logrus.Fields{
|
||||||
|
"method": string(ctx.Method()),
|
||||||
|
"path": string(ctx.Path()),
|
||||||
|
"remote_ip": ctx.RemoteIP().String(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// NewAutheliaCtx instantiate an AutheliaCtx out of a RequestCtx.
|
// NewAutheliaCtx instantiate an AutheliaCtx out of a RequestCtx.
|
||||||
func NewAutheliaCtx(ctx *fasthttp.RequestCtx, configuration schema.Configuration, providers Providers) (*AutheliaCtx, error) {
|
func NewAutheliaCtx(ctx *fasthttp.RequestCtx, configuration schema.Configuration, providers Providers) (*AutheliaCtx, error) {
|
||||||
autheliaCtx := new(AutheliaCtx)
|
autheliaCtx := new(AutheliaCtx)
|
||||||
autheliaCtx.RequestCtx = ctx
|
autheliaCtx.RequestCtx = ctx
|
||||||
autheliaCtx.Providers = providers
|
autheliaCtx.Providers = providers
|
||||||
autheliaCtx.Configuration = configuration
|
autheliaCtx.Configuration = configuration
|
||||||
autheliaCtx.Logger = logging.NewRequestLogger(ctx)
|
autheliaCtx.Logger = NewRequestLogger(autheliaCtx)
|
||||||
|
|
||||||
userSession, err := providers.SessionProvider.GetSession(ctx)
|
userSession, err := providers.SessionProvider.GetSession(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -153,7 +161,7 @@ func (c *AutheliaCtx) SetJSONBody(value interface{}) error {
|
||||||
|
|
||||||
// RemoteIP return the remote IP taking X-Forwarded-For header into account if provided.
|
// RemoteIP return the remote IP taking X-Forwarded-For header into account if provided.
|
||||||
func (c *AutheliaCtx) RemoteIP() net.IP {
|
func (c *AutheliaCtx) RemoteIP() net.IP {
|
||||||
XForwardedFor := c.RequestCtx.Request.Header.Peek("X-Forwarded-For")
|
XForwardedFor := c.Request.Header.Peek("X-Forwarded-For")
|
||||||
if XForwardedFor != nil {
|
if XForwardedFor != nil {
|
||||||
ips := strings.Split(string(XForwardedFor), ",")
|
ips := strings.Split(string(XForwardedFor), ",")
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
package middlewares
|
package middlewares
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/clems4ever/authelia/internal/logging"
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogRequestMiddleware logs the query that is being treated.
|
// LogRequestMiddleware logs the query that is being treated.
|
||||||
func LogRequestMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
func LogRequestMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler {
|
||||||
return func(ctx *fasthttp.RequestCtx) {
|
return func(ctx *fasthttp.RequestCtx) {
|
||||||
logger := logging.NewRequestLogger(ctx)
|
autheliaCtx := &AutheliaCtx{RequestCtx: ctx}
|
||||||
|
logger := NewRequestLogger(autheliaCtx)
|
||||||
|
|
||||||
logger.Trace("Request hit")
|
logger.Trace("Request hit")
|
||||||
next(ctx)
|
next(ctx)
|
||||||
|
|
Loading…
Reference in New Issue