|
8 | 8 | using System.ComponentModel; |
9 | 9 | using System.Data; |
10 | 10 | using System.Linq.Expressions; |
| 11 | +using System.Net; |
| 12 | +using System.Net.Sockets; |
11 | 13 | using System.Reflection; |
| 14 | +using System.Runtime.Versioning; |
12 | 15 |
|
13 | 16 | namespace BootstrapBlazor.Components; |
14 | 17 |
|
@@ -898,4 +901,63 @@ static Expression<Func<ComponentBase, object, string, object>> CreateLambda(Type |
898 | 901 | /// <param name="type"></param> |
899 | 902 | /// <returns></returns> |
900 | 903 | public static IStringLocalizer? CreateLocalizer(Type type) => CacheManager.CreateLocalizerByType(type); |
| 904 | + |
| 905 | + /// <summary> |
| 906 | + /// Converts a string representation of an IP address or hostname into an <see cref="IPAddress"/> object. |
| 907 | + /// </summary> |
| 908 | + /// <remarks>This method handles common special cases for IP address strings, such as "localhost" and |
| 909 | + /// "any". For other inputs, it attempts to parse the string as an IP address using <see |
| 910 | + /// cref="IPAddress.TryParse(string, out IPAddress)"/>. If parsing fails, the method resolves the input as a |
| 911 | + /// hostname.</remarks> |
| 912 | + /// <param name="ipString">A string containing the IP address or hostname to convert. Special values include: <list type="bullet"> |
| 913 | + /// <item><description><c>"localhost"</c> returns the loopback address (<see |
| 914 | + /// cref="IPAddress.Loopback"/>).</description></item> <item><description><c>"any"</c> returns the wildcard address |
| 915 | + /// (<see cref="IPAddress.Any"/>).</description></item> </list> For other values, the method attempts to parse the |
| 916 | + /// string as an IP address or resolve it as a hostname.</param> |
| 917 | + /// <returns>An <see cref="IPAddress"/> object representing the parsed or resolved IP address. If the input cannot be parsed |
| 918 | + /// or resolved, the method returns a default IP address.</returns> |
| 919 | + [UnsupportedOSPlatform("browser")] |
| 920 | + public static IPAddress ConvertToIPAddress(string ipString) |
| 921 | + { |
| 922 | + if (string.IsNullOrEmpty(ipString)) |
| 923 | + { |
| 924 | + throw new ArgumentNullException(nameof(ipString), "IP address cannot be null or empty."); |
| 925 | + } |
| 926 | + |
| 927 | + if (ipString.Equals("localhost", StringComparison.OrdinalIgnoreCase)) |
| 928 | + { |
| 929 | + return IPAddress.Loopback; |
| 930 | + } |
| 931 | + if (ipString.Equals("any", StringComparison.OrdinalIgnoreCase)) |
| 932 | + { |
| 933 | + return IPAddress.Any; |
| 934 | + } |
| 935 | + |
| 936 | + return IPAddress.TryParse(ipString, out var ip) ? ip : IPAddressByHostName; |
| 937 | + } |
| 938 | + |
| 939 | + [ExcludeFromCodeCoverage] |
| 940 | + |
| 941 | + [UnsupportedOSPlatform("browser")] |
| 942 | + private static IPAddress IPAddressByHostName => Dns.GetHostAddresses(Dns.GetHostName(), AddressFamily.InterNetwork).FirstOrDefault() ?? IPAddress.Loopback; |
| 943 | + |
| 944 | + /// <summary> |
| 945 | + /// Converts a string representation of an IP address and a port number into an <see cref="IPEndPoint"/> instance. |
| 946 | + /// </summary> |
| 947 | + /// <remarks>This method is not supported on browser platforms.</remarks> |
| 948 | + /// <param name="ipString">The string representation of the IP address. Must be a valid IPv4 or IPv6 address.</param> |
| 949 | + /// <param name="port">The port number associated with the endpoint. Must be between 0 and 65535.</param> |
| 950 | + /// <returns>An <see cref="IPEndPoint"/> representing the specified IP address and port.</returns> |
| 951 | + /// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="port"/> is less than 0 or greater than 65535.</exception> |
| 952 | + [UnsupportedOSPlatform("browser")] |
| 953 | + public static IPEndPoint ConvertToIpEndPoint(string ipString, int port) |
| 954 | + { |
| 955 | + if (port < 0 || port > 65535) |
| 956 | + { |
| 957 | + throw new ArgumentOutOfRangeException(nameof(port), "Port must be between 0 and 65535."); |
| 958 | + } |
| 959 | + |
| 960 | + var address = ConvertToIPAddress(ipString); |
| 961 | + return new IPEndPoint(address, port); |
| 962 | + } |
901 | 963 | } |
0 commit comments