Commit bb6be9d4 authored by Honfika's avatar Honfika

small exception handling improvement

parent 59e3062d
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 ...@@ -177,10 +177,9 @@ namespace Titanium.Web.Proxy
} }
catch (Exception e) catch (Exception e)
{ {
ExceptionFunc(new Exception(
$"Could'nt authenticate client '{connectHostname}' with fake certificate.", e));
sslStream?.Dispose(); sslStream?.Dispose();
return; throw new ProxyConnectException(
$"Could'nt authenticate client '{connectHostname}' with fake certificate.", e, connectArgs);
} }
if (await HttpHelper.IsConnectMethod(clientStream) == -1) if (await HttpHelper.IsConnectMethod(clientStream) == -1)
...@@ -282,21 +281,21 @@ namespace Titanium.Web.Proxy ...@@ -282,21 +281,21 @@ namespace Titanium.Web.Proxy
clientStreamWriter, cancellationTokenSource, connectHostname, clientStreamWriter, cancellationTokenSource, connectHostname,
connectArgs?.WebSession.ConnectRequest); connectArgs?.WebSession.ConnectRequest);
} }
catch (ProxyHttpException e) catch (ProxyException e)
{ {
ExceptionFunc(e); OnException(clientStream, e);
} }
catch (IOException e) catch (IOException e)
{ {
ExceptionFunc(new Exception("Connection was aborted", e)); OnException(clientStream, new Exception("Connection was aborted", e));
} }
catch (SocketException e) catch (SocketException e)
{ {
ExceptionFunc(new Exception("Could not connect", e)); OnException(clientStream, new Exception("Could not connect", e));
} }
catch (Exception 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 finally
{ {
......
...@@ -327,10 +327,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -327,10 +327,7 @@ namespace Titanium.Web.Proxy.Network
{ {
if (RootCertificate == null) if (RootCertificate == null)
{ {
exceptionFunc( exceptionFunc(new Exception("Could not install certificate as it is null or empty."));
new Exception("Could not install certificate"
+ " as it is null or empty."));
return; return;
} }
...@@ -368,10 +365,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -368,10 +365,7 @@ namespace Titanium.Web.Proxy.Network
{ {
if (certificate == null) if (certificate == null)
{ {
exceptionFunc( exceptionFunc(new Exception("Could not remove certificate as it is null or empty."));
new Exception("Could not remove certificate"
+ " as it is null or empty."));
return; return;
} }
......
...@@ -7,6 +7,7 @@ using System.Security.Authentication; ...@@ -7,6 +7,7 @@ using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using StreamExtended.Network;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Helpers.WinHttp; using Titanium.Web.Proxy.Helpers.WinHttp;
...@@ -663,6 +664,18 @@ namespace Titanium.Web.Proxy ...@@ -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> /// <summary>
/// Quit listening on the given end point. /// Quit listening on the given end point.
/// </summary> /// </summary>
......
using System; using System;
using System.IO;
using System.Net.Security; using System.Net.Security;
using System.Net.Sockets; using System.Net.Sockets;
using System.Security.Authentication; using System.Security.Authentication;
...@@ -8,6 +9,7 @@ using StreamExtended; ...@@ -8,6 +9,7 @@ using StreamExtended;
using StreamExtended.Helpers; using StreamExtended.Helpers;
using StreamExtended.Network; using StreamExtended.Network;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Extensions; using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
...@@ -77,10 +79,9 @@ namespace Titanium.Web.Proxy ...@@ -77,10 +79,9 @@ namespace Titanium.Web.Proxy
} }
catch (Exception e) catch (Exception e)
{ {
ExceptionFunc(new Exception(
$"Could'nt authenticate client '{httpsHostName}' with fake certificate.", e));
sslStream?.Dispose(); sslStream?.Dispose();
return; throw new ProxyConnectException(
$"Could'nt authenticate client '{httpsHostName}' with fake certificate.", e, null);
} }
} }
else else
...@@ -127,6 +128,22 @@ namespace Titanium.Web.Proxy ...@@ -127,6 +128,22 @@ namespace Titanium.Web.Proxy
await HandleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter, await HandleHttpSessionRequest(endPoint, clientConnection, clientStream, clientStreamWriter,
cancellationTokenSource, isHttps ? httpsHostName : null, null, true); 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 finally
{ {
clientStream.Dispose(); 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