Commit a5b2368d authored by Honfika's avatar Honfika

Add SessionEventArgs to ProxyAuthorizationException, own delegate tpye for the exception func.

parent 0ea53fab
...@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.UnitTests ...@@ -19,7 +19,7 @@ namespace Titanium.Web.Proxy.UnitTests
{ {
var tasks = new List<Task>(); var tasks = new List<Task>();
var mgr = new CertificateManager(null, null, false, false, false, new Lazy<Action<Exception>>(() => (e => var mgr = new CertificateManager(null, null, false, false, false, new Lazy<ExceptionHandler>(() => (e =>
{ {
//Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty); //Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty);
})).Value); })).Value);
...@@ -48,7 +48,7 @@ namespace Titanium.Web.Proxy.UnitTests ...@@ -48,7 +48,7 @@ namespace Titanium.Web.Proxy.UnitTests
{ {
var tasks = new List<Task>(); var tasks = new List<Task>();
var mgr = new CertificateManager(null, null, false, false, false, new Lazy<Action<Exception>>(() => (e => var mgr = new CertificateManager(null, null, false, false, false, new Lazy<ExceptionHandler>(() => (e =>
{ {
//Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty); //Console.WriteLine(e.ToString() + e.InnerException != null ? e.InnerException.ToString() : string.Empty);
})).Value); })).Value);
......
...@@ -29,7 +29,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -29,7 +29,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
private readonly int bufferSize; private readonly int bufferSize;
private readonly Action<Exception> exceptionFunc; private readonly ExceptionHandler exceptionFunc;
/// <summary> /// <summary>
/// Backing field for corresponding public property /// Backing field for corresponding public property
...@@ -105,7 +105,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -105,7 +105,7 @@ namespace Titanium.Web.Proxy.EventArguments
/// </summary> /// </summary>
internal SessionEventArgs(int bufferSize, internal SessionEventArgs(int bufferSize,
ProxyEndPoint endPoint, ProxyEndPoint endPoint,
Action<Exception> exceptionFunc) ExceptionHandler exceptionFunc)
{ {
this.bufferSize = bufferSize; this.bufferSize = bufferSize;
this.exceptionFunc = exceptionFunc; this.exceptionFunc = exceptionFunc;
......
...@@ -10,7 +10,7 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -10,7 +10,7 @@ namespace Titanium.Web.Proxy.EventArguments
public bool IsHttpsConnect { get; internal set; } public bool IsHttpsConnect { get; internal set; }
internal TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ConnectRequest connectRequest, Action<Exception> exceptionFunc) internal TunnelConnectSessionEventArgs(int bufferSize, ProxyEndPoint endPoint, ConnectRequest connectRequest, ExceptionHandler exceptionFunc)
: base(bufferSize, endPoint, exceptionFunc) : base(bufferSize, endPoint, exceptionFunc)
{ {
WebSession.Request = connectRequest; WebSession.Request = connectRequest;
......
using System;
namespace Titanium.Web.Proxy
{
public delegate void ExceptionHandler(Exception exception);
}
\ No newline at end of file
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
/// Constructor. /// Constructor.
/// </summary> /// </summary>
/// <param name="message"></param> /// <param name="message"></param>
public BodyNotFoundException(string message) : base(message) internal BodyNotFoundException(string message) : base(message)
{ {
} }
} }
......
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Exceptions namespace Titanium.Web.Proxy.Exceptions
...@@ -13,13 +14,17 @@ namespace Titanium.Web.Proxy.Exceptions ...@@ -13,13 +14,17 @@ namespace Titanium.Web.Proxy.Exceptions
/// Instantiate new instance /// Instantiate new instance
/// </summary> /// </summary>
/// <param name="message">Exception message</param> /// <param name="message">Exception message</param>
/// <param name="session">The <see cref="SessionEventArgs"/> instance containing the event data.</param>
/// <param name="innerException">Inner exception associated to upstream proxy authorization</param> /// <param name="innerException">Inner exception associated to upstream proxy authorization</param>
/// <param name="headers">Http's headers associated</param> /// <param name="headers">Http's headers associated</param>
public ProxyAuthorizationException(string message, Exception innerException, IEnumerable<HttpHeader> headers) : base(message, innerException) internal ProxyAuthorizationException(string message, SessionEventArgs session, Exception innerException, IEnumerable<HttpHeader> headers) : base(message, innerException)
{ {
Session = session;
Headers = headers; Headers = headers;
} }
public SessionEventArgs Session { get; }
/// <summary> /// <summary>
/// Headers associated with the authorization exception /// Headers associated with the authorization exception
/// </summary> /// </summary>
......
...@@ -14,7 +14,7 @@ namespace Titanium.Web.Proxy.Exceptions ...@@ -14,7 +14,7 @@ namespace Titanium.Web.Proxy.Exceptions
/// <param name="message">Message for this exception</param> /// <param name="message">Message for this exception</param>
/// <param name="innerException">Associated inner exception</param> /// <param name="innerException">Associated inner exception</param>
/// <param name="sessionEventArgs">Instance of <see cref="EventArguments.SessionEventArgs"/> associated to the exception</param> /// <param name="sessionEventArgs">Instance of <see cref="EventArguments.SessionEventArgs"/> associated to the exception</param>
public ProxyHttpException(string message, Exception innerException, SessionEventArgs sessionEventArgs) : base(message, innerException) internal ProxyHttpException(string message, Exception innerException, SessionEventArgs sessionEventArgs) : base(message, innerException)
{ {
SessionEventArgs = sessionEventArgs; SessionEventArgs = sessionEventArgs;
} }
......
...@@ -7,7 +7,7 @@ namespace Titanium.Web.Proxy.Extensions ...@@ -7,7 +7,7 @@ namespace Titanium.Web.Proxy.Extensions
internal static class FuncExtensions internal static class FuncExtensions
{ {
public static async Task InvokeAsync<T>(this AsyncEventHandler<T> callback, object sender, T args, Action<Exception> exceptionFunc) public static async Task InvokeAsync<T>(this AsyncEventHandler<T> callback, object sender, T args, ExceptionHandler exceptionFunc)
{ {
var invocationList = callback.GetInvocationList(); var invocationList = callback.GetInvocationList();
...@@ -17,7 +17,7 @@ namespace Titanium.Web.Proxy.Extensions ...@@ -17,7 +17,7 @@ namespace Titanium.Web.Proxy.Extensions
} }
} }
private static async Task InternalInvokeAsync<T>(AsyncEventHandler<T> callback, object sender, T args, Action<Exception> exceptionFunc) private static async Task InternalInvokeAsync<T>(AsyncEventHandler<T> callback, object sender, T args, ExceptionHandler exceptionFunc)
{ {
try try
{ {
......
...@@ -122,7 +122,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -122,7 +122,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="exceptionFunc"></param> /// <param name="exceptionFunc"></param>
/// <returns></returns> /// <returns></returns>
internal static async Task SendRawApm(Stream clientStream, Stream serverStream, int bufferSize, internal static async Task SendRawApm(Stream clientStream, Stream serverStream, int bufferSize,
Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive, Action<Exception> exceptionFunc) Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive, ExceptionHandler exceptionFunc)
{ {
var tcs = new TaskCompletionSource<bool>(); var tcs = new TaskCompletionSource<bool>();
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
...@@ -145,7 +145,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -145,7 +145,7 @@ namespace Titanium.Web.Proxy.Helpers
} }
private static void BeginRead(Stream inputStream, Stream outputStream, byte[] buffer, CancellationTokenSource cts, Action<byte[], int, int> onCopy, private static void BeginRead(Stream inputStream, Stream outputStream, byte[] buffer, CancellationTokenSource cts, Action<byte[], int, int> onCopy,
Action<Exception> exceptionFunc) ExceptionHandler exceptionFunc)
{ {
if (cts.IsCancellationRequested) if (cts.IsCancellationRequested)
{ {
...@@ -221,7 +221,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -221,7 +221,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="exceptionFunc"></param> /// <param name="exceptionFunc"></param>
/// <returns></returns> /// <returns></returns>
internal static async Task SendRawTap(Stream clientStream, Stream serverStream, int bufferSize, internal static async Task SendRawTap(Stream clientStream, Stream serverStream, int bufferSize,
Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive, Action<Exception> exceptionFunc) Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive, ExceptionHandler exceptionFunc)
{ {
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
...@@ -247,7 +247,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -247,7 +247,7 @@ namespace Titanium.Web.Proxy.Helpers
/// <param name="exceptionFunc"></param> /// <param name="exceptionFunc"></param>
/// <returns></returns> /// <returns></returns>
internal static Task SendRaw(Stream clientStream, Stream serverStream, int bufferSize, internal static Task SendRaw(Stream clientStream, Stream serverStream, int bufferSize,
Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive, Action<Exception> exceptionFunc) Action<byte[], int, int> onDataSend, Action<byte[], int, int> onDataReceive, ExceptionHandler exceptionFunc)
{ {
#if NET45 #if NET45
return SendRawApm(clientStream, serverStream, bufferSize, onDataSend, onDataReceive, exceptionFunc); return SendRawApm(clientStream, serverStream, bufferSize, onDataSend, onDataReceive, exceptionFunc);
......
...@@ -45,7 +45,7 @@ namespace Titanium.Web.Proxy.Models ...@@ -45,7 +45,7 @@ namespace Titanium.Web.Proxy.Models
{ {
} }
internal async Task InvokeBeforeTunnelConnectRequest(ProxyServer proxyServer, TunnelConnectSessionEventArgs connectArgs, Action<Exception> exceptionFunc) internal async Task InvokeBeforeTunnelConnectRequest(ProxyServer proxyServer, TunnelConnectSessionEventArgs connectArgs, ExceptionHandler exceptionFunc)
{ {
if (BeforeTunnelConnectRequest != null) if (BeforeTunnelConnectRequest != null)
{ {
...@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy.Models ...@@ -53,7 +53,7 @@ namespace Titanium.Web.Proxy.Models
} }
} }
internal async Task InvokeBeforeTunnectConnectResponse(ProxyServer proxyServer, TunnelConnectSessionEventArgs connectArgs, Action<Exception> exceptionFunc, bool isClientHello = false) internal async Task InvokeBeforeTunnectConnectResponse(ProxyServer proxyServer, TunnelConnectSessionEventArgs connectArgs, ExceptionHandler exceptionFunc, bool isClientHello = false)
{ {
if (BeforeTunnelConnectResponse != null) if (BeforeTunnelConnectResponse != null)
{ {
......
...@@ -33,9 +33,9 @@ namespace Titanium.Web.Proxy.Network.Certificate ...@@ -33,9 +33,9 @@ namespace Titanium.Web.Proxy.Network.Certificate
// Set this flag to true when exception detected to avoid further exceptions // Set this flag to true when exception detected to avoid further exceptions
private static bool doNotSetFriendlyName; private static bool doNotSetFriendlyName;
private readonly Action<Exception> exceptionFunc; private readonly ExceptionHandler exceptionFunc;
internal BCCertificateMaker(Action<Exception> exceptionFunc) internal BCCertificateMaker(ExceptionHandler exceptionFunc)
{ {
this.exceptionFunc = exceptionFunc; this.exceptionFunc = exceptionFunc;
} }
......
...@@ -43,12 +43,12 @@ namespace Titanium.Web.Proxy.Network.Certificate ...@@ -43,12 +43,12 @@ namespace Titanium.Web.Proxy.Network.Certificate
private object sharedPrivateKey; private object sharedPrivateKey;
private readonly Action<Exception> exceptionFunc; private readonly ExceptionHandler exceptionFunc;
/// <summary> /// <summary>
/// Constructor. /// Constructor.
/// </summary> /// </summary>
internal WinCertificateMaker(Action<Exception> exceptionFunc) internal WinCertificateMaker(ExceptionHandler exceptionFunc)
{ {
this.exceptionFunc = exceptionFunc; this.exceptionFunc = exceptionFunc;
typeX500DN = Type.GetTypeFromProgID("X509Enrollment.CX500DistinguishedName", true); typeX500DN = Type.GetTypeFromProgID("X509Enrollment.CX500DistinguishedName", true);
......
...@@ -59,7 +59,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -59,7 +59,7 @@ namespace Titanium.Web.Proxy.Network
private readonly ConcurrentDictionary<string, CachedCertificate> certificateCache; private readonly ConcurrentDictionary<string, CachedCertificate> certificateCache;
private readonly ConcurrentDictionary<string, Task<X509Certificate2>> pendingCertificateCreationTasks; private readonly ConcurrentDictionary<string, Task<X509Certificate2>> pendingCertificateCreationTasks;
private readonly Action<Exception> exceptionFunc; private readonly ExceptionHandler exceptionFunc;
/// <summary> /// <summary>
/// Is the root certificate used by this proxy is valid? /// Is the root certificate used by this proxy is valid?
...@@ -107,7 +107,9 @@ namespace Titanium.Web.Proxy.Network ...@@ -107,7 +107,9 @@ namespace Titanium.Web.Proxy.Network
if (certEngine == null) if (certEngine == null)
{ {
certEngine = engine == CertificateEngine.BouncyCastle ? (ICertificateMaker)new BCCertificateMaker(exceptionFunc) : new WinCertificateMaker(exceptionFunc); certEngine = engine == CertificateEngine.BouncyCastle
? (ICertificateMaker)new BCCertificateMaker(exceptionFunc)
: new WinCertificateMaker(exceptionFunc);
} }
} }
} }
...@@ -198,7 +200,7 @@ namespace Titanium.Web.Proxy.Network ...@@ -198,7 +200,7 @@ namespace Titanium.Web.Proxy.Network
/// <param name="machineTrustRootCertificate">Note:setting machineTrustRootCertificate to true will force userTrustRootCertificate to true</param> /// <param name="machineTrustRootCertificate">Note:setting machineTrustRootCertificate to true will force userTrustRootCertificate to true</param>
/// <param name="trustRootCertificateAsAdmin"></param> /// <param name="trustRootCertificateAsAdmin"></param>
/// <param name="exceptionFunc"></param> /// <param name="exceptionFunc"></param>
internal CertificateManager(string rootCertificateName, string rootCertificateIssuerName, bool userTrustRootCertificate, bool machineTrustRootCertificate, bool trustRootCertificateAsAdmin, Action < Exception> exceptionFunc) internal CertificateManager(string rootCertificateName, string rootCertificateIssuerName, bool userTrustRootCertificate, bool machineTrustRootCertificate, bool trustRootCertificateAsAdmin, ExceptionHandler exceptionFunc)
{ {
this.exceptionFunc = exceptionFunc; this.exceptionFunc = exceptionFunc;
......
...@@ -63,7 +63,7 @@ namespace Titanium.Web.Proxy ...@@ -63,7 +63,7 @@ namespace Titanium.Web.Proxy
} }
catch (Exception e) catch (Exception e)
{ {
ExceptionFunc(new ProxyAuthorizationException("Error whilst authorizing request", e, httpHeaders)); ExceptionFunc(new ProxyAuthorizationException("Error whilst authorizing request", session, e, httpHeaders));
//Return not authorized //Return not authorized
session.WebSession.Response = CreateAuthentication407Response("Proxy Authentication Invalid"); session.WebSession.Response = CreateAuthentication407Response("Proxy Authentication Invalid");
......
...@@ -53,12 +53,12 @@ namespace Titanium.Web.Proxy ...@@ -53,12 +53,12 @@ namespace Titanium.Web.Proxy
/// <summary> /// <summary>
/// An default exception log func /// An default exception log func
/// </summary> /// </summary>
private readonly Lazy<Action<Exception>> defaultExceptionFunc = new Lazy<Action<Exception>>(() => (e => { })); private readonly Lazy<ExceptionHandler> defaultExceptionFunc = new Lazy<ExceptionHandler>(() => (e => { }));
/// <summary> /// <summary>
/// backing exception func for exposed public property /// backing exception func for exposed public property
/// </summary> /// </summary>
private Action<Exception> exceptionFunc; private ExceptionHandler exceptionFunc;
/// <summary> /// <summary>
/// Is the proxy currently running /// Is the proxy currently running
...@@ -192,7 +192,7 @@ namespace Titanium.Web.Proxy ...@@ -192,7 +192,7 @@ namespace Titanium.Web.Proxy
/// <summary> /// <summary>
/// Callback for error events in proxy /// Callback for error events in proxy
/// </summary> /// </summary>
public Action<Exception> ExceptionFunc public ExceptionHandler ExceptionFunc
{ {
get => exceptionFunc ?? defaultExceptionFunc.Value; get => exceptionFunc ?? defaultExceptionFunc.Value;
set => exceptionFunc = value; set => exceptionFunc = value;
......
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