Commit 1ebc346b authored by Daniel Herken's avatar Daniel Herken

Added option to bypass external proxy for localhost connections

parent 196bf663
......@@ -18,6 +18,11 @@ namespace Titanium.Web.Proxy.Models
/// </summary>
public bool UseDefaultCredentials { get; set; }
/// <summary>
/// Bypass this proxy for connections to localhost?
/// </summary>
public bool BypassForLocalhost { get; set; }
/// <summary>
/// Username.
/// </summary>
......
......@@ -46,12 +46,17 @@ namespace Titanium.Web.Proxy.Network.Tcp
TcpClient client;
Stream stream;
bool isLocalhost = IsLocalhost(remoteHostName);
bool useHttpsProxy = externalHttpsProxy != null && externalHttpsProxy.HostName != remoteHostName && (externalHttpsProxy.BypassForLocalhost && !isLocalhost);
bool useHttpProxy = externalHttpProxy != null && externalHttpProxy.HostName != remoteHostName && (externalHttpProxy.BypassForLocalhost && !isLocalhost);
if (isHttps)
{
SslStream sslStream = null;
//If this proxy uses another external proxy then create a tunnel request for HTTPS connections
if (externalHttpsProxy != null && externalHttpsProxy.HostName != remoteHostName)
if (useHttpsProxy)
{
client = new TcpClient();
client.Client.Bind(upStreamEndPoint);
......@@ -113,7 +118,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
else
{
if (externalHttpProxy != null && externalHttpProxy.HostName != remoteHostName)
if (useHttpProxy)
{
client = new TcpClient();
client.Client.Bind(upStreamEndPoint);
......@@ -149,5 +154,42 @@ namespace Titanium.Web.Proxy.Network.Tcp
Version = httpVersion
};
}
private bool IsLocalhost(string hostName)
{
bool isLocalhost = false;
IPHostEntry localhost = Dns.GetHostEntry("127.0.0.1");
if (hostName == localhost.HostName)
{
IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
isLocalhost = hostEntry.AddressList.Any(IPAddress.IsLoopback);
}
if (!isLocalhost)
{
localhost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = null;
if (IPAddress.TryParse(hostName, out ipAddress))
isLocalhost = localhost.AddressList.Any(x => x.Equals(ipAddress));
if (!isLocalhost)
{
try
{
var hostEntry = Dns.GetHostEntry(hostName);
isLocalhost = localhost.AddressList.Any(x => hostEntry.AddressList.Any(x.Equals));
}
catch (SocketException)
{
}
}
}
return isLocalhost;
}
}
}
\ No newline at end of file
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