Added some IP address improvements
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
namespace System.Net
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace System.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extension methods for <see cref="IPAddress"/>es.
|
||||
@@ -48,5 +51,90 @@
|
||||
|
||||
return new IPAddress(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an <see cref="IPAddress"/> to a clean string representation.
|
||||
/// </summary>
|
||||
/// <param name="ipAddress">The <see cref="IPAddress"/> to convert.</param>
|
||||
/// <returns>A clean string representation of the <see cref="IPAddress"/>.</returns>
|
||||
public static string ToCleanString(this IPAddress ipAddress)
|
||||
{
|
||||
string address = ipAddress.ToString();
|
||||
if (ipAddress.IsIPv4MappedToIPv6)
|
||||
address = ipAddress.MapToIPv4().ToString();
|
||||
|
||||
return address;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a prefix length to a subnet mask (IPv4 only).
|
||||
/// </summary>
|
||||
/// <param name="prefixLength">The prefix length.</param>
|
||||
public static IPAddress ToSubnetMask(this int prefixLength)
|
||||
{
|
||||
if (prefixLength < 0 || prefixLength > 32)
|
||||
throw new ArgumentOutOfRangeException(nameof(prefixLength));
|
||||
|
||||
byte[] bytes = new byte[4];
|
||||
for (int i = 0; i < prefixLength; i++)
|
||||
{
|
||||
int byteIndex = i / 8;
|
||||
int bitIndex = 7 - (i % 8);
|
||||
bytes[byteIndex] |= (byte)(1 << bitIndex);
|
||||
}
|
||||
|
||||
return new IPAddress(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Obfuscates an IP address by masking portions of it to conceal sensitive information.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method replaces parts of the IP address with a masking character to help prevent exposure of
|
||||
/// full address details in logs or user interfaces. For IPv4-mapped IPv6 addresses, the address is first converted to
|
||||
/// its IPv4 equivalent before obfuscation. Loopback addresses (<c>127.0.0.1</c> and <c>::1</c>) are not obfuscated.
|
||||
/// </remarks>
|
||||
/// <param name="address">The IP address to obfuscate.</param>
|
||||
/// <param name="obfuscation">The char (or string) to obfuscate with.</param>
|
||||
/// <returns>
|
||||
/// A string representation of the obfuscated IP address. For loopback addresses, the original address is returned unmodified.
|
||||
/// </returns>
|
||||
public static string ObfuscateIpAddress(this IPAddress address, string obfuscation = "•")
|
||||
{
|
||||
string[] addressParts;
|
||||
string delimiter;
|
||||
switch (address.AddressFamily)
|
||||
{
|
||||
case AddressFamily.InterNetwork:
|
||||
if (address.ToString() == "127.0.0.1")
|
||||
return address.ToString();
|
||||
|
||||
delimiter = ".";
|
||||
addressParts = address.ToString().Split(delimiter.First());
|
||||
break;
|
||||
|
||||
case AddressFamily.InterNetworkV6:
|
||||
if (address.IsIPv4MappedToIPv6)
|
||||
return address.MapToIPv4().ObfuscateIpAddress(obfuscation);
|
||||
|
||||
if (address.ToString() == "::1")
|
||||
return address.ToString();
|
||||
|
||||
delimiter = ":";
|
||||
addressParts = address.ToString().Split(delimiter.First());
|
||||
break;
|
||||
|
||||
default:
|
||||
return address.ToString();
|
||||
}
|
||||
|
||||
for (int i = 0; i < addressParts.Length; i++)
|
||||
{
|
||||
if (i % 2 == 0)
|
||||
addressParts[i] = obfuscation;
|
||||
}
|
||||
|
||||
return string.Join(delimiter, addressParts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user