mirror of
https://github.com/netchx/netch.git
synced 2026-03-30 19:09:48 +08:00
refactor: try catch ICSHelper exception #393
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user