Unverified Commit 0f910b1a authored by Jehonathan Thomas's avatar Jehonathan Thomas Committed by GitHub

Merge pull request #551 from jmh76/issue-550

Issue 550
parents 771ccfb4 3d73a6bd
......@@ -13,11 +13,11 @@ namespace Titanium.Web.Proxy.Exceptions
/// </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>
......@@ -26,6 +26,6 @@ namespace Titanium.Web.Proxy.Exceptions
/// <remarks>
/// This object properties should not be edited.
/// </remarks>
public TunnelConnectSessionEventArgs ConnectEventArgs { get; }
public SessionEventArgsBase Session { get; }
}
}
......@@ -13,11 +13,11 @@ namespace Titanium.Web.Proxy.Exceptions
/// </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>
......@@ -26,6 +26,6 @@ namespace Titanium.Web.Proxy.Exceptions
/// <remarks>
/// This object properties should not be edited.
/// </remarks>
public SessionEventArgs SessionEventArgs { get; }
public SessionEventArgs Session { get; }
}
}
......@@ -161,13 +161,13 @@ namespace Titanium.Web.Proxy
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
......@@ -197,9 +197,13 @@ namespace Titanium.Web.Proxy
}
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)
......
......@@ -4,6 +4,7 @@ using System.IO;
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;
......@@ -62,16 +63,17 @@ namespace Titanium.Web.Proxy
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
......@@ -81,9 +83,18 @@ namespace Titanium.Web.Proxy
}
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
......
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