Commit 5f65f4d7 authored by Honfika's avatar Honfika

small exception handling improvement

parent c3dd0f09
using System;
using Titanium.Web.Proxy.EventArguments;
namespace Titanium.Web.Proxy.Exceptions
{
/// <summary>
/// Proxy Connection exception.
/// </summary>
public class ProxyConnectException : ProxyException
{
/// <summary>
/// Instantiate new instance
/// </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(
message, innerException)
{
ConnectEventArgs = connectEventArgs;
}
/// <summary>
/// Gets session info associated to the exception.
/// </summary>
/// <remarks>
/// This object properties should not be edited.
/// </remarks>
public TunnelConnectSessionEventArgs ConnectEventArgs { get; }
}
}
......@@ -177,10 +177,9 @@ namespace Titanium.Web.Proxy
}
catch (Exception e)
{
ExceptionFunc(new Exception(
$"Could'nt authenticate client '{connectHostname}' with fake certificate.", e));
sslStream?.Dispose();
return;
throw new ProxyConnectException(
$"Could'nt authenticate client '{connectHostname}' with fake certificate.", e, connectArgs);
}
if (await HttpHelper.IsConnectMethod(clientStream) == -1)
......@@ -282,21 +281,21 @@ namespace Titanium.Web.Proxy
clientStreamWriter, cancellationTokenSource, connectHostname,
connectArgs?.WebSession.ConnectRequest);
}
catch (ProxyHttpException e)
catch (ProxyException e)
{
ExceptionFunc(e);
OnException(clientStream, e);
}
catch (IOException e)
{
ExceptionFunc(new Exception("Connection was aborted", e));
OnException(clientStream, new Exception("Connection was aborted", e));
}
catch (SocketException e)
{
ExceptionFunc(new Exception("Could not connect", e));
OnException(clientStream, new Exception("Could not connect", e));
}
catch (Exception e)
{
ExceptionFunc(new Exception("Error occured in whilst handling the client", e));
OnException(clientStream, new Exception("Error occured in whilst handling the client", e));
}
finally
{
......
......@@ -327,10 +327,7 @@ namespace Titanium.Web.Proxy.Network
{
if (RootCertificate == null)
{
exceptionFunc(
new Exception("Could not install certificate"
+ " as it is null or empty."));
exceptionFunc(new Exception("Could not install certificate as it is null or empty."));
return;
}
......@@ -368,10 +365,7 @@ namespace Titanium.Web.Proxy.Network
{
if (certificate == null)
{
exceptionFunc(
new Exception("Could not remove certificate"
+ " as it is null or empty."));
exceptionFunc(new Exception("Could not remove certificate as it is null or empty."));
return;
}
......
......@@ -7,6 +7,7 @@ using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using StreamExtended.Network;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Helpers.WinHttp;
......@@ -663,6 +664,18 @@ namespace Titanium.Web.Proxy
}
}
private void OnException(CustomBufferedStream clientStream, Exception exception)
{
#if DEBUG
if (clientStream is DebugCustomBufferedStream debugStream)
{
debugStream.LogException(exception);
}
#endif
ExceptionFunc(exception);
}
/// <summary>
/// Quit listening on the given end point.
/// </summary>
......
using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
......@@ -8,6 +9,7 @@ using StreamExtended;
using StreamExtended.Helpers;
using StreamExtended.Network;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Models;
......@@ -77,10 +79,9 @@ namespace Titanium.Web.Proxy
}
catch (Exception e)
{
ExceptionFunc(new Exception(
$"Could'nt authenticate client '{httpsHostName}' with fake certificate.", e));
sslStream?.Dispose();
return;
throw new ProxyConnectException(
$"Could'nt authenticate client '{httpsHostName}' with fake certificate.", e, null);
}
}
else
......@@ -127,6 +128,22 @@ namespace Titanium.Web.Proxy
await HandleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter,
cancellationTokenSource, isHttps ? httpsHostName : null, null, true);
}
catch (ProxyException e)
{
OnException(clientStream, e);
}
catch (IOException e)
{
OnException(clientStream, new Exception("Connection was aborted", e));
}
catch (SocketException e)
{
OnException(clientStream, new Exception("Could not connect", e));
}
catch (Exception e)
{
OnException(clientStream, new Exception("Error occured in whilst handling the client", e));
}
finally
{
clientStream.Dispose();
......
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