Commit 07ff8ab2 authored by justcoding121's avatar justcoding121

refactor local IP check

parent da6856d2
...@@ -28,36 +28,44 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -28,36 +28,44 @@ namespace Titanium.Web.Proxy.Helpers
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)
{ {
localhost = Dns.GetHostEntry(Dns.GetHostName()); return true;
}
var isLocalhost = false;
IPHostEntry hostEntry = null;
//check if parsable to an IP Address
if (IPAddress.TryParse(hostName, out var ipAddress)) if (IPAddress.TryParse(hostName, out var ipAddress))
{ {
isLocalhost = localhost.AddressList.Any(x => x.Equals(ipAddress)); hostEntry = Dns.GetHostEntry(localhostDnsName);
isLocalhost = hostEntry.AddressList.Any(x => x.Equals(ipAddress));
} }
if (!isLocalhost) if (!isLocalhost)
{ {
try try
{ {
var hostEntry = Dns.GetHostEntry(hostName); hostEntry = Dns.GetHostEntry(hostName);
isLocalhost = localhost.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals)); isLocalhost = hostEntry.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals));
} }
catch (SocketException) catch (SocketException)
{ {
} }
} }
}
return isLocalhost; return isLocalhost;
} }
......
...@@ -229,16 +229,12 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -229,16 +229,12 @@ 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;
......
...@@ -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