Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 178fa97

Browse files
authored
Merge pull request #903 from justcoding121/develop
beta
2 parents 28ed4c9 + efecb69 commit 178fa97

26 files changed

+222
-154
lines changed

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

+28-20
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

@@ -1002,7 +1006,11 @@ void dispose(bool disposing)
10021006
return;
10031007
}
10041008

1005-
clearCertificatesTokenSource.Dispose();
1009+
if (disposing)
1010+
{
1011+
clearCertificatesTokenSource.Dispose();
1012+
}
1013+
10061014
disposed = true;
10071015
}
10081016

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

+8-2
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

@@ -684,7 +684,8 @@ public void TerminateServerConnection()
684684
HttpClient.CloseServerConnection = true;
685685
}
686686

687-
private bool disposed = false;
687+
private bool disposed;
688+
688689
protected override void Dispose(bool disposing)
689690
{
690691
if (disposed)
@@ -700,6 +701,11 @@ protected override void Dispose(bool disposing)
700701

701702
~SessionEventArgs()
702703
{
704+
#if DEBUG
705+
// Finalizer should not be called
706+
System.Diagnostics.Debugger.Break();
707+
#endif
708+
703709
Dispose(false);
704710
}
705711
}

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

+18-9
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)
@@ -159,19 +164,18 @@ protected virtual void Dispose(bool disposing)
159164
return;
160165
}
161166

162-
disposed = true;
163-
164167
if (disposing)
165168
{
166169
CustomUpStreamProxyUsed = null;
167170

168-
DataSent = null;
169-
DataReceived = null;
170-
Exception = null;
171+
HttpClient.FinishSession();
171172
}
172173

173-
HttpClient.FinishSession();
174+
DataSent = null;
175+
DataReceived = null;
176+
Exception = null;
174177

178+
disposed = true;
175179
}
176180

177181
public void Dispose()
@@ -182,6 +186,11 @@ public void Dispose()
182186

183187
~SessionEventArgsBase()
184188
{
189+
#if DEBUG
190+
// Finalizer should not be called
191+
System.Diagnostics.Debugger.Break();
192+
#endif
193+
185194
Dispose(false);
186195
}
187196

@@ -203,7 +212,7 @@ internal void OnDataSent(byte[] buffer, int offset, int count)
203212
}
204213
catch (Exception ex)
205214
{
206-
ExceptionFunc(new Exception("Exception thrown in user event", ex));
215+
OnException(new Exception("Exception thrown in user event", ex));
207216
}
208217
}
209218

@@ -215,7 +224,7 @@ internal void OnDataReceived(byte[] buffer, int offset, int count)
215224
}
216225
catch (Exception ex)
217226
{
218-
ExceptionFunc(new Exception("Exception thrown in user event", ex));
227+
OnException(new Exception("Exception thrown in user event", ex));
219228
}
220229
}
221230

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

+12-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,8 +72,18 @@ 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
}
78+
79+
~TunnelConnectSessionEventArgs()
80+
{
81+
#if DEBUG
82+
// Finalizer should not be called
83+
System.Diagnostics.Debugger.Break();
84+
#endif
85+
86+
Dispose(false);
87+
}
7888
}
7989
}

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ private async Task handleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
3838
Task<TcpServerConnection?>? prefetchConnectionTask = null;
3939
bool closeServerConnection = false;
4040

41+
TunnelConnectSessionEventArgs? connectArgs = null;
42+
4143
try
4244
{
43-
TunnelConnectSessionEventArgs? connectArgs = null;
44-
4545
var method = await HttpHelper.GetMethod(clientStream, BufferPool, cancellationToken);
4646
if (clientStream.IsClosed)
4747
{
@@ -402,6 +402,7 @@ await Http2Helper.SendHttp2(clientStream, connection.Stream,
402402
await tcpConnectionFactory.Release(prefetchConnectionTask, closeServerConnection);
403403

404404
clientStream.Dispose();
405+
connectArgs?.Dispose();
405406
}
406407
}
407408
}

0 commit comments

Comments
 (0)