Skip to content

Commit 8d6c23f

Browse files
committed
more dispose fixes
1 parent 9d2b534 commit 8d6c23f

24 files changed

+172
-141
lines changed

src/Titanium.Web.Proxy/Certificates/CertificateManager.cs

+23-19
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private ICertificateMaker certEngine
120120
/// <param name="exceptionFunc"></param>
121121
internal CertificateManager(string? rootCertificateName, string? rootCertificateIssuerName,
122122
bool userTrustRootCertificate, bool machineTrustRootCertificate, bool trustRootCertificateAsAdmin,
123-
ExceptionHandler exceptionFunc)
123+
ExceptionHandler? exceptionFunc)
124124
{
125125
ExceptionFunc = exceptionFunc;
126126

@@ -167,7 +167,7 @@ internal CertificateManager(string? rootCertificateName, string? rootCertificate
167167
/// <summary>
168168
/// Exception handler
169169
/// </summary>
170-
internal ExceptionHandler ExceptionFunc { get; set; }
170+
internal ExceptionHandler? ExceptionFunc { get; set; }
171171

172172
/// <summary>
173173
/// Select Certificate Engine.
@@ -339,7 +339,7 @@ private void installCertificate(StoreName storeName, StoreLocation storeLocation
339339
}
340340
catch (Exception e)
341341
{
342-
ExceptionFunc(
342+
onException(
343343
new Exception("Failed to make system trust root certificate "
344344
+ $" for {storeName}\\{storeLocation} store location. You may need admin rights.",
345345
e));
@@ -360,7 +360,7 @@ private void uninstallCertificate(StoreName storeName, StoreLocation storeLocati
360360
{
361361
if (certificate == null)
362362
{
363-
ExceptionFunc(new Exception("Could not remove certificate as it is null or empty."));
363+
onException(new Exception("Could not remove certificate as it is null or empty."));
364364
return;
365365
}
366366

@@ -374,9 +374,8 @@ private void uninstallCertificate(StoreName storeName, StoreLocation storeLocati
374374
}
375375
catch (Exception e)
376376
{
377-
ExceptionFunc(
378-
new Exception("Failed to remove root certificate trust "
379-
+ $" for {storeLocation} store location. You may need admin rights.", e));
377+
onException(new Exception("Failed to remove root certificate trust "
378+
+ $" for {storeLocation} store location. You may need admin rights.", e));
380379
}
381380
finally
382381
{
@@ -408,6 +407,11 @@ private X509Certificate2 makeCertificate(string certificateName, bool isRootCert
408407
return certificate;
409408
}
410409

410+
private void onException(Exception exception)
411+
{
412+
ExceptionFunc?.Invoke(exception);
413+
}
414+
411415
private static ConcurrentDictionary<string, object> saveCertificateLocks
412416
= new ConcurrentDictionary<string, object>();
413417

@@ -434,13 +438,13 @@ private static ConcurrentDictionary<string, object> saveCertificateLocks
434438

435439
if (certificate != null && certificate.NotAfter <= DateTime.Now)
436440
{
437-
ExceptionFunc(new Exception($"Cached certificate for {subjectName} has expired."));
441+
onException(new Exception($"Cached certificate for {subjectName} has expired."));
438442
certificate = null;
439443
}
440444
}
441445
catch (Exception e)
442446
{
443-
ExceptionFunc(new Exception("Failed to load fake certificate.", e));
447+
onException(new Exception("Failed to load fake certificate.", e));
444448
certificate = null;
445449
}
446450

@@ -472,7 +476,7 @@ private static ConcurrentDictionary<string, object> saveCertificateLocks
472476
}
473477
catch (Exception e)
474478
{
475-
ExceptionFunc(new Exception("Failed to save fake certificate.", e));
479+
onException(new Exception("Failed to save fake certificate.", e));
476480
}
477481
});
478482
}
@@ -484,7 +488,7 @@ private static ConcurrentDictionary<string, object> saveCertificateLocks
484488
}
485489
catch (Exception e)
486490
{
487-
ExceptionFunc(e);
491+
onException(e);
488492
certificate = null;
489493
}
490494

@@ -628,7 +632,7 @@ public bool CreateRootCertificate(bool persistToFile = true)
628632

629633
if (rootCert != null && rootCert.NotAfter <= DateTime.Now)
630634
{
631-
ExceptionFunc(new Exception("Loaded root certificate has expired."));
635+
onException(new Exception("Loaded root certificate has expired."));
632636
return false;
633637
}
634638

@@ -641,7 +645,7 @@ public bool CreateRootCertificate(bool persistToFile = true)
641645
catch (Exception e)
642646
{
643647
// root cert cannot be loaded
644-
ExceptionFunc(new Exception("Root cert cannot be loaded.", e));
648+
onException(new Exception("Root cert cannot be loaded.", e));
645649
}
646650
}
647651

@@ -651,7 +655,7 @@ public bool CreateRootCertificate(bool persistToFile = true)
651655
}
652656
catch (Exception e)
653657
{
654-
ExceptionFunc(e);
658+
onException(e);
655659
}
656660

657661
if (persistToFile && RootCertificate != null)
@@ -664,14 +668,14 @@ public bool CreateRootCertificate(bool persistToFile = true)
664668
}
665669
catch (Exception e)
666670
{
667-
ExceptionFunc(new Exception("An error happened when clearing certificate cache.", e));
671+
onException(new Exception("An error happened when clearing certificate cache.", e));
668672
}
669673

670674
certificateCache.SaveRootCertificate(PfxFilePath, PfxPassword, RootCertificate);
671675
}
672676
catch (Exception e)
673677
{
674-
ExceptionFunc(e);
678+
onException(e);
675679
}
676680
}
677681

@@ -691,15 +695,15 @@ public bool CreateRootCertificate(bool persistToFile = true)
691695

692696
if (rootCert != null && rootCert.NotAfter <= DateTime.Now)
693697
{
694-
ExceptionFunc(new ArgumentException("Loaded root certificate has expired."));
698+
onException(new ArgumentException("Loaded root certificate has expired."));
695699
return null;
696700
}
697701

698702
return rootCert;
699703
}
700704
catch (Exception e)
701705
{
702-
ExceptionFunc(e);
706+
onException(e);
703707
return null;
704708
}
705709
}
@@ -808,7 +812,7 @@ public bool TrustRootCertificateAsAdmin(bool machineTrusted = false)
808812
}
809813
catch (Exception e)
810814
{
811-
ExceptionFunc(e);
815+
onException(e);
812816
return false;
813817
}
814818

src/Titanium.Web.Proxy/Certificates/Makers/BCCertificateMaker.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ internal class BCCertificateMaker : ICertificateMaker
3434
// Set this flag to true when exception detected to avoid further exceptions
3535
private static bool doNotSetFriendlyName;
3636

37-
private readonly ExceptionHandler exceptionFunc;
37+
private readonly ExceptionHandler? exceptionFunc;
3838

39-
internal BCCertificateMaker(ExceptionHandler exceptionFunc, int certificateValidDays)
39+
internal BCCertificateMaker(ExceptionHandler? exceptionFunc, int certificateValidDays)
4040
{
4141
this.certificateValidDays = certificateValidDays;
4242
this.exceptionFunc = exceptionFunc;

src/Titanium.Web.Proxy/Certificates/Makers/BCCertificateMakerFast.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ internal class BCCertificateMakerFast : ICertificateMaker
3434
// Set this flag to true when exception detected to avoid further exceptions
3535
private static bool doNotSetFriendlyName;
3636

37-
private readonly ExceptionHandler exceptionFunc;
37+
private readonly ExceptionHandler? exceptionFunc;
3838

3939
public AsymmetricCipherKeyPair KeyPair { get; set; }
4040

41-
internal BCCertificateMakerFast(ExceptionHandler exceptionFunc, int certificateValidDays)
41+
internal BCCertificateMakerFast(ExceptionHandler? exceptionFunc, int certificateValidDays)
4242
{
4343
this.certificateValidDays = certificateValidDays;
4444
this.exceptionFunc = exceptionFunc;

src/Titanium.Web.Proxy/Certificates/Makers/WinCertificateMaker.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal class WinCertificateMaker : ICertificateMaker
1717
// Validity Days for Root Certificates Generated.
1818
private int certificateValidDays;
1919

20-
private readonly ExceptionHandler exceptionFunc;
20+
private readonly ExceptionHandler? exceptionFunc;
2121

2222
private readonly string sProviderName = "Microsoft Enhanced Cryptographic Provider v1.0";
2323

@@ -53,7 +53,7 @@ internal class WinCertificateMaker : ICertificateMaker
5353
/// <summary>
5454
/// Constructor.
5555
/// </summary>
56-
internal WinCertificateMaker(ExceptionHandler exceptionFunc, int certificateValidDays)
56+
internal WinCertificateMaker(ExceptionHandler? exceptionFunc, int certificateValidDays)
5757
{
5858
this.certificateValidDays = certificateValidDays;
5959
this.exceptionFunc = exceptionFunc;

src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ internal void OnMultipartRequestPartSent(ReadOnlySpan<char> boundary, HeaderColl
144144
}
145145
catch (Exception ex)
146146
{
147-
ExceptionFunc(new Exception("Exception thrown in user event", ex));
147+
OnException(new Exception("Exception thrown in user event", ex));
148148
}
149149
}
150150

src/Titanium.Web.Proxy/EventArguments/SessionEventArgsBase.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public abstract class SessionEventArgsBase : ProxyEventArgsBase, IDisposable
3737
public Guid ServerConnectionId => HttpClient.HasConnection ? ServerConnection.Id : Guid.Empty;
3838

3939
protected readonly IBufferPool BufferPool;
40-
protected readonly ExceptionHandler ExceptionFunc;
40+
protected readonly ExceptionHandler? ExceptionFunc;
4141
private bool enableWinAuth;
4242

4343
/// <summary>
@@ -150,6 +150,11 @@ public bool EnableWinAuth
150150
/// </summary>
151151
public Exception? Exception { get; internal set; }
152152

153+
protected void OnException(Exception exception)
154+
{
155+
ExceptionFunc?.Invoke(exception);
156+
}
157+
153158
private bool disposed = false;
154159

155160
protected virtual void Dispose(bool disposing)
@@ -207,7 +212,7 @@ internal void OnDataSent(byte[] buffer, int offset, int count)
207212
}
208213
catch (Exception ex)
209214
{
210-
ExceptionFunc(new Exception("Exception thrown in user event", ex));
215+
OnException(new Exception("Exception thrown in user event", ex));
211216
}
212217
}
213218

@@ -219,7 +224,7 @@ internal void OnDataReceived(byte[] buffer, int offset, int count)
219224
}
220225
catch (Exception ex)
221226
{
222-
ExceptionFunc(new Exception("Exception thrown in user event", ex));
227+
OnException(new Exception("Exception thrown in user event", ex));
223228
}
224229
}
225230

src/Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal void OnDecryptedDataSent(byte[] buffer, int offset, int count)
6060
}
6161
catch (Exception ex)
6262
{
63-
ExceptionFunc(new Exception("Exception thrown in user event", ex));
63+
OnException(new Exception("Exception thrown in user event", ex));
6464
}
6565
}
6666

@@ -72,7 +72,7 @@ internal void OnDecryptedDataReceived(byte[] buffer, int offset, int count)
7272
}
7373
catch (Exception ex)
7474
{
75-
ExceptionFunc(new Exception("Exception thrown in user event", ex));
75+
OnException(new Exception("Exception thrown in user event", ex));
7676
}
7777
}
7878

src/Titanium.Web.Proxy/Extensions/FuncExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Titanium.Web.Proxy.Extensions
77
internal static class FuncExtensions
88
{
99
internal static async Task InvokeAsync<T>(this AsyncEventHandler<T> callback, object sender, T args,
10-
ExceptionHandler exceptionFunc)
10+
ExceptionHandler? exceptionFunc)
1111
{
1212
var invocationList = callback.GetInvocationList();
1313

@@ -18,15 +18,15 @@ internal static async Task InvokeAsync<T>(this AsyncEventHandler<T> callback, ob
1818
}
1919

2020
private static async Task internalInvokeAsync<T>(AsyncEventHandler<T> callback, object sender, T args,
21-
ExceptionHandler exceptionFunc)
21+
ExceptionHandler? exceptionFunc)
2222
{
2323
try
2424
{
2525
await callback(sender, args);
2626
}
2727
catch (Exception e)
2828
{
29-
exceptionFunc(new Exception("Exception thrown in user event", e));
29+
exceptionFunc?.Invoke(new Exception("Exception thrown in user event", e));
3030
}
3131
}
3232
}

