Extensions: updated GetRemoteEndPoint() to allow controlling private IP check for x-real-ip header.

This commit is contained in:
Shreyas Zare
2024-11-16 17:20:21 +05:30
parent 50b263c829
commit 161eeccb90

View File

@@ -32,7 +32,7 @@ namespace DnsServerCore
{
readonly static string[] HTTP_METHODS = new string[] { "GET", "POST" };
public static IPEndPoint GetRemoteEndPoint(this HttpContext context, string realIpHeaderName)
public static IPEndPoint GetRemoteEndPoint(this HttpContext context, string realIpHeaderName = null, bool readRealIpHeaderForPrivateIpOnly = true)
{
try
{
@@ -43,14 +43,12 @@ namespace DnsServerCore
if (remoteIP.IsIPv4MappedToIPv6)
remoteIP = remoteIP.MapToIPv4();
if (!string.IsNullOrEmpty(realIpHeaderName) && NetUtilities.IsPrivateIP(remoteIP))
if (!string.IsNullOrEmpty(realIpHeaderName) && (!readRealIpHeaderForPrivateIpOnly || NetUtilities.IsPrivateIP(remoteIP)))
{
//get the real IP address of the requesting client from X-Real-IP header set in nginx proxy_pass block
string xRealIp = context.Request.Headers[realIpHeaderName];
if (IPAddress.TryParse(xRealIp, out IPAddress address))
{
//get the real IP address of the requesting client from X-Real-IP header set in nginx proxy_pass block
return new IPEndPoint(address, 0);
}
}
return new IPEndPoint(remoteIP, context.Connection.RemotePort);