Added headers to store original IP, host, and protocol values
This commit is contained in:
parent
dd8d0aa078
commit
ab56ac6038
|
|
@ -15,6 +15,9 @@ namespace Microsoft.AspNet.HttpOverrides
|
||||||
private const string XForwardedForHeaderName = "X-Forwarded-For";
|
private const string XForwardedForHeaderName = "X-Forwarded-For";
|
||||||
private const string XForwardedHostHeaderName = "X-Forwarded-Host";
|
private const string XForwardedHostHeaderName = "X-Forwarded-Host";
|
||||||
private const string XForwardedProtoHeaderName = "X-Forwarded-Proto";
|
private const string XForwardedProtoHeaderName = "X-Forwarded-Proto";
|
||||||
|
private const string XOriginalIPName = "X-Original-IP";
|
||||||
|
private const string XOriginalHostName = "X-Original-Host";
|
||||||
|
private const string XOriginalProtoName = "X-Original-Proto";
|
||||||
private readonly OverrideHeaderMiddlewareOptions _options;
|
private readonly OverrideHeaderMiddlewareOptions _options;
|
||||||
private readonly RequestDelegate _next;
|
private readonly RequestDelegate _next;
|
||||||
|
|
||||||
|
|
@ -31,15 +34,15 @@ namespace Microsoft.AspNet.HttpOverrides
|
||||||
var xForwardedForHeaderValue = context.Request.Headers.GetCommaSeparatedValues(XForwardedForHeaderName);
|
var xForwardedForHeaderValue = context.Request.Headers.GetCommaSeparatedValues(XForwardedForHeaderName);
|
||||||
if (xForwardedForHeaderValue != null && xForwardedForHeaderValue.Count > 0)
|
if (xForwardedForHeaderValue != null && xForwardedForHeaderValue.Count > 0)
|
||||||
{
|
{
|
||||||
IPAddress originalIPAddress;
|
IPAddress ipFromHeader;
|
||||||
if (IPAddress.TryParse(xForwardedForHeaderValue[0], out originalIPAddress))
|
if (IPAddress.TryParse(xForwardedForHeaderValue[0], out ipFromHeader))
|
||||||
{
|
{
|
||||||
if (context.Connection.RemoteIpAddress != null)
|
var remoteIPString = context.Connection.RemoteIpAddress?.ToString();
|
||||||
|
if (!string.IsNullOrEmpty(remoteIPString))
|
||||||
{
|
{
|
||||||
var ipList = context.Request.Headers.Get(XForwardedForHeaderName);
|
context.Request.Headers.Set(XOriginalIPName, remoteIPString);
|
||||||
context.Request.Headers.Set(XForwardedForHeaderName, (ipList + "," + context.Connection.RemoteIpAddress.ToString()));
|
|
||||||
}
|
}
|
||||||
context.Connection.RemoteIpAddress = originalIPAddress;
|
context.Connection.RemoteIpAddress = ipFromHeader;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -49,6 +52,11 @@ namespace Microsoft.AspNet.HttpOverrides
|
||||||
var xForwardHostHeaderValue = context.Request.Headers.Get(XForwardedHostHeaderName);
|
var xForwardHostHeaderValue = context.Request.Headers.Get(XForwardedHostHeaderName);
|
||||||
if (!string.IsNullOrEmpty(xForwardHostHeaderValue))
|
if (!string.IsNullOrEmpty(xForwardHostHeaderValue))
|
||||||
{
|
{
|
||||||
|
var hostString = context.Request.Host.ToString();
|
||||||
|
if (!string.IsNullOrEmpty(hostString))
|
||||||
|
{
|
||||||
|
context.Request.Headers.Set(XOriginalHostName, hostString);
|
||||||
|
}
|
||||||
context.Request.Host = HostString.FromUriComponent(xForwardHostHeaderValue);
|
context.Request.Host = HostString.FromUriComponent(xForwardHostHeaderValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -58,6 +66,10 @@ namespace Microsoft.AspNet.HttpOverrides
|
||||||
var xForwardProtoHeaderValue = context.Request.Headers.Get(XForwardedProtoHeaderName);
|
var xForwardProtoHeaderValue = context.Request.Headers.Get(XForwardedProtoHeaderName);
|
||||||
if (!string.IsNullOrEmpty(xForwardProtoHeaderValue))
|
if (!string.IsNullOrEmpty(xForwardProtoHeaderValue))
|
||||||
{
|
{
|
||||||
|
if (!string.IsNullOrEmpty(context.Request.Scheme))
|
||||||
|
{
|
||||||
|
context.Request.Headers.Set(XOriginalProtoName, context.Request.Scheme);
|
||||||
|
}
|
||||||
context.Request.Scheme = xForwardProtoHeaderValue;
|
context.Request.Scheme = xForwardProtoHeaderValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue