Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Titanium-Web-Proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Titanium-Web-Proxy
Commits
4220af29
Unverified
Commit
4220af29
authored
Jan 04, 2019
by
Jehonathan Thomas
Committed by
GitHub
Jan 04, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #536 from jmh76/issue-535
Update Network.cs. Thanks
parents
4eb392fa
8010695a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
30 deletions
+32
-30
Network.cs
src/Titanium.Web.Proxy/Helpers/Network.cs
+32
-30
No files found.
src/Titanium.Web.Proxy/Helpers/Network.cs
View file @
4220af29
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
;
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment