This commit is contained in:
31
server/AyaNova/util/IsLocalExtension.cs
Normal file
31
server/AyaNova/util/IsLocalExtension.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
|
||||
//https://stackoverflow.com/a/41242493/8939
|
||||
public static class IsLocalExtension
|
||||
{
|
||||
|
||||
private const string NullIpAddress = "::1";
|
||||
|
||||
public static bool IsLocal(this HttpRequest req)
|
||||
{
|
||||
var connection = req.HttpContext.Connection;
|
||||
if (connection.RemoteIpAddress.IsSet())
|
||||
{
|
||||
//We have a remote address set up
|
||||
return connection.LocalIpAddress.IsSet()
|
||||
//Is local is same as remote, then we are local
|
||||
? connection.RemoteIpAddress.Equals(connection.LocalIpAddress)
|
||||
//else we are remote if the remote IP address is not a loopback address
|
||||
: IPAddress.IsLoopback(connection.RemoteIpAddress);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsSet(this IPAddress address)
|
||||
{
|
||||
return address != null && address.ToString() != NullIpAddress;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user