Commit 92fa402d authored by justcoding121's avatar justcoding121

move sslstream dispose to end to be safe

parent 0f910b1a
...@@ -41,11 +41,13 @@ namespace Titanium.Web.Proxy ...@@ -41,11 +41,13 @@ namespace Titanium.Web.Proxy
bool closeServerConnection = false; bool closeServerConnection = false;
bool calledRequestHandler = false; bool calledRequestHandler = false;
SslStream sslStream = null;
try try
{ {
string connectHostname = null; string connectHostname = null;
TunnelConnectSessionEventArgs connectArgs = null; TunnelConnectSessionEventArgs connectArgs = null;
// Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request) // Client wants to create a secure tcp tunnel (probably its a HTTPS or Websocket request)
if (await HttpHelper.IsConnectMethod(clientStream) == 1) if (await HttpHelper.IsConnectMethod(clientStream) == 1)
...@@ -150,8 +152,6 @@ namespace Titanium.Web.Proxy ...@@ -150,8 +152,6 @@ namespace Titanium.Web.Proxy
await tcpConnectionFactory.Release(connection, true); await tcpConnectionFactory.Release(connection, true);
} }
SslStream sslStream = null;
if (EnableTcpServerConnectionPrefetch) if (EnableTcpServerConnectionPrefetch)
{ {
//don't pass cancellation token here //don't pass cancellation token here
...@@ -201,10 +201,6 @@ namespace Titanium.Web.Proxy ...@@ -201,10 +201,6 @@ namespace Titanium.Web.Proxy
throw new ProxyConnectException( throw new ProxyConnectException(
$"Couldn't authenticate host '{connectHostname}' with certificate '{certname}'.", e, connectArgs); $"Couldn't authenticate host '{connectHostname}' with certificate '{certname}'.", e, connectArgs);
} }
finally
{
sslStream?.Dispose();
}
if (await HttpHelper.IsConnectMethod(clientStream) == -1) if (await HttpHelper.IsConnectMethod(clientStream) == -1)
{ {
...@@ -354,6 +350,7 @@ namespace Titanium.Web.Proxy ...@@ -354,6 +350,7 @@ namespace Titanium.Web.Proxy
await tcpConnectionFactory.Release(prefetchConnectionTask, closeServerConnection); await tcpConnectionFactory.Release(prefetchConnectionTask, closeServerConnection);
} }
sslStream?.Dispose();
clientStream.Dispose(); clientStream.Dispose();
if (!cancellationTokenSource.IsCancellationRequested) if (!cancellationTokenSource.IsCancellationRequested)
......
...@@ -35,6 +35,8 @@ namespace Titanium.Web.Proxy ...@@ -35,6 +35,8 @@ namespace Titanium.Web.Proxy
var clientStream = new CustomBufferedStream(clientConnection.GetStream(), BufferPool, BufferSize); var clientStream = new CustomBufferedStream(clientConnection.GetStream(), BufferPool, BufferSize);
var clientStreamWriter = new HttpResponseWriter(clientStream, BufferPool, BufferSize); var clientStreamWriter = new HttpResponseWriter(clientStream, BufferPool, BufferSize);
SslStream sslStream = null;
try try
{ {
var clientHelloInfo = await SslTools.PeekClientHello(clientStream, BufferPool, cancellationToken); var clientHelloInfo = await SslTools.PeekClientHello(clientStream, BufferPool, cancellationToken);
...@@ -61,8 +63,6 @@ namespace Titanium.Web.Proxy ...@@ -61,8 +63,6 @@ namespace Titanium.Web.Proxy
if (endPoint.DecryptSsl && args.DecryptSsl) if (endPoint.DecryptSsl && args.DecryptSsl)
{ {
SslStream sslStream = null;
//do client authentication using certificate //do client authentication using certificate
X509Certificate2 certificate = null; X509Certificate2 certificate = null;
try try
...@@ -92,10 +92,7 @@ namespace Titanium.Web.Proxy ...@@ -92,10 +92,7 @@ namespace Titanium.Web.Proxy
throw new ProxyConnectException( throw new ProxyConnectException(
$"Couldn't authenticate host '{httpsHostName}' with certificate '{certname}'.", e, session); $"Couldn't authenticate host '{httpsHostName}' with certificate '{certname}'.", e, session);
} }
finally
{
sslStream?.Dispose();
}
} }
else else
{ {
...@@ -161,8 +158,9 @@ namespace Titanium.Web.Proxy ...@@ -161,8 +158,9 @@ namespace Titanium.Web.Proxy
} }
finally finally
{ {
sslStream?.Dispose();
clientStream.Dispose(); clientStream.Dispose();
if (!cancellationTokenSource.IsCancellationRequested) if (!cancellationTokenSource.IsCancellationRequested)
{ {
cancellationTokenSource.Cancel(); cancellationTokenSource.Cancel();
......
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