mirror of
https://github.com/lidarr/lidarr.git
synced 2025-07-10 23:33:38 -07:00
Added UserAgent to api request trace log
Co-Authored-By: Taloth <Taloth@users.noreply.github.com>
This commit is contained in:
parent
807460771e
commit
8af09595fb
3 changed files with 66 additions and 51 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue