Holy crap auth works (#2099)
* Holy crap auth works * Fix some usages of UserID instead of UserName * Refactor preferences. They be non-async now. Also faster. * Rename DbContext. * Guest username assignment. * Fix saving of profiles. * Don't store data for guests. * Fix generating invalid random colors. * Don't allow dumb garbage for char preferences. * Bans. * Lol forgot to fill out the command description. * Connection log. * Rename all the tables and columns to be snake_case. * Re-do migrations. * Fixing tests and warnings. * Update submodule
This commit is contained in:
committed by
GitHub
parent
8a33e0a9bd
commit
66c8a68891
79
Content.Server/Utility/IPAddressExt.cs
Normal file
79
Content.Server/Utility/IPAddressExt.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Content.Server.Utility
|
||||
{
|
||||
public static class IPAddressExt
|
||||
{
|
||||
// Taken from https://stackoverflow.com/a/56461160/4678631
|
||||
public static bool IsInSubnet(this IPAddress address, string subnetMask)
|
||||
{
|
||||
var slashIdx = subnetMask.IndexOf("/", StringComparison.Ordinal);
|
||||
if (slashIdx == -1)
|
||||
{
|
||||
// We only handle netmasks in format "IP/PrefixLength".
|
||||
throw new NotSupportedException("Only SubNetMasks with a given prefix length are supported.");
|
||||
}
|
||||
|
||||
// First parse the address of the netmask before the prefix length.
|
||||
var maskAddress = IPAddress.Parse(subnetMask.Substring(0, slashIdx));
|
||||
|
||||
if (maskAddress.AddressFamily != address.AddressFamily)
|
||||
{
|
||||
// We got something like an IPV4-Address for an IPv6-Mask. This is not valid.
|
||||
return false;
|
||||
}
|
||||
|
||||
// Now find out how long the prefix is.
|
||||
int maskLength = int.Parse(subnetMask.Substring(slashIdx + 1));
|
||||
|
||||
if (maskAddress.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
// Convert the mask address to an unsigned integer.
|
||||
var maskAddressBits = BitConverter.ToUInt32(maskAddress.GetAddressBytes().Reverse().ToArray(), 0);
|
||||
|
||||
// And convert the IpAddress to an unsigned integer.
|
||||
var ipAddressBits = BitConverter.ToUInt32(address.GetAddressBytes().Reverse().ToArray(), 0);
|
||||
|
||||
// Get the mask/network address as unsigned integer.
|
||||
uint mask = uint.MaxValue << (32 - maskLength);
|
||||
|
||||
// https://stackoverflow.com/a/1499284/3085985
|
||||
// Bitwise AND mask and MaskAddress, this should be the same as mask and IpAddress
|
||||
// as the end of the mask is 0000 which leads to both addresses to end with 0000
|
||||
// and to start with the prefix.
|
||||
return (maskAddressBits & mask) == (ipAddressBits & mask);
|
||||
}
|
||||
|
||||
if (maskAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
// Convert the mask address to a BitArray.
|
||||
var maskAddressBits = new BitArray(maskAddress.GetAddressBytes());
|
||||
|
||||
// And convert the IpAddress to a BitArray.
|
||||
var ipAddressBits = new BitArray(address.GetAddressBytes());
|
||||
|
||||
if (maskAddressBits.Length != ipAddressBits.Length)
|
||||
{
|
||||
throw new ArgumentException("Length of IP Address and Subnet Mask do not match.");
|
||||
}
|
||||
|
||||
// Compare the prefix bits.
|
||||
for (int maskIndex = 0; maskIndex < maskLength; maskIndex++)
|
||||
{
|
||||
if (ipAddressBits[maskIndex] != maskAddressBits[maskIndex])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
throw new NotSupportedException("Only InterNetworkV6 or InterNetwork address families are supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user