Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 4220af2

Browse files
Merge pull request #536 from jmh76/issue-535
Update Network.cs. Thanks
2 parents 4eb392f + 8010695 commit 4220af2

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Net;
4+
using System.Net.NetworkInformation;
35
using System.Net.Sockets;
46

57
namespace Titanium.Web.Proxy.Helpers
68
{
79
internal class NetworkHelper
810
{
11+
private static readonly string localhostName = Dns.GetHostName();
12+
private static readonly IPHostEntry localhostEntry = Dns.GetHostEntry(string.Empty);
13+
914
/// <summary>
1015
/// Adapated from below link
1116
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
@@ -19,55 +24,52 @@ internal static bool IsLocalIpAddress(IPAddress address)
1924
return true;
2025
}
2126

22-
// get local IP addresses
23-
var localIPs = Dns.GetHostAddresses(Dns.GetHostName());
24-
25-
// test if any host IP equals to any local IP or to localhost
26-
return localIPs.Contains(address);
27+
// test if host IP equals any local IP
28+
return localhostEntry.AddressList.Contains(address);
2729
}
2830

2931
internal static bool IsLocalIpAddress(string hostName)
3032
{
31-
hostName = hostName.ToLower();
32-
33-
if (hostName == "127.0.0.1"
34-
|| hostName == "localhost")
33+
if (IPAddress.TryParse(hostName, out var ipAddress)
34+
&& IsLocalIpAddress(ipAddress))
3535
{
3636
return true;
3737
}
3838

39-
var localhostDnsName = Dns.GetHostName().ToLower();
40-
41-
//if hostname matches current machine DNS name
42-
if (hostName == localhostDnsName)
39+
if (hostName.Equals("localhost", StringComparison.OrdinalIgnoreCase))
4340
{
4441
return true;
4542
}
4643

47-
var isLocalhost = false;
48-
IPHostEntry hostEntry = null;
49-
50-
//check if parsable to an IP Address
51-
if (IPAddress.TryParse(hostName, out var ipAddress))
44+
//if hostname matches local host name
45+
if (hostName.Equals(localhostName, StringComparison.OrdinalIgnoreCase))
5246
{
53-
hostEntry = Dns.GetHostEntry(localhostDnsName);
54-
isLocalhost = hostEntry.AddressList.Any(x => x.Equals(ipAddress));
47+
return true;
5548
}
5649

57-
if (!isLocalhost)
50+
// if hostname matches fully qualified local DNS name
51+
if (hostName.Equals(localhostEntry.HostName, StringComparison.OrdinalIgnoreCase))
5852
{
59-
try
60-
{
61-
hostEntry = Dns.GetHostEntry(hostName);
62-
isLocalhost = hostEntry.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals));
63-
}
64-
catch (SocketException)
53+
return true;
54+
}
55+
56+
try
57+
{
58+
// do reverse DNS lookup even if hostName is an IP address
59+
var hostEntry = Dns.GetHostEntry(hostName);
60+
// if DNS resolved hostname matches local DNS name,
61+
// or if host IP address list contains any local IP address
62+
if (hostEntry.HostName.Equals(localhostEntry.HostName, StringComparison.OrdinalIgnoreCase)
63+
|| hostEntry.AddressList.Any(hostIP => localhostEntry.AddressList.Contains(hostIP)))
6564
{
65+
return true;
6666
}
6767
}
68+
catch (SocketException)
69+
{
70+
}
6871

69-
70-
return isLocalhost;
72+
return false;
7173
}
7274
}
7375
}

0 commit comments

Comments
 (0)