From be53432a2e96f368d752e15f6b376bf4c5eb3d62 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 9 Oct 2020 19:21:00 +0800 Subject: [PATCH] refactor: try catch ICSHelper exception #393 --- Netch/Forms/SettingForm.cs | 55 +++++++++++++++++++++++++------------- Netch/Utils/ICSHelper.cs | 42 ++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 25 deletions(-) diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 9a331613..e098c843 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -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; + } } } } \ No newline at end of file diff --git a/Netch/Utils/ICSHelper.cs b/Netch/Utils/ICSHelper.cs index 7306b223..bbb7662f 100644 --- a/Netch/Utils/ICSHelper.cs +++ b/Netch/Utils/ICSHelper.cs @@ -13,22 +13,40 @@ namespace Netch.Utils { get { - if (Global.TUNTAP.Adapter == null) - TUNTAPController.SearchTapAdapter(); + AutoSearchTapAdapter(); - foreach (var connection in new NetworkConnectionCollection().Cast().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().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();