Commit 4dc981aa authored by justcoding121's avatar justcoding121 Committed by justcoding121

Merge pull request #155 from svstoichkov/release

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