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

Issue 550 #551

Merged
merged 3 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Titanium.Web.Proxy/Exceptions/ProxyConnectException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public class ProxyConnectException : ProxyException
/// </summary>
/// <param name="message">Message for this exception</param>
/// <param name="innerException">Associated inner exception</param>
/// <param name="connectEventArgs">Instance of <see cref="EventArguments.TunnelConnectSessionEventArgs" /> associated to the exception</param>
internal ProxyConnectException(string message, Exception innerException, TunnelConnectSessionEventArgs connectEventArgs) : base(
/// <param name="session">Instance of <see cref="EventArguments.TunnelConnectSessionEventArgs" /> associated to the exception</param>
internal ProxyConnectException(string message, Exception innerException, SessionEventArgsBase session) : base(
message, innerException)
{
ConnectEventArgs = connectEventArgs;
Session = session;
}

/// <summary>
Expand All @@ -26,6 +26,6 @@ internal ProxyConnectException(string message, Exception innerException, TunnelC
/// <remarks>
/// This object properties should not be edited.
/// </remarks>
public TunnelConnectSessionEventArgs ConnectEventArgs { get; }
public SessionEventArgsBase Session { get; }
}
}
8 changes: 4 additions & 4 deletions src/Titanium.Web.Proxy/Exceptions/ProxyHttpException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public class ProxyHttpException : ProxyException
/// </summary>
/// <param name="message">Message for this exception</param>
/// <param name="innerException">Associated inner exception</param>
/// <param name="sessionEventArgs">Instance of <see cref="EventArguments.SessionEventArgs" /> associated to the exception</param>
internal ProxyHttpException(string message, Exception innerException, SessionEventArgs sessionEventArgs) : base(
/// <param name="session">Instance of <see cref="EventArguments.SessionEventArgs" /> associated to the exception</param>
internal ProxyHttpException(string message, Exception innerException, SessionEventArgs session) : base(
message, innerException)
{
SessionEventArgs = sessionEventArgs;
Session = session;
}

/// <summary>
Expand All @@ -26,6 +26,6 @@ internal ProxyHttpException(string message, Exception innerException, SessionEve
/// <remarks>
/// This object properties should not be edited.
/// </remarks>
public SessionEventArgs SessionEventArgs { get; }
public SessionEventArgs Session { get; }
}
}
14 changes: 9 additions & 5 deletions src/Titanium.Web.Proxy/ExplicitClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
cancellationToken: CancellationToken.None);
}

X509Certificate2 certificate = null;
try
{
sslStream = new SslStream(clientStream);
sslStream = new SslStream(clientStream, true);

string certName = HttpHelper.GetWildCardDomainName(connectHostname);

var certificate = endPoint.GenericCertificate ??
certificate = endPoint.GenericCertificate ??
await CertificateManager.CreateServerCertificate(certName);

// Successfully managed to authenticate the client using the fake certificate
Expand Down Expand Up @@ -197,9 +197,13 @@ await clientStreamWriter.WriteResponseAsync(connectArgs.HttpClient.Response,
}
catch (Exception e)
{
sslStream?.Dispose();
var certname = certificate?.GetNameInfo(X509NameType.SimpleName, false);
throw new ProxyConnectException(
$"Could'nt authenticate client '{connectHostname}' with fake certificate.", e, connectArgs);
$"Couldn't authenticate host '{connectHostname}' with certificate '{certname}'.", e, connectArgs);
}
finally
{
sslStream?.Dispose();
}

if (await HttpHelper.IsConnectMethod(clientStream) == -1)
Expand Down
23 changes: 17 additions & 6 deletions src/Titanium.Web.Proxy/TransparentClientHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using StreamExtended;
Expand Down Expand Up @@ -62,16 +63,17 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn

SslStream sslStream = null;

//do client authentication using fake certificate
//do client authentication using certificate
X509Certificate2 certificate = null;
try
{
sslStream = new SslStream(clientStream);
sslStream = new SslStream(clientStream, true);

string certName = HttpHelper.GetWildCardDomainName(httpsHostName);
var certificate = endPoint.GenericCertificate ??
certificate = endPoint.GenericCertificate ??
await CertificateManager.CreateServerCertificate(certName);

// Successfully managed to authenticate the client using the fake certificate
// Successfully managed to authenticate the client using the certificate
await sslStream.AuthenticateAsServerAsync(certificate, false, SslProtocols.Tls, false);

// HTTPS server created - we can now decrypt the client's traffic
Expand All @@ -81,9 +83,18 @@ private async Task handleClient(TransparentProxyEndPoint endPoint, TcpClientConn
}
catch (Exception e)
{
sslStream?.Dispose();
var certname = certificate?.GetNameInfo(X509NameType.SimpleName, false);
var session = new SessionEventArgs(this, endPoint, cancellationTokenSource)
{
ProxyClient = { Connection = clientConnection },
HttpClient = { ConnectRequest = null }
};
throw new ProxyConnectException(
$"Could'nt authenticate client '{httpsHostName}' with fake certificate.", e, null);
$"Couldn't authenticate host '{httpsHostName}' with certificate '{certname}'.", e, session);
}
finally
{
sslStream?.Dispose();
}
}
else
Expand Down