Commit 07ff8ab2 authored by justcoding121's avatar justcoding121

refactor local IP check

parent da6856d2
...@@ -21,44 +21,52 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -21,44 +21,52 @@ namespace Titanium.Web.Proxy.Helpers
// get local IP addresses // get local IP addresses
var localIPs = Dns.GetHostAddresses(Dns.GetHostName()); var localIPs = Dns.GetHostAddresses(Dns.GetHostName());
// test if any host IP equals to any local IP or to localhost // test if any host IP equals to any local IP or to localhost
return localIPs.Contains(address); return localIPs.Contains(address);
} }
internal static bool IsLocalIpAddress(string hostName) internal static bool IsLocalIpAddress(string hostName)
{ {
bool isLocalhost = false; hostName = hostName.ToLower();
var localhost = Dns.GetHostEntry("127.0.0.1"); if (hostName == "127.0.0.1"
if (hostName == localhost.HostName) || hostName == "localhost")
{ {
var hostEntry = Dns.GetHostEntry(hostName); return true;
isLocalhost = hostEntry.AddressList.Any(IPAddress.IsLoopback);
} }
if (!isLocalhost) var localhostDnsName = Dns.GetHostName().ToLower();
//if hostname matches current machine DNS name
if (hostName == localhostDnsName)
{
return true;
}
var isLocalhost = false;
IPHostEntry hostEntry = null;
//check if parsable to an IP Address
if (IPAddress.TryParse(hostName, out var ipAddress))
{ {
localhost = Dns.GetHostEntry(Dns.GetHostName()); hostEntry = Dns.GetHostEntry(localhostDnsName);
isLocalhost = hostEntry.AddressList.Any(x => x.Equals(ipAddress));
}
if (IPAddress.TryParse(hostName, out var ipAddress)) if (!isLocalhost)
{
try
{ {
isLocalhost = localhost.AddressList.Any(x => x.Equals(ipAddress)); hostEntry = Dns.GetHostEntry(hostName);
isLocalhost = hostEntry.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals));
} }
catch (SocketException)
if (!isLocalhost)
{ {
try
{
var hostEntry = Dns.GetHostEntry(hostName);
isLocalhost = localhost.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals));
}
catch (SocketException)
{
}
} }
} }
return isLocalhost; return isLocalhost;
} }
} }
......
...@@ -229,17 +229,13 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -229,17 +229,13 @@ namespace Titanium.Web.Proxy.Network.Tcp
ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy, ProxyServer proxyServer, IPEndPoint upStreamEndPoint, ExternalProxy externalProxy,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
if (remoteHostName == "localhost" //deny connection to proxy end points to avoid infinite connection loop.
|| remoteHostName == "127.0.0.1" if (server.ProxyEndPoints.Any(x => x.Port == remotePort)
|| remoteHostName == Dns.GetHostName()) && NetworkHelper.IsLocalIpAddress(remoteHostName))
{ {
if (server.ProxyEndPoints.Any(x => x.Port == remotePort)) throw new Exception($"A client is making HTTP request to one of the listening ports of this proxy {remoteHostName}:{remotePort}");
{
throw new Exception($"A client is making HTTP request to one of the listening ports of this proxy {remoteHostName}:{remotePort}");
}
} }
bool useUpstreamProxy = false; bool useUpstreamProxy = false;
// check if external proxy is set for HTTP/HTTPS // check if external proxy is set for HTTP/HTTPS
......
...@@ -442,7 +442,7 @@ namespace Titanium.Web.Proxy ...@@ -442,7 +442,7 @@ namespace Titanium.Web.Proxy
systemProxySettingsManager.SetProxy( systemProxySettingsManager.SetProxy(
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Any) |
Equals(endPoint.IpAddress, IPAddress.Loopback) Equals(endPoint.IpAddress, IPAddress.Loopback)
? "127.0.0.1" ? "localhost"
: endPoint.IpAddress.ToString(), : endPoint.IpAddress.ToString(),
endPoint.Port, endPoint.Port,
protocolType); protocolType);
...@@ -545,8 +545,7 @@ namespace Titanium.Web.Proxy ...@@ -545,8 +545,7 @@ namespace Titanium.Web.Proxy
var protocolToRemove = ProxyProtocolType.None; var protocolToRemove = ProxyProtocolType.None;
foreach (var proxy in proxyInfo.Proxies.Values) foreach (var proxy in proxyInfo.Proxies.Values)
{ {
if ((proxy.HostName == "127.0.0.1" if (NetworkHelper.IsLocalIpAddress(proxy.HostName)
|| proxy.HostName.EqualsIgnoreCase("localhost"))
&& ProxyEndPoints.Any(x => x.Port == proxy.Port)) && ProxyEndPoints.Any(x => x.Port == proxy.Port))
{ {
protocolToRemove |= proxy.ProtocolType; protocolToRemove |= proxy.ProtocolType;
......
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