src/Titanium.Web.Proxy/Handlers/ProxyAuthorizationHandler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private async Task<bool> checkAuthorization(SessionEventArgsBase session)
7272
}
7373
catch (Exception e)
7474
{
75-
ExceptionFunc(new ProxyAuthorizationException("Error whilst authorizing request", session, e,
75+
onException(null, new ProxyAuthorizationException("Error whilst authorizing request", session, e,
7676
httpHeaders));
7777

7878
// Return not authorized

src/Titanium.Web.Proxy/Helpers/TcpHelper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ private static async Task sendRawTap(Stream clientStream, Stream serverStream, I
133133
internal static Task SendRaw(Stream clientStream, Stream serverStream, IBufferPool bufferPool,
134134
Action<byte[], int, int>? onDataSend, Action<byte[], int, int>? onDataReceive,
135135
CancellationTokenSource cancellationTokenSource,
136-
ExceptionHandler exceptionFunc)
136+
ExceptionHandler? exceptionFunc)
137137
{
138138
// todo: fix APM mode
139139
return sendRawTap(clientStream, serverStream, bufferPool, onDataSend, onDataReceive,

src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs

+2-13
Original file line numberDiff line numberDiff line change
@@ -349,32 +349,21 @@ private enum AutoWebProxyState
349349

350350
private bool disposed = false;
351351

352-
void dispose(bool disposing)
352+
public void Dispose()
353353
{
354354
if (disposed)
355355
{
356356
return;
357357
}
358358

359-
disposed = true;
360-
361359
if (session == null || session.IsInvalid)
362360
{
363361
return;
364362
}
365363

366364
session.Close();
367-
}
368-
369-
public void Dispose()
370-
{
371-
dispose(true);
372-
GC.SuppressFinalize(this);
373-
}
374365

375-
~WinHttpWebProxyFinder()
376-
{
377-
dispose(false);
366+
disposed = true;
378367
}
379368
}
380369
}

src/Titanium.Web.Proxy/Http2/Http2Helper.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal static async Task SendHttp2(Stream clientStream, Stream serverStream,
3434
Func<SessionEventArgs> sessionFactory,
3535
Func<SessionEventArgs, Task> onBeforeRequest, Func<SessionEventArgs, Task> onBeforeResponse,
3636
CancellationTokenSource cancellationTokenSource, Guid connectionId,
37-
ExceptionHandler exceptionFunc)
37+
ExceptionHandler? exceptionFunc)
3838
{
3939
var clientSettings = new Http2Settings();
4040
var serverSettings = new Http2Settings();
@@ -62,7 +62,7 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output,
6262
Func<SessionEventArgs> sessionFactory, ConcurrentDictionary<int, SessionEventArgs> sessions,
6363
Func<SessionEventArgs, Task> onBeforeRequestResponse,
6464
Guid connectionId, bool isClient, CancellationToken cancellationToken,
65-
ExceptionHandler exceptionFunc)
65+
ExceptionHandler? exceptionFunc)
6666
{
6767
int headerTableSize = 0;
6868
Decoder? decoder = null;
@@ -281,7 +281,7 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output,
281281
}
282282
catch (Exception ex)
283283
{
284-
exceptionFunc(new ProxyHttpException("Failed to decode HTTP/2 headers", ex, args));
284+
exceptionFunc?.Invoke(new ProxyHttpException("Failed to decode HTTP/2 headers", ex, args));
285285
}
286286

287287
if (!endHeaders)
@@ -351,7 +351,7 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output,
351351
if (streamId == 0)
352352
{
353353
// connection error
354-
exceptionFunc(new ProxyHttpException("HTTP/2 connection error. Error code: " + errorCode, null, args));
354+
exceptionFunc?.Invoke(new ProxyHttpException("HTTP/2 connection error. Error code: " + errorCode, null, args));
355355
return;
356356
}
357357
else
@@ -361,7 +361,7 @@ private static async Task copyHttp2FrameAsync(Stream input, Stream output,
361361

362362
if (errorCode != 8 /*cancel*/)
363363
{
364-
exceptionFunc(new ProxyHttpException("HTTP/2 stream error. Error code: " + errorCode, null, args));
364+
exceptionFunc?.Invoke(new ProxyHttpException("HTTP/2 stream error. Error code: " + errorCode, null, args));
365365
}
366366
}
367367
}

0 commit comments

Comments
 (0)