Unverified Commit 2e1fd72c authored by Jehonathan Thomas's avatar Jehonathan Thomas Committed by GitHub

Merge pull request #507 from justcoding121/master

Console lock issues
parents 9fa89a12 c037b265
...@@ -16,9 +16,7 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -16,9 +16,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
public class ProxyTestController public class ProxyTestController
{ {
private readonly SemaphoreSlim @lock = new SemaphoreSlim(1); private readonly SemaphoreSlim @lock = new SemaphoreSlim(1);
private readonly ProxyServer proxyServer; private readonly ProxyServer proxyServer;
private ExplicitProxyEndPoint explicitEndPoint; private ExplicitProxyEndPoint explicitEndPoint;
public ProxyTestController() public ProxyTestController()
...@@ -32,27 +30,13 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -32,27 +30,13 @@ namespace Titanium.Web.Proxy.Examples.Basic
proxyServer.ExceptionFunc = async exception => proxyServer.ExceptionFunc = async exception =>
{ {
await @lock.WaitAsync();
try
{
var color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
if (exception is ProxyHttpException phex) if (exception is ProxyHttpException phex)
{ {
Console.WriteLine(exception.Message + ": " + phex.InnerException?.Message); await WriteToConsole(exception.Message + ": " + phex.InnerException?.Message, true);
} }
else else
{ {
Console.WriteLine(exception.Message); await WriteToConsole(exception.Message, true);
}
Console.ForegroundColor = color;
}
finally
{
@lock.Release();
} }
}; };
proxyServer.ForwardToUpstreamGateway = true; proxyServer.ForwardToUpstreamGateway = true;
...@@ -110,7 +94,7 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -110,7 +94,7 @@ namespace Titanium.Web.Proxy.Examples.Basic
// Only explicit proxies can be set as system proxy! // Only explicit proxies can be set as system proxy!
//proxyServer.SetAsSystemHttpProxy(explicitEndPoint); //proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
//proxyServer.SetAsSystemHttpsProxy(explicitEndPoint); //proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
if(RunTime.IsWindows) if (RunTime.IsWindows)
{ {
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp); proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
} }
...@@ -277,18 +261,24 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -277,18 +261,24 @@ namespace Titanium.Web.Proxy.Examples.Basic
return Task.FromResult(0); return Task.FromResult(0);
} }
private async Task WriteToConsole(string message) private async Task WriteToConsole(string message, bool useRedColor = false)
{ {
await @lock.WaitAsync(); await @lock.WaitAsync();
try if (useRedColor)
{ {
ConsoleColor existing = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message); Console.WriteLine(message);
Console.ForegroundColor = existing;
} }
finally else
{ {
@lock.Release(); Console.WriteLine(message);
} }
@lock.Release();
} }
///// <summary> ///// <summary>
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFrameworks>net45;netcoreapp2.0</TargetFrameworks> <TargetFrameworks>net45;netcoreapp2.1</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo> <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<LangVersion>7.1</LangVersion> <LangVersion>7.1</LangVersion>
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
......
...@@ -427,17 +427,16 @@ namespace Titanium.Web.Proxy.Network ...@@ -427,17 +427,16 @@ namespace Titanium.Web.Proxy.Network
certificate = makeCertificate(certificateName, false); certificate = makeCertificate(certificateName, false);
// store as cache // store as cache
Task.Run(() =>
{
try try
{ {
File.WriteAllBytes(certificatePath, certificate.Export(X509ContentType.Pkcs12)); var exported = certificate.Export(X509ContentType.Pkcs12);
File.WriteAllBytes(certificatePath, exported);
} }
catch (Exception e) catch (Exception e)
{ {
ExceptionFunc(new Exception("Failed to save fake certificate.", e)); ExceptionFunc(new Exception("Failed to save fake certificate.", e));
} }
});
} }
else else
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment