Unverified Commit 4220af29 authored by Jehonathan Thomas's avatar Jehonathan Thomas Committed by GitHub

Merge pull request #536 from jmh76/issue-535

Update Network.cs.

Thanks
parents 4eb392fa 8010695a
using System.Linq; using System;
using System.Linq;
using System.Net; using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
internal class NetworkHelper internal class NetworkHelper
{ {
private static readonly string localhostName = Dns.GetHostName();
private static readonly IPHostEntry localhostEntry = Dns.GetHostEntry(string.Empty);
/// <summary> /// <summary>
/// Adapated from below link /// Adapated from below link
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost /// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
...@@ -19,55 +24,52 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -19,55 +24,52 @@ namespace Titanium.Web.Proxy.Helpers
return true; return true;
} }
// get local IP addresses // test if host IP equals any local IP
var localIPs = Dns.GetHostAddresses(Dns.GetHostName()); return localhostEntry.AddressList.Contains(address);
// test if any host IP equals to any local IP or to localhost
return localIPs.Contains(address);
} }
internal static bool IsLocalIpAddress(string hostName) internal static bool IsLocalIpAddress(string hostName)
{ {
hostName = hostName.ToLower(); if (IPAddress.TryParse(hostName, out var ipAddress)
&& IsLocalIpAddress(ipAddress))
if (hostName == "127.0.0.1"
|| hostName == "localhost")
{ {
return true; return true;
} }
var localhostDnsName = Dns.GetHostName().ToLower(); if (hostName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
//if hostname matches current machine DNS name
if (hostName == localhostDnsName)
{ {
return true; return true;
} }
var isLocalhost = false; //if hostname matches local host name
IPHostEntry hostEntry = null; if (hostName.Equals(localhostName, StringComparison.OrdinalIgnoreCase))
//check if parsable to an IP Address
if (IPAddress.TryParse(hostName, out var ipAddress))
{ {
hostEntry = Dns.GetHostEntry(localhostDnsName); return true;
isLocalhost = hostEntry.AddressList.Any(x => x.Equals(ipAddress));
} }
if (!isLocalhost) // if hostname matches fully qualified local DNS name
if (hostName.Equals(localhostEntry.HostName, StringComparison.OrdinalIgnoreCase))
{ {
try return true;
{ }
hostEntry = Dns.GetHostEntry(hostName);
isLocalhost = hostEntry.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals)); try
} {
catch (SocketException) // do reverse DNS lookup even if hostName is an IP address
var hostEntry = Dns.GetHostEntry(hostName);
// if DNS resolved hostname matches local DNS name,
// or if host IP address list contains any local IP address
if (hostEntry.HostName.Equals(localhostEntry.HostName, StringComparison.OrdinalIgnoreCase)
|| hostEntry.AddressList.Any(hostIP => localhostEntry.AddressList.Contains(hostIP)))
{ {
return true;
} }
} }
catch (SocketException)
{
}
return false;
return isLocalhost;
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment