Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 3d73a6b

Browse files
committed
#550 sslStream not disposed; improve ProxyConnectException
1 parent 9570acb commit 3d73a6b

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

src/Titanium.Web.Proxy/Exceptions/ProxyConnectException.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public class ProxyConnectException : ProxyException
1313
/// </summary>
1414
/// <param name="message">Message for this exception</param>
1515
/// <param name="innerException">Associated inner exception</param>
16-
/// <param name="connectEventArgs">Instance of <see cref="EventArguments.TunnelConnectSessionEventArgs" /> associated to the exception</param>
17-
internal ProxyConnectException(string message, Exception innerException, TunnelConnectSessionEventArgs connectEventArgs) : base(
16+
/// <param name="session">Instance of <see cref="EventArguments.TunnelConnectSessionEventArgs" /> associated to the exception</param>
17+
internal ProxyConnectException(string message, Exception innerException, SessionEventArgsBase session) : base(
1818
message, innerException)
1919
{
20-
ConnectEventArgs = connectEventArgs;
20+
Session = session;
2121
}
2222

2323
/// <summary>
@@ -26,6 +26,6 @@ internal ProxyConnectException(string message, Exception innerException, TunnelC
2626
/// <remarks>
2727
/// This object properties should not be edited.
2828
/// </remarks>
29-
public TunnelConnectSessionEventArgs ConnectEventArgs { get; }
29+
public SessionEventArgsBase Session { get; }
3030
}
3131
}

src/Titanium.Web.Proxy/Exceptions/ProxyHttpException.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public class ProxyHttpException : ProxyException
1313
/// </summary>
1414
/// <param name="message">Message for this exception</param>
1515
/// <param name="innerException">Associated inner exception</param>
16-
/// <param name="sessionEventArgs">Instance of <see cref="EventArguments.SessionEventArgs" /> associated to the exception</param>
17-
internal ProxyHttpException(string message, Exception innerException, SessionEventArgs sessionEventArgs) : base(
16+
/// <param name="session">Instance of <see cref="EventArguments.SessionEventArgs" /> associated to the exception</param>
17+
internal ProxyHttpException(string message, Exception innerException, SessionEventArgs session) : base(
1818
message, innerException)
1919
{
20-
SessionEventArgs = sessionEventArgs;
20+
Session = session;
2121
}
2222

2323
/// <summary>
@@ -26,6 +26,6 @@ internal ProxyHttpException(string message, Exception innerException, SessionEve
2626
/// <remarks>
2727
/// This object properties should not be edited.
2828
/// </remarks>
29-
public SessionEventArgs SessionEventArgs { get; }
29+
public SessionEventArgs Session { get; }
3030
}
3131
}

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
161161
cancellationToken: CancellationToken.None);
162162
}
163163

164+
X509Certificate2 certificate = null;
164165
try
165166
{
166-
sslStream = new SslStream(clientStream);
167+
sslStream = new SslStream(clientStream, true);
167168

168169
string certName = HttpHelper.GetWildCardDomainName(connectHostname);
169-
170-
var certificate = endPoint.GenericCertificate ??
170+
certificate = endPoint.GenericCertificate ??
171171
await CertificateManager.CreateServerCertificate(certName);
172172

173173
// Successfully managed to authenticate the client using the fake certificate
@@ -197,9 +197,13 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
197197
}
198198
catch (Exception e)
199199
{
200-
sslStream?.Dispose();
200+
var certname = certificate?.GetNameInfo(X509NameType.SimpleName, false);
201201
throw new ProxyConnectException(
202-
$"Could'nt authenticate client '{connectHostname}' with fake certificate.", e, connectArgs);
202+
$"Couldn't authenticate host '{connectHostname}' with certificate '{certname}'.", e, connectArgs);
203+
}
204+
finally
205+
{
206+
sslStream?.Dispose();
203207
}
204208

205209
if (await HttpHelper.IsConnectMethod(clientStream) == -1)

src/Titanium.Web.Proxy/TransparentClientHandler.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net.Security;
55
using System.Net.Sockets;
66
using System.Security.Authentication;
7+
using System.Security.Cryptography.X509Certificates;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using StreamExtended;
@@ -62,16 +63,17 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn
6263

6364
SslStream sslStream = null;
6465

65-
//do client authentication using fake certificate
66+
//do client authentication using certificate
67+
X509Certificate2 certificate = null;
6668
try
6769
{
68-
sslStream = new SslStream(clientStream);
70+
sslStream = new SslStream(clientStream, true);
6971

7072
string certName = HttpHelper.GetWildCardDomainName(httpsHostName);
71-
var certificate = endPoint.GenericCertificate ??
73+
certificate = endPoint.GenericCertificate ??
7274
await CertificateManager.CreateServerCertificate(certName);
7375

74-
// Successfully managed to authenticate the client using the fake certificate
76+
// Successfully managed to authenticate the client using the certificate
7577
await sslStream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls, false);
7678

7779
// HTTPS server created - we can now decrypt the client's traffic
@@ -81,9 +83,18 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn
8183
}
8284
catch (Exception e)
8385
{
84-
sslStream?.Dispose();
86+
var certname = certificate?.GetNameInfo(X509NameType.SimpleName, false);
87+
var session = new SessionEventArgs(this, endPoint, cancellationTokenSource)
88+
{
89+
ProxyClient = { Connection = clientConnection },
90+
HttpClient = { ConnectRequest = null }
91+
};
8592
throw new ProxyConnectException(
86-
$"Could'nt authenticate client '{httpsHostName}' with fake certificate.", e, null);
93+
$"Couldn't authenticate host '{httpsHostName}' with certificate '{certname}'.", e, session);
94+
}
95+
finally
96+
{
97+
sslStream?.Dispose();
8798
}
8899
}
89100
else

0 commit comments

Comments
 (0)