mirror of
https://github.com/Caraxi/mare.server.git
synced 2025-12-12 15:07:23 +01:00
adjust extensions, again and again
This commit is contained in:
parent
03d7549d29
commit
3490c5c8d8
1 changed files with 24 additions and 16 deletions
|
|
@ -4,28 +4,36 @@ namespace MareSynchronosShared;
|
|||
|
||||
public static class Extensions
|
||||
{
|
||||
private static long _noIpCntr = 0;
|
||||
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"];
|
||||
|
||||
if (!string.IsNullOrEmpty(accessor.HttpContext.Request.Headers["X-Forwarded-For"]))
|
||||
try
|
||||
{
|
||||
return accessor.HttpContext.Request.Headers["X-Forwarded-For"];
|
||||
}
|
||||
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.IsNullOrWhiteSpace(ipAddress))
|
||||
{
|
||||
var addresses = ipAddress.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
var lastEntry = addresses.LastOrDefault();
|
||||
if (lastEntry != null)
|
||||
if (!string.IsNullOrEmpty(accessor.HttpContext.Request.Headers["X-Forwarded-For"]))
|
||||
{
|
||||
return lastEntry;
|
||||
return accessor.HttpContext.Request.Headers["X-Forwarded-For"];
|
||||
}
|
||||
}
|
||||
|
||||
return accessor.HttpContext.Connection.RemoteIpAddress.ToString();
|
||||
var ipAddress = accessor.HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(ipAddress))
|
||||
{
|
||||
var addresses = ipAddress.Split(',', StringSplitOptions.RemoveEmptyEntries);
|
||||
var lastEntry = addresses.LastOrDefault();
|
||||
if (lastEntry != null)
|
||||
{
|
||||
return lastEntry;
|
||||
}
|
||||
}
|
||||
|
||||
return accessor.HttpContext.Connection.RemoteIpAddress?.ToString() ?? "NoIp";
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "NoIp" + _noIpCntr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue