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

Commit a863fd1

Browse files
committed
fix lock issues causing memory violation
1 parent 3b8ae98 commit a863fd1

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
1616
public class ProxyTestController
1717
{
1818
private readonly SemaphoreSlim @lock = new SemaphoreSlim(1);
19-
2019
private readonly ProxyServer proxyServer;
21-
2220
private ExplicitProxyEndPoint explicitEndPoint;
2321

2422
public ProxyTestController()
@@ -32,27 +30,13 @@ public ProxyTestController()
3230

3331
proxyServer.ExceptionFunc = async exception =>
3432
{
35-
await @lock.WaitAsync();
36-
37-
try
33+
if (exception is ProxyHttpException phex)
3834
{
39-
var color = Console.ForegroundColor;
40-
Console.ForegroundColor = ConsoleColor.Red;
41-
if (exception is ProxyHttpException phex)
42-
{
43-
Console.WriteLine(exception.Message + ": " + phex.InnerException?.Message);
44-
}
45-
else
46-
{
47-
Console.WriteLine(exception.Message);
48-
}
49-
50-
Console.ForegroundColor = color;
51-
35+
await WriteToConsole(exception.Message + ": " + phex.InnerException?.Message, ConsoleColor.Red);
5236
}
53-
finally
37+
else
5438
{
55-
@lock.Release();
39+
await WriteToConsole(exception.Message, ConsoleColor.Red);
5640
}
5741
};
5842
proxyServer.ForwardToUpstreamGateway = true;
@@ -110,7 +94,7 @@ public void StartProxy()
11094
// Only explicit proxies can be set as system proxy!
11195
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
11296
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
113-
if(RunTime.IsWindows)
97+
if (RunTime.IsWindows)
11498
{
11599
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
116100
}
@@ -281,6 +265,24 @@ private async Task WriteToConsole(string message)
281265
{
282266
await @lock.WaitAsync();
283267

268+
ConsoleColor color;
269+
270+
try
271+
{
272+
color = Console.ForegroundColor;
273+
}
274+
finally
275+
{
276+
@lock.Release();
277+
}
278+
279+
await WriteToConsole(message, color);
280+
}
281+
282+
private async Task WriteToConsole(string message, ConsoleColor color)
283+
{
284+
await @lock.WaitAsync();
285+
284286
try
285287
{
286288
Console.WriteLine(message);
@@ -289,6 +291,7 @@ private async Task WriteToConsole(string message)
289291
{
290292
@lock.Release();
291293
}
294+
292295
}
293296

294297
///// <summary>

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public sealed class CertificateManager : IDisposable
6060
private X509Certificate2 rootCertificate;
6161

6262
private string rootCertificateName;
63-
63+
6464
/// <summary>
6565
/// Initializes a new instance of the <see cref="CertificateManager"/> class.
6666
/// </summary>
@@ -242,7 +242,7 @@ public X509Certificate2 RootCertificate
242242
public void Dispose()
243243
{
244244
}
245-
245+
246246
private string getRootCertificateDirectory()
247247
{
248248
string assemblyLocation = Assembly.GetExecutingAssembly().Location;
@@ -427,17 +427,16 @@ internal X509Certificate2 CreateCertificate(string certificateName, bool isRootC
427427
certificate = makeCertificate(certificateName, false);
428428

429429
// store as cache
430-
Task.Run(() =>
430+
try
431+
{
432+
var exported = certificate.Export(X509ContentType.Pkcs12);
433+
File.WriteAllBytes(certificatePath, exported);
434+
}
435+
catch (Exception e)
431436
{
432-
try
433-
{
434-
File.WriteAllBytes(certificatePath, certificate.Export(X509ContentType.Pkcs12));
435-
}
436-
catch (Exception e)
437-
{
438-
ExceptionFunc(new Exception("Failed to save fake certificate.", e));
439-
}
440-
});
437+
ExceptionFunc(new Exception("Failed to save fake certificate.", e));
438+
}
439+
441440
}
442441
else
443442
{
@@ -530,7 +529,7 @@ internal async void ClearIdleCertificates()
530529
await Task.Delay(1000 * 60);
531530
}
532531
}
533-
532+
534533
/// <summary>
535534
/// Stops the certificate cache clear process
536535
/// </summary>
@@ -776,7 +775,7 @@ public void EnsureRootCertificate(bool userTrustRootCertificate,
776775

777776
EnsureRootCertificate();
778777
}
779-
778+
780779
/// <summary>
781780
/// Determines whether the root certificate is trusted.
782781
/// </summary>

0 commit comments

Comments
 (0)