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

Console lock issues #507

Merged
merged 4 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions examples/Titanium.Web.Proxy.Examples.Basic/ProxyTestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
public class ProxyTestController
{
private readonly SemaphoreSlim @lock = new SemaphoreSlim(1);

private readonly ProxyServer proxyServer;

private ExplicitProxyEndPoint explicitEndPoint;

public ProxyTestController()
Expand All @@ -32,27 +30,13 @@ public ProxyTestController()

proxyServer.ExceptionFunc = async exception =>
{
await @lock.WaitAsync();

try
if (exception is ProxyHttpException phex)
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
if (exception is ProxyHttpException phex)
{
Console.WriteLine(exception.Message + ": " + phex.InnerException?.Message);
}
else
{
Console.WriteLine(exception.Message);
}

Console.ForegroundColor = color;

await WriteToConsole(exception.Message + ": " + phex.InnerException?.Message, true);
}
finally
else
{
@lock.Release();
await WriteToConsole(exception.Message, true);
}
};
proxyServer.ForwardToUpstreamGateway = true;
Expand Down Expand Up @@ -110,7 +94,7 @@ public void StartProxy()
// Only explicit proxies can be set as system proxy!
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
if(RunTime.IsWindows)
if (RunTime.IsWindows)
{
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
}
Expand Down Expand Up @@ -277,18 +261,24 @@ public Task OnCertificateSelection(object sender, CertificateSelectionEventArgs
return Task.FromResult(0);
}

private async Task WriteToConsole(string message)
private async Task WriteToConsole(string message, bool useRedColor = false)
{
await @lock.WaitAsync();

try
if (useRedColor)
{
ConsoleColor existing = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ForegroundColor = existing;
}
finally
else
{
@lock.Release();
Console.WriteLine(message);
}

@lock.Release();

}

///// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net45;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>net45;netcoreapp2.1</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<LangVersion>7.1</LangVersion>
<Platforms>AnyCPU;x64</Platforms>
Expand Down
27 changes: 13 additions & 14 deletions src/Titanium.Web.Proxy/Network/CertificateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public sealed class CertificateManager : IDisposable
private X509Certificate2 rootCertificate;

private string rootCertificateName;

/// <summary>
/// Initializes a new instance of the <see cref="CertificateManager"/> class.
/// </summary>
Expand Down Expand Up @@ -242,7 +242,7 @@ public X509Certificate2 RootCertificate
public void Dispose()
{
}

private string getRootCertificateDirectory()
{
string assemblyLocation = Assembly.GetExecutingAssembly().Location;
Expand Down Expand Up @@ -427,17 +427,16 @@ internal X509Certificate2 CreateCertificate(string certificateName, bool isRootC
certificate = makeCertificate(certificateName, false);

// store as cache
Task.Run(() =>
try
{
var exported = certificate.Export(X509ContentType.Pkcs12);
File.WriteAllBytes(certificatePath, exported);
}
catch (Exception e)
{
try
{
File.WriteAllBytes(certificatePath, certificate.Export(X509ContentType.Pkcs12));
}
catch (Exception e)
{
ExceptionFunc(new Exception("Failed to save fake certificate.", e));
}
});
ExceptionFunc(new Exception("Failed to save fake certificate.", e));
}

}
else
{
Expand Down Expand Up @@ -530,7 +529,7 @@ internal async void ClearIdleCertificates()
await Task.Delay(1000 * 60);
}
}

/// <summary>
/// Stops the certificate cache clear process
/// </summary>
Expand Down Expand Up @@ -776,7 +775,7 @@ public void EnsureRootCertificate(bool userTrustRootCertificate,

EnsureRootCertificate();
}

/// <summary>
/// Determines whether the root certificate is trusted.
/// </summary>
Expand Down