refactor: try catch ICSHelper exception #393

This commit is contained in:
ChsBuffer
2020-10-09 19:21:00 +08:00
parent 9d78de0f6c
commit be53432a2e
2 changed files with 72 additions and 25 deletions

View File

@@ -15,6 +15,7 @@ namespace Netch.Forms
public SettingForm()
{
InitializeComponent();
InitText();
}
private void InitValue()
@@ -39,11 +40,18 @@ namespace Netch.Forms
ProxyDNSCheckBox.Checked = Global.Settings.TUNTAP.ProxyDNS;
UseFakeDNSCheckBox.Checked = Global.Settings.TUNTAP.UseFakeDNS;
var icsHelperEnabled = ICSHelper.Enabled;
if (icsHelperEnabled != null)
try
{
ICSCheckBox.Enabled = true;
ICSCheckBox.Checked = (bool) icsHelperEnabled;
var icsHelperEnabled = ICSHelper.Enabled;
if (icsHelperEnabled != null)
{
ICSCheckBox.Enabled = true;
ICSCheckBox.Checked = (bool) icsHelperEnabled;
}
}
catch
{
// ignored
}
// Behavior
@@ -105,9 +113,9 @@ namespace Netch.Forms
private void SettingForm_Load(object sender, EventArgs e)
{
UseFakeDNSCheckBox.Visible = Global.Flags.SupportFakeDns;
InitText();
InitValue();
Task.Run(() => BeginInvoke(new Action(() => UseFakeDNSCheckBox.Visible = Global.Flags.SupportFakeDns)));
}
private void GlobalBypassIPsButton_Click(object sender, EventArgs e)
@@ -388,20 +396,31 @@ namespace Netch.Forms
private async void ICSCheckBox_CheckedChanged(object sender, EventArgs e)
{
ICSCheckBox.Enabled = false;
await Task.Run(() =>
try
{
if (ICSCheckBox.Checked)
ICSCheckBox.Enabled = false;
await Task.Run(() =>
{
if (!(ICSHelper.Enabled ?? true))
ICSCheckBox.Checked = ICSHelper.Enable();
}
else
{
ICSHelper.Disable();
}
});
ICSCheckBox.Enabled = true;
if (ICSCheckBox.Checked)
{
if (!(ICSHelper.Enabled ?? true))
ICSCheckBox.Checked = ICSHelper.Enable();
}
else
{
ICSHelper.Disable();
}
});
}
catch (Exception exception)
{
ICSCheckBox.Checked = false;
Logging.Error(exception.ToString());
}
finally
{
ICSCheckBox.Enabled = true;
}
}
}
}

View File

@@ -13,22 +13,40 @@ namespace Netch.Utils
{
get
{
if (Global.TUNTAP.Adapter == null)
TUNTAPController.SearchTapAdapter();
AutoSearchTapAdapter();
foreach (var connection in new NetworkConnectionCollection().Cast<NetworkConnection>().Where(connection => connection.DeviceName == Global.TUNTAP.Adapter?.Description))
if (Global.TUNTAP.Adapter == null)
return null;
foreach (NetworkConnection connection in new NetworkConnectionCollection())
{
return connection.SharingEnabled;
try
{
if (connection.DeviceName == Global.TUNTAP.Adapter?.Description)
{
return connection.SharingEnabled;
}
}
catch (Exception e)
{
Logging.Warning(e.ToString());
}
}
return null;
}
}
private static void AutoSearchTapAdapter()
{
if (Global.TUNTAP.Adapter == null)
TUNTAPController.SearchTapAdapter();
}
public static bool Enable()
{
Utils.SearchOutboundAdapter(false);
TUNTAPController.SearchTapAdapter();
AutoSearchTapAdapter();
if (Global.TUNTAP.Adapter == null || Global.Outbound.Adapter == null)
{
@@ -136,9 +154,19 @@ namespace Netch.Utils
public static void Disable()
{
foreach (var connection in new NetworkConnectionCollection().Cast<NetworkConnection>().Where(connection => connection.SharingEnabled))
foreach (NetworkConnection connection in new NetworkConnectionCollection())
{
connection.DisableSharing();
try
{
if (connection.SharingEnabled)
{
connection.DisableSharing();
}
}
catch (Exception e)
{
Logging.Warning(e.ToString());
}
}
CleanupWMISharingEntries();