mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-16 11:37:58 +02:00
Added UserAgent to api request trace log
This commit is contained in:
parent
66ee28d0a9
commit
0716d0931a
@ -203,70 +203,27 @@ private string GetApiKey(NancyContext context)
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ private Response LogStart(NancyContext context)
|
||||
|
||||
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 @@ private static string GetRequestPathAndQuery(Request request)
|
||||
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}";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using Nancy;
|
||||
using NzbDrone.Common.Extensions;
|
||||
|
||||
namespace Sonarr.Http.Extensions
|
||||
{
|
||||
@ -78,5 +81,48 @@ public static int GetIntegerQueryParameter(this Request request, string paramete
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user