From 8af09595fb7a09a4f9bcb427ae9909face305283 Mon Sep 17 00:00:00 2001 From: Qstick Date: Fri, 2 Oct 2020 23:02:42 -0400 Subject: [PATCH] Added UserAgent to api request trace log Co-Authored-By: Taloth --- .../Authentication/AuthenticationService.cs | 55 ++----------------- .../Pipelines/RequestLoggingPipeline.cs | 16 +++++- .../Extensions/RequestExtensions.cs | 46 ++++++++++++++++ 3 files changed, 66 insertions(+), 51 deletions(-) diff --git a/src/Lidarr.Http/Authentication/AuthenticationService.cs b/src/Lidarr.Http/Authentication/AuthenticationService.cs index d3588f3e8..091ca7efb 100644 --- a/src/Lidarr.Http/Authentication/AuthenticationService.cs +++ b/src/Lidarr.Http/Authentication/AuthenticationService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Linq; using System.Net; using System.Security.Claims; @@ -209,70 +209,27 @@ namespace Lidarr.Http.Authentication public void LogUnauthorized(NancyContext context) { - _authLogger.Info("Auth-Unauthorized ip {0} url '{1}'", GetRemoteIP(context), context.Request.Url.ToString()); + _authLogger.Info("Auth-Unauthorized ip {0} url '{1}'", context.GetRemoteIP(), context.Request.Url.ToString()); } private void LogInvalidated(NancyContext context) { - _authLogger.Info("Auth-Invalidated ip {0}", GetRemoteIP(context)); + _authLogger.Info("Auth-Invalidated ip {0}", context.GetRemoteIP()); } private void LogFailure(NancyContext context, string username) { - _authLogger.Warn("Auth-Failure ip {0} username '{1}'", GetRemoteIP(context), username); + _authLogger.Warn("Auth-Failure ip {0} username '{1}'", context.GetRemoteIP(), username); } private void LogSuccess(NancyContext context, string username) { - _authLogger.Info("Auth-Success ip {0} username '{1}'", GetRemoteIP(context), username); + _authLogger.Info("Auth-Success ip {0} username '{1}'", context.GetRemoteIP(), username); } private void LogLogout(NancyContext context, string username) { - _authLogger.Info("Auth-Logout ip {0} username '{1}'", GetRemoteIP(context), username); - } - - private string GetRemoteIP(NancyContext context) - { - if (context == null || context.Request == null) - { - return "Unknown"; - } - - var remoteAddress = context.Request.UserHostAddress; - IPAddress remoteIP; - - // Only check if forwarded by a local network reverse proxy - if (IPAddress.TryParse(remoteAddress, out remoteIP) && remoteIP.IsLocalAddress()) - { - var realIPHeader = context.Request.Headers["X-Real-IP"]; - if (realIPHeader.Any()) - { - return realIPHeader.First().ToString(); - } - - var forwardedForHeader = context.Request.Headers["X-Forwarded-For"]; - if (forwardedForHeader.Any()) - { - // Get the first address that was forwarded by a local IP to prevent remote clients faking another proxy - foreach (var forwardedForAddress in forwardedForHeader.SelectMany(v => v.Split(',')).Select(v => v.Trim()).Reverse()) - { - if (!IPAddress.TryParse(forwardedForAddress, out remoteIP)) - { - return remoteAddress; - } - - if (!remoteIP.IsLocalAddress()) - { - return forwardedForAddress; - } - - remoteAddress = forwardedForAddress; - } - } - } - - return remoteAddress; + _authLogger.Info("Auth-Logout ip {0} username '{1}'", context.GetRemoteIP(), username); } } } diff --git a/src/Lidarr.Http/Extensions/Pipelines/RequestLoggingPipeline.cs b/src/Lidarr.Http/Extensions/Pipelines/RequestLoggingPipeline.cs index 53353b6d4..ff8e66c11 100644 --- a/src/Lidarr.Http/Extensions/Pipelines/RequestLoggingPipeline.cs +++ b/src/Lidarr.Http/Extensions/Pipelines/RequestLoggingPipeline.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading; using Lidarr.Http.ErrorManagement; using Lidarr.Http.Extensions; @@ -42,7 +42,7 @@ namespace NzbDrone.Api.Extensions.Pipelines var reqPath = GetRequestPathAndQuery(context.Request); - _loggerHttp.Trace("Req: {0} [{1}] {2}", id, context.Request.Method, reqPath); + _loggerHttp.Trace("Req: {0} [{1}] {2} (from {3})", id, context.Request.Method, reqPath, GetOrigin(context)); return null; } @@ -89,5 +89,17 @@ namespace NzbDrone.Api.Extensions.Pipelines return request.Url.Path; } } + + private static string GetOrigin(NancyContext context) + { + if (context.Request.Headers.UserAgent.IsNullOrWhiteSpace()) + { + return context.GetRemoteIP(); + } + else + { + return $"{context.GetRemoteIP()} {context.Request.Headers.UserAgent}"; + } + } } } diff --git a/src/Lidarr.Http/Extensions/RequestExtensions.cs b/src/Lidarr.Http/Extensions/RequestExtensions.cs index b56fd3f65..ab1c3f7a6 100644 --- a/src/Lidarr.Http/Extensions/RequestExtensions.cs +++ b/src/Lidarr.Http/Extensions/RequestExtensions.cs @@ -1,5 +1,8 @@ using System; +using System.Linq; +using System.Net; using Nancy; +using NzbDrone.Common.Extensions; namespace Lidarr.Http.Extensions { @@ -90,5 +93,48 @@ namespace Lidarr.Http.Extensions return defaultValue; } + + public static string GetRemoteIP(this NancyContext context) + { + if (context == null || context.Request == null) + { + return "Unknown"; + } + + var remoteAddress = context.Request.UserHostAddress; + IPAddress remoteIP; + + // Only check if forwarded by a local network reverse proxy + if (IPAddress.TryParse(remoteAddress, out remoteIP) && remoteIP.IsLocalAddress()) + { + var realIPHeader = context.Request.Headers["X-Real-IP"]; + if (realIPHeader.Any()) + { + return realIPHeader.First().ToString(); + } + + var forwardedForHeader = context.Request.Headers["X-Forwarded-For"]; + if (forwardedForHeader.Any()) + { + // Get the first address that was forwarded by a local IP to prevent remote clients faking another proxy + foreach (var forwardedForAddress in forwardedForHeader.SelectMany(v => v.Split(',')).Select(v => v.Trim()).Reverse()) + { + if (!IPAddress.TryParse(forwardedForAddress, out remoteIP)) + { + return remoteAddress; + } + + if (!remoteIP.IsLocalAddress()) + { + return forwardedForAddress; + } + + remoteAddress = forwardedForAddress; + } + } + } + + return remoteAddress; + } } }