get actual IP from connection

This commit is contained in:
Stanley Dimant
2022-08-03 19:42:02 +02:00
parent d5b7dd69e8
commit 9e0ac74de7
2 changed files with 28 additions and 3 deletions

View File

@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Http;
namespace MareSynchronosServer
{
public static class Extensions
{
public static string GetIpAddress(this IHttpContextAccessor accessor)
{
if (!string.IsNullOrEmpty(accessor.HttpContext.Request.Headers["CF-CONNECTING-IP"]))
return accessor.HttpContext.Request.Headers["CF-CONNECTING-IP"];
var ipAddress = accessor.HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR");
if (!string.IsNullOrEmpty(ipAddress))
{
var addresses = ipAddress.Split(',');
if (addresses.Length != 0)
return addresses.Last();
}
return accessor.HttpContext.Connection.RemoteIpAddress.ToString();
}
}
}