Commit 903a5958 authored by Honfika's avatar Honfika

Stream disposing fix, SslStream should dispose the inner stream (true parameter removed)

parent d837e87b
...@@ -146,14 +146,6 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -146,14 +146,6 @@ namespace Titanium.Web.Proxy.Helpers
return baseStream.BeginWrite(buffer, offset, count, callback, state); return baseStream.BeginWrite(buffer, offset, count, callback, state);
} }
/// <summary>
/// Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed.
/// </summary>
public override void Close()
{
baseStream.Close();
}
/// <summary> /// <summary>
/// Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token. /// Asynchronously reads the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.
/// </summary> /// </summary>
......
...@@ -213,7 +213,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -213,7 +213,7 @@ namespace Titanium.Web.Proxy.Helpers
var tcpConnection = await tcpConnectionFactory.CreateClient(server, var tcpConnection = await tcpConnectionFactory.CreateClient(server,
remoteHostName, remotePort, remoteHostName, remotePort,
httpVersion, isHttps, httpVersion, isHttps,
null, null, clientStream); null, null);
try try
{ {
......
...@@ -26,7 +26,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -26,7 +26,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// </summary> /// </summary>
internal Version Version { get; set; } internal Version Version { get; set; }
internal TcpClient TcpClient { get; set; } internal TcpClient TcpClient { private get; set; }
/// <summary> /// <summary>
/// used to read lines from server /// used to read lines from server
...@@ -54,7 +54,6 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -54,7 +54,6 @@ namespace Titanium.Web.Proxy.Network.Tcp
public void Dispose() public void Dispose()
{ {
Stream?.Close(); Stream?.Close();
Stream?.Dispose();
StreamReader?.Dispose(); StreamReader?.Dispose();
......
...@@ -28,17 +28,12 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -28,17 +28,12 @@ namespace Titanium.Web.Proxy.Network.Tcp
/// <param name="isHttps"></param> /// <param name="isHttps"></param>
/// <param name="externalHttpProxy"></param> /// <param name="externalHttpProxy"></param>
/// <param name="externalHttpsProxy"></param> /// <param name="externalHttpsProxy"></param>
/// <param name="clientStream"></param>
/// <returns></returns> /// <returns></returns>
internal async Task<TcpConnection> CreateClient(ProxyServer server, internal async Task<TcpConnection> CreateClient(ProxyServer server,
string remoteHostName, int remotePort, Version httpVersion, string remoteHostName, int remotePort, Version httpVersion,
bool isHttps, bool isHttps,
ExternalProxy externalHttpProxy, ExternalProxy externalHttpsProxy, ExternalProxy externalHttpProxy, ExternalProxy externalHttpsProxy)
Stream clientStream)
{ {
TcpClient client;
CustomBufferedStream stream;
bool useHttpProxy = false; bool useHttpProxy = false;
//check if external proxy is set for HTTP //check if external proxy is set for HTTP
if (!isHttps && externalHttpProxy != null if (!isHttps && externalHttpProxy != null
...@@ -71,10 +66,13 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -71,10 +66,13 @@ namespace Titanium.Web.Proxy.Network.Tcp
} }
} }
TcpClient client = null;
CustomBufferedStream stream = null;
try
{
if (isHttps) if (isHttps)
{ {
SslStream sslStream = null;
//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 (useHttpsProxy) if (useHttpsProxy)
{ {
...@@ -117,22 +115,10 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -117,22 +115,10 @@ namespace Titanium.Web.Proxy.Network.Tcp
stream = new CustomBufferedStream(client.GetStream(), server.BufferSize); stream = new CustomBufferedStream(client.GetStream(), server.BufferSize);
} }
try var sslStream = new SslStream(stream, false, server.ValidateServerCertificate, server.SelectClientCertificate);
{
sslStream = new SslStream(stream, true, server.ValidateServerCertificate,
server.SelectClientCertificate);
await sslStream.AuthenticateAsClientAsync(remoteHostName, null, server.SupportedSslProtocols, server.CheckCertificateRevocation);
stream = new CustomBufferedStream(sslStream, server.BufferSize); stream = new CustomBufferedStream(sslStream, server.BufferSize);
}
catch
{
sslStream?.Close();
sslStream?.Dispose();
throw; await sslStream.AuthenticateAsClientAsync(remoteHostName, null, server.SupportedSslProtocols, server.CheckCertificateRevocation);
}
} }
else else
{ {
...@@ -152,6 +138,13 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -152,6 +138,13 @@ namespace Titanium.Web.Proxy.Network.Tcp
client.ReceiveTimeout = server.ConnectionTimeOutSeconds * 1000; client.ReceiveTimeout = server.ConnectionTimeOutSeconds * 1000;
client.SendTimeout = server.ConnectionTimeOutSeconds * 1000; client.SendTimeout = server.ConnectionTimeOutSeconds * 1000;
}
catch (Exception)
{
stream?.Dispose();
client?.Close();
throw;
}
Interlocked.Increment(ref server.serverConnectionCount); Interlocked.Increment(ref server.serverConnectionCount);
......
...@@ -111,7 +111,7 @@ namespace Titanium.Web.Proxy ...@@ -111,7 +111,7 @@ namespace Titanium.Web.Proxy
try try
{ {
sslStream = new SslStream(clientStream, true); sslStream = new SslStream(clientStream);
var certificate = endPoint.GenericCertificate ?? var certificate = endPoint.GenericCertificate ??
CertificateManager.CreateCertificate(httpRemoteUri.Host, false); CertificateManager.CreateCertificate(httpRemoteUri.Host, false);
...@@ -191,7 +191,7 @@ namespace Titanium.Web.Proxy ...@@ -191,7 +191,7 @@ namespace Titanium.Web.Proxy
{ {
if (endPoint.EnableSsl) if (endPoint.EnableSsl)
{ {
var sslStream = new SslStream(clientStream, true); var sslStream = new SslStream(clientStream);
clientStream = new CustomBufferedStream(sslStream, BufferSize); clientStream = new CustomBufferedStream(sslStream, BufferSize);
//implement in future once SNI supported by SSL stream, for now use the same certificate //implement in future once SNI supported by SSL stream, for now use the same certificate
...@@ -542,8 +542,7 @@ namespace Titanium.Web.Proxy ...@@ -542,8 +542,7 @@ namespace Titanium.Web.Proxy
args.WebSession.Request.HttpVersion, args.WebSession.Request.HttpVersion,
args.IsHttps, args.IsHttps,
customUpStreamHttpProxy ?? UpStreamHttpProxy, customUpStreamHttpProxy ?? UpStreamHttpProxy,
customUpStreamHttpsProxy ?? UpStreamHttpsProxy, customUpStreamHttpsProxy ?? UpStreamHttpsProxy);
args.ProxyClient.ClientStream);
} }
......
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