mirror of
https://github.com/fergalmoran/DnsServer.git
synced 2025-12-27 20:09:23 +00:00
Extensions: updated GetRemoteEndPoint() to allow controlling private IP check for x-real-ip header.
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user