1
- using System . Linq ;
1
+ using System ;
2
+ using System . Linq ;
2
3
using System . Net ;
4
+ using System . Net . NetworkInformation ;
3
5
using System . Net . Sockets ;
4
6
5
7
namespace Titanium . Web . Proxy . Helpers
6
8
{
7
9
internal class NetworkHelper
8
10
{
11
+ private static readonly string localhostName = Dns . GetHostName ( ) ;
12
+ private static readonly IPHostEntry localhostEntry = Dns . GetHostEntry ( string . Empty ) ;
13
+
9
14
/// <summary>
10
15
/// Adapated from below link
11
16
/// http://stackoverflow.com/questions/11834091/how-to-check-if-localhost
@@ -19,55 +24,52 @@ internal static bool IsLocalIpAddress(IPAddress address)
19
24
return true ;
20
25
}
21
26
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 ) ;
27
29
}
28
30
29
31
internal static bool IsLocalIpAddress ( string hostName )
30
32
{
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 ) )
35
35
{
36
36
return true ;
37
37
}
38
38
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 ) )
43
40
{
44
41
return true ;
45
42
}
46
43
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 ) )
52
46
{
53
- hostEntry = Dns . GetHostEntry ( localhostDnsName ) ;
54
- isLocalhost = hostEntry . AddressList . Any ( x => x . Equals ( ipAddress ) ) ;
47
+ return true ;
55
48
}
56
49
57
- if ( ! isLocalhost )
50
+ // if hostname matches fully qualified local DNS name
51
+ if ( hostName . Equals ( localhostEntry . HostName , StringComparison . OrdinalIgnoreCase ) )
58
52
{
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 ) ) )
65
64
{
65
+ return true ;
66
66
}
67
67
}
68
+ catch ( SocketException )
69
+ {
70
+ }
68
71
69
-
70
- return isLocalhost ;
72
+ return false ;
71
73
}
72
74
}
73
75
}
0 commit comments