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

Commit 2e1fd72

Browse files
Merge pull request #507 from justcoding121/master
Console lock issues
2 parents 9fa89a1 + c037b26 commit 2e1fd72

File tree

3 files changed

+29
-40
lines changed

3 files changed

+29
-40
lines changed

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

Lines changed: 15 additions & 25 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, true);
5236
}
53-
finally
37+
else
5438
{
55-
@lock.Release();
39+
await WriteToConsole(exception.Message, true);
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
}
@@ -277,18 +261,24 @@ public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs
277261
return Task.FromResult(0);
278262
}
279263

280-
private async Task WriteToConsole(string message)
264+
private async Task WriteToConsole(string message, bool useRedColor = false)
281265
{
282266
await @lock.WaitAsync();
283267

284-
try
268+
if (useRedColor)
285269
{
270+
ConsoleColor existing = Console.ForegroundColor;
271+
Console.ForegroundColor = ConsoleColor.Red;
286272
Console.WriteLine(message);
273+
Console.ForegroundColor = existing;
287274
}
288-
finally
275+
else
289276
{
290-
@lock.Release();
277+
Console.WriteLine(message);
291278
}
279+
280+
@lock.Release();
281+
292282
}
293283

294284
///// <summary>

examples/Titanium.Web.Proxy.Examples.Basic/Titanium.Web.Proxy.Examples.Basic.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFrameworks>net45;netcoreapp2.0</TargetFrameworks>
5+
<TargetFrameworks>net45;netcoreapp2.1</TargetFrameworks>
66
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
77
<LangVersion>7.1</LangVersion>
88
<Platforms>AnyCPU;x64</Platforms>

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)