Catch and log known exceptions in SystemProxy

This commit is contained in:
noisyfox
2016-11-12 19:38:47 +11:00
parent 0101aa3741
commit cfc6e674da
4 changed files with 50 additions and 13 deletions
@@ -21,25 +21,32 @@ namespace Shadowsocks.Controller
enabled = false;
}
if (enabled)
try
{
if (global)
if (enabled)
{
WinINet.SetIEProxy(true, true, "127.0.0.1:" + config.localPort.ToString(), "");
if (global)
{
WinINet.SetIEProxy(true, true, "127.0.0.1:" + config.localPort.ToString(), "");
}
else
{
string pacUrl;
if (config.useOnlinePac && !config.pacUrl.IsNullOrEmpty())
pacUrl = config.pacUrl;
else
pacUrl = $"http://127.0.0.1:{config.localPort}/pac?t={GetTimestamp(DateTime.Now)}";
WinINet.SetIEProxy(true, false, "", pacUrl);
}
}
else
{
string pacUrl;
if (config.useOnlinePac && !config.pacUrl.IsNullOrEmpty())
pacUrl = config.pacUrl;
else
pacUrl = $"http://127.0.0.1:{config.localPort}/pac?t={GetTimestamp(DateTime.Now)}";
WinINet.SetIEProxy(true, false, "", pacUrl);
WinINet.SetIEProxy(false, false, "", "");
}
}
else
catch (ProxyException ex)
{
WinINet.SetIEProxy(false, false, "", "");
Logging.LogUsefulException(ex);
}
}
}
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Shadowsocks.Util.SystemProxy
{
class ProxyException : Exception
{
public ProxyException()
{
}
public ProxyException(string message) : base(message)
{
}
public ProxyException(string message, Exception innerException) : base(message, innerException)
{
}
protected ProxyException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
@@ -17,6 +17,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using Shadowsocks.Controller;
@@ -131,7 +132,7 @@ namespace Shadowsocks.Util.SystemProxy
// Throw an exception if this operation failed.
if (!bReturn)
{
throw new Exception("InternetSetOption: " + Marshal.GetLastWin32Error());
throw new ProxyException("InternetSetOption failed.", new Win32Exception());
}
// Notify the system that the registry settings have been changed and cause
@@ -161,7 +162,7 @@ namespace Shadowsocks.Util.SystemProxy
var ret = RemoteAccessService.GetAllConns(ref allConnections);
if (ret == 2)
throw new Exception("Cannot get all connections");
throw new ProxyException("Cannot get all connections");
if (ret == 1)
{
@@ -191,6 +191,7 @@
<Compile Include="Util\SystemProxy\INTERNET_PER_CONN_OPTION.cs" />
<Compile Include="Util\SystemProxy\INTERNET_PER_CONN_OPTION_LIST.cs" />
<Compile Include="Util\SystemProxy\NativeMethods.cs" />
<Compile Include="Util\SystemProxy\ProxyException.cs" />
<Compile Include="Util\SystemProxy\RAS.cs" />
<Compile Include="Util\SystemProxy\WinINet.cs" />
<Compile Include="Util\Util.cs" />