Last active
April 26, 2021 13:02
-
-
Save OnSive/22eb6e1cff69c073a3f5c774545796ef to your computer and use it in GitHub Desktop.
Get private and public IP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #region Local & Public IP | |
| /// <summary> | |
| /// Returns the local and public IP of a network interface | |
| /// </summary> | |
| public static bool GetIPAddresses(string interfaceId, out string localIp, out string publicIp) | |
| => GetIpAddresses(NetworkInterface.GetAllNetworkInterfaces() | |
| .FirstOrDefault(x => string.Compare(x.Id, interfaceId, StringComparison.Ordinal) == 0), | |
| out localIp, out publicIp); | |
| /// <summary> | |
| /// Returns the local and public IP of a network interface | |
| /// </summary> | |
| public static bool GetIpAddresses(NetworkInterface networkInterface, out string localIp, out string publicIp) | |
| { | |
| localIp = string.Empty; | |
| publicIp = string.Empty; | |
| try | |
| { | |
| if ((networkInterface != null) && | |
| ((networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) || (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet))) | |
| { | |
| foreach (var oUnicastIPAddressInformation in networkInterface.GetIPProperties().UnicastAddresses | |
| .Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork)) | |
| { | |
| localIp = oUnicastIPAddressInformation.Address.ToString(); | |
| if ((networkInterface.OperationalStatus == OperationalStatus.Up) && | |
| (networkInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel) && | |
| (networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback)) | |
| { | |
| publicIp = GetPublicIpAddress(localIp); | |
| return !string.IsNullOrWhiteSpace(publicIp) && GlobalFunctions.Check_IPAdress(localIp, bCheckIPv6: true) && GlobalFunctions.Check_IPAdress(publicIp, bCheckIPv6: true); | |
| } | |
| } | |
| } | |
| return false; | |
| } | |
| catch (Exception ex) | |
| { | |
| return false; | |
| } | |
| } | |
| /// <summary> | |
| /// Returns the public IP of the device | |
| /// </summary> | |
| private static string GetPublicIpAddress(string localIp) | |
| { | |
| try | |
| { | |
| string publicIp = GetStringResponse(localIp, "https://icanhazip.com"); | |
| if ((publicIp == null) || !GlobalFunctions.Check_IPAdress(publicIp)) | |
| publicIp = GetStringResponse(localIp, "https://api.ipify.org"); | |
| if ((publicIp == null) || !GlobalFunctions.Check_IPAdress(publicIp)) | |
| publicIp = GetStringResponse(localIp, "https://api64.ipify.org"); | |
| return ((publicIp == null) || !GlobalFunctions.Check_IPAdress(publicIp, bCheckIPv6: true)) ? string.Empty : publicIp; | |
| } | |
| catch (Exception ex) | |
| { | |
| return string.Empty; | |
| } | |
| } | |
| /// <summary> | |
| /// Return the content of a web page. The request is send with the specified local IP address. | |
| /// </summary> | |
| private static string GetStringResponse(string localIp, string url) | |
| { | |
| try | |
| { | |
| var webRequest = (HttpWebRequest)WebRequest.Create(url); | |
| webRequest.ServicePoint.BindIPEndPointDelegate = (sp, remoteEndPoint, retryCount) => | |
| { | |
| if (retryCount > 3) | |
| throw new Exception("Cut the crap with ignoring timeouts!"); | |
| Console.WriteLine($"{nameof(GetStringResponse)} | IdleSince: {sp.IdleSince} | Remote Endpoint: {remoteEndPoint.Address}:{remoteEndPoint.Port} | Retry Count: {retryCount} | Own IP: {localIp}"); | |
| return new IPEndPoint(IPAddress.Parse(localIp), 0); | |
| }; | |
| using (var webResponse = webRequest.GetResponse()) | |
| { | |
| if (webResponse.ContentLength <= 0) | |
| return null; | |
| using (var reader = new StreamReader(webResponse.GetResponseStream())) | |
| return reader.ReadToEnd().Replace("\n", string.Empty); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| return null; | |
| } | |
| } | |
| #endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment