Commit 71d16e3d authored by Jehonathan's avatar Jehonathan Committed by GitHub

Merge pull request #155 from svstoichkov/release

Bind proxy requests to specific network adapter
parents 9f9a16af 3095d1ed
......@@ -15,6 +15,8 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Helpers
{
using System.Net;
internal enum IpVersion
{
Ipv4 = 1,
......@@ -173,7 +175,7 @@ namespace Titanium.Web.Proxy.Helpers
remoteHostName, remotePort,
httpVersion, isHttps,
supportedProtocols, remoteCertificateValidationCallback, localCertificateSelectionCallback,
null, null, clientStream);
null, null, clientStream, new IPEndPoint(IPAddress.Any, 0));
try
{
......
......@@ -12,6 +12,8 @@ using Titanium.Web.Proxy.Shared;
namespace Titanium.Web.Proxy.Network.Tcp
{
using System.Net;
/// <summary>
/// A class that manages Tcp Connection to server used by this proxy server
/// </summary>
......@@ -40,7 +42,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
bool isHttps, SslProtocols supportedSslProtocols,
RemoteCertificateValidationCallback remoteCertificateValidationCallback, LocalCertificateSelectionCallback localCertificateSelectionCallback,
ExternalProxy externalHttpProxy, ExternalProxy externalHttpsProxy,
Stream clientStream)
Stream clientStream, EndPoint localEndPoint)
{
TcpClient client;
Stream stream;
......@@ -52,7 +54,9 @@ namespace Titanium.Web.Proxy.Network.Tcp
//If this proxy uses another external proxy then create a tunnel request for HTTPS connections
if (externalHttpsProxy != null && externalHttpsProxy.HostName != remoteHostName)
{
client = new TcpClient(externalHttpsProxy.HostName, externalHttpsProxy.Port);
client = new TcpClient();
client.Client.Bind(localEndPoint);
client.Client.Connect(externalHttpsProxy.HostName, externalHttpsProxy.Port);
stream = client.GetStream();
using (var writer = new StreamWriter(stream, Encoding.ASCII, bufferSize, true) { NewLine = ProxyConstants.NewLine })
......@@ -76,7 +80,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
var result = await reader.ReadLineAsync();
if (!new string[] { "200 OK", "connection established" }.Any(s=> result.ToLower().Contains(s.ToLower())))
if (!new string[] { "200 OK", "connection established" }.Any(s => result.ToLower().Contains(s.ToLower())))
{
throw new Exception("Upstream proxy failed to create a secure tunnel");
}
......@@ -86,7 +90,9 @@ namespace Titanium.Web.Proxy.Network.Tcp
}
else
{
client = new TcpClient(remoteHostName, remotePort);
client = new TcpClient();
client.Client.Bind(localEndPoint);
client.Client.Connect(remoteHostName, remotePort);
stream = client.GetStream();
}
......@@ -110,12 +116,16 @@ namespace Titanium.Web.Proxy.Network.Tcp
{
if (externalHttpProxy != null && externalHttpProxy.HostName != remoteHostName)
{
client = new TcpClient(externalHttpProxy.HostName, externalHttpProxy.Port);
client = new TcpClient();
client.Client.Bind(localEndPoint);
client.Client.Connect(externalHttpProxy.HostName, externalHttpProxy.Port);
stream = client.GetStream();
}
else
{
client = new TcpClient(remoteHostName, remotePort);
client = new TcpClient();
client.Client.Bind(localEndPoint);
client.Client.Connect(remoteHostName, remotePort);
stream = client.GetStream();
}
}
......@@ -126,6 +136,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
stream.ReadTimeout = connectionTimeOutSeconds * 1000;
stream.WriteTimeout = connectionTimeOutSeconds * 1000;
return new TcpConnection()
{
UpStreamHttpProxy = externalHttpProxy,
......@@ -140,4 +151,4 @@ namespace Titanium.Web.Proxy.Network.Tcp
};
}
}
}
}
\ No newline at end of file
......@@ -117,6 +117,11 @@ namespace Titanium.Web.Proxy
/// </summary>
public ExternalProxy UpStreamHttpsProxy { get; set; }
/// <summary>
/// Local adapter endpoint
/// </summary>
public EndPoint LocalEndPoint { get; set; } = new IPEndPoint(IPAddress.Any, 0);
/// <summary>
/// Verifies the remote Secure Sockets Layer (SSL) certificate used for authentication
/// </summary>
......
......@@ -251,7 +251,7 @@ namespace Titanium.Web.Proxy
args.IsHttps, SupportedSslProtocols,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
new LocalCertificateSelectionCallback(SelectClientCertificate),
customUpStreamHttpProxy ?? UpStreamHttpProxy, customUpStreamHttpsProxy ?? UpStreamHttpsProxy, args.ProxyClient.ClientStream);
customUpStreamHttpProxy ?? UpStreamHttpProxy, customUpStreamHttpsProxy ?? UpStreamHttpsProxy, args.ProxyClient.ClientStream, LocalEndPoint);
}
args.WebSession.Request.RequestLocked = true;
......
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