Commit e1cf60d6 authored by justcoding121's avatar justcoding121

#87 Fix abrupt termination of SSL connections

parent 44ba131c
...@@ -65,19 +65,18 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -65,19 +65,18 @@ namespace Titanium.Web.Proxy.Helpers
sb.Append(Environment.NewLine); sb.Append(Environment.NewLine);
} }
var tcpConnection = await tcpConnectionFactory.CreateClient(bufferSize, connectionTimeOutSeconds, var tcpConnection = await tcpConnectionFactory.CreateClient(bufferSize, connectionTimeOutSeconds,
remoteHostName, remotePort, remoteHostName, remotePort,
httpVersion, isHttps, httpVersion, isHttps,
supportedProtocols, remoteCertificateValidationCallback, localCertificateSelectionCallback, supportedProtocols, remoteCertificateValidationCallback, localCertificateSelectionCallback,
null, null, clientStream); null, null, clientStream);
TcpClient tunnelClient = tcpConnection.TcpClient;
Stream tunnelStream = tcpConnection.Stream;
try try
{ {
TcpClient tunnelClient = tcpConnection.TcpClient;
Stream tunnelStream = tcpConnection.Stream;
Task sendRelay; Task sendRelay;
//Now async relay all server=>client & client=>server data //Now async relay all server=>client & client=>server data
...@@ -101,7 +100,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -101,7 +100,7 @@ namespace Titanium.Web.Proxy.Helpers
} }
finally finally
{ {
tunnelStream.Dispose(); tcpConnection.Dispose();
} }
} }
......
...@@ -44,11 +44,15 @@ namespace Titanium.Web.Proxy.Network ...@@ -44,11 +44,15 @@ namespace Titanium.Web.Proxy.Network
public void Dispose() public void Dispose()
{ {
Stream.Dispose();
TcpClient.LingerState = new LingerOption(true, 0);
TcpClient.Client.Shutdown(SocketShutdown.Both); TcpClient.Client.Shutdown(SocketShutdown.Both);
TcpClient.Client.Disconnect(true); TcpClient.Client.Close();
TcpClient.Client.Dispose(); TcpClient.Client.Dispose();
Stream.Dispose(); TcpClient.Close();
} }
} }
} }
...@@ -120,6 +120,8 @@ namespace Titanium.Web.Proxy.Network ...@@ -120,6 +120,8 @@ namespace Titanium.Web.Proxy.Network
stream.ReadTimeout = connectionTimeOutSeconds * 1000; stream.ReadTimeout = connectionTimeOutSeconds * 1000;
stream.WriteTimeout = connectionTimeOutSeconds * 1000; stream.WriteTimeout = connectionTimeOutSeconds * 1000;
client.NoDelay = true;
return new TcpConnection() return new TcpConnection()
{ {
HostName = remoteHostName, HostName = remoteHostName,
......
...@@ -381,10 +381,14 @@ namespace Titanium.Web.Proxy ...@@ -381,10 +381,14 @@ namespace Titanium.Web.Proxy
if (tcpClient != null) if (tcpClient != null)
{ {
Task.Run(async () => Task.Run(async () =>
{ {
try try
{ {
tcpClient.NoDelay = true;
if (endPoint.GetType() == typeof(TransparentProxyEndPoint)) if (endPoint.GetType() == typeof(TransparentProxyEndPoint))
{ {
await HandleClient(endPoint as TransparentProxyEndPoint, tcpClient); await HandleClient(endPoint as TransparentProxyEndPoint, tcpClient);
...@@ -393,16 +397,18 @@ namespace Titanium.Web.Proxy ...@@ -393,16 +397,18 @@ namespace Titanium.Web.Proxy
{ {
await HandleClient(endPoint as ExplicitProxyEndPoint, tcpClient); await HandleClient(endPoint as ExplicitProxyEndPoint, tcpClient);
} }
} }
finally finally
{ {
if (tcpClient != null) if (tcpClient != null)
{ {
tcpClient.LingerState = new LingerOption(true, 0);
tcpClient.Client.Shutdown(SocketShutdown.Both); tcpClient.Client.Shutdown(SocketShutdown.Both);
tcpClient.Client.Disconnect(true); tcpClient.Client.Close();
tcpClient.Client.Dispose(); tcpClient.Client.Dispose();
tcpClient.Close();
} }
} }
}); });
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net.Sockets;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
using Titanium.Web.Proxy.Compression; using Titanium.Web.Proxy.Compression;
......
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