From a11463d3dbacfa3e94a6e1cf43213c9b9a6cf76b Mon Sep 17 00:00:00 2001 From: D9ping Date: Sat, 6 Aug 2016 17:07:18 +0200 Subject: [PATCH] closes #3 Load current settings on startup. Signed-off-by: D9ping --- IPv6Control/DisabledComponentValue.cs | 218 ++++++++++++++++++++------ IPv6Control/FrmMain.cs | 35 ++++- 2 files changed, 202 insertions(+), 51 deletions(-) diff --git a/IPv6Control/DisabledComponentValue.cs b/IPv6Control/DisabledComponentValue.cs index 74a3c68..e1ab3ce 100644 --- a/IPv6Control/DisabledComponentValue.cs +++ b/IPv6Control/DisabledComponentValue.cs @@ -1,14 +1,13 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Security; -using System.Text; namespace IPv6Control { class DisabledComponentValue { - private int decvalue = 0; + private UInt32 decvalue = 0; + private bool docalculatevalue = false; + private bool disableAllTransitionTechnologies = false; private bool disable6to4 = false; private bool disableISATAP = false; @@ -17,6 +16,21 @@ class DisabledComponentValue private bool preferIPv4 = false; private bool disableAllIpHttps = false; + /// + /// Creating a new instance of DisabledComponentValue class. + /// + public DisabledComponentValue() + { + } + + public bool DoCalculateValue + { + set + { + this.docalculatevalue = value; + } + } + public bool DisableAllTransitionTechnologies { get @@ -25,12 +39,16 @@ public bool DisableAllTransitionTechnologies } set { - if (value) - { - this.decvalue += 1; - } else + if (this.docalculatevalue) { - this.decvalue -= 1; + if (value) + { + this.decvalue += 1; + } + else + { + this.decvalue -= 1; + } } this.disableAllTransitionTechnologies = value; @@ -45,14 +63,18 @@ public bool Disable6to4 } set { - if (value) + if (this.docalculatevalue) { - this.decvalue += 2; - } else - { - this.decvalue -= 2; + if (value) + { + this.decvalue += 2; + } + else + { + this.decvalue -= 2; + } } - + this.disable6to4 = value; } } @@ -65,13 +87,16 @@ public bool DisableISATAP } set { - if (value) - { - this.decvalue += 4; - } - else + if (this.docalculatevalue) { - this.decvalue -= 4; + if (value) + { + this.decvalue += 4; + } + else + { + this.decvalue -= 4; + } } this.disableISATAP = value; @@ -86,13 +111,16 @@ public bool DisableTeredo } set { - if (value) - { - this.decvalue += 8; - } - else + if (this.docalculatevalue) { - this.decvalue -= 8; + if (value) + { + this.decvalue += 8; + } + else + { + this.decvalue -= 8; + } } this.disableTeredo = value; @@ -107,13 +135,16 @@ public bool DisableIPv6OnAllNotTunnels } set { - if (value) + if (this.docalculatevalue) { - this.decvalue += 16; - } - else - { - this.decvalue -= 16; + if (value) + { + this.decvalue += 16; + } + else + { + this.decvalue -= 16; + } } this.disableIPv6OnAllNotTunnels = value; @@ -128,13 +159,16 @@ public bool PreferIPv4 } set { - if (value) - { - this.decvalue += 32; - } - else + if (this.docalculatevalue) { - this.decvalue -= 32; + if (value) + { + this.decvalue += 32; + } + else + { + this.decvalue -= 32; + } } this.preferIPv4 = value; @@ -149,20 +183,23 @@ public bool DisableAllIpHttps } set { - if (value) + if (this.docalculatevalue) { - this.decvalue += 128; - } - else - { - this.decvalue -= 128; + if (value) + { + this.decvalue += 128; + } + else + { + this.decvalue -= 128; + } } this.disableAllIpHttps = value; } } - public int GetDecValue() + public UInt32 GetDecValue() { return this.decvalue; } @@ -179,6 +216,93 @@ public string GetHexdecValueStr() return hexdec; } + /// + /// Load the DisabledComponents registery value and set boolean properties of this class. + /// Administrator rights are required to read the registery value. + /// + /// True if loading from registery succesfull. + public bool LoadFromRegisteryValue() + { + Microsoft.Win32.RegistryKey keyip6param; + try + { + keyip6param = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters", true); + } + catch (SecurityException) + { + return false; + } + + if (keyip6param == null) + { + return false; + } + + bool loadsucceeded = false; + try + { + Object objDisabledComponents = keyip6param.GetValue("DisabledComponents"); + UInt32 newdecvalue; + if (UInt32.TryParse(objDisabledComponents.ToString(), out newdecvalue)) + { + this.decvalue = newdecvalue; + if (newdecvalue - 128 <= 255) + { + newdecvalue -= 128; + this.DisableAllIpHttps = true; + } + + if (newdecvalue - 32 <= 255) + { + newdecvalue -= 32; + this.preferIPv4 = true; + } + + if (newdecvalue - 16 <= 255) + { + newdecvalue -= 16; + this.DisableIPv6OnAllNotTunnels = true; + } + + if (newdecvalue - 8 <= 255) + { + newdecvalue -= 8; + this.DisableTeredo = true; + } + + if (newdecvalue - 4 <= 255) + { + newdecvalue -= 4; + this.DisableISATAP = true; + } + + if (newdecvalue - 2 <= 255) + { + newdecvalue -= 2; + this.Disable6to4 = true; + } + + if (newdecvalue - 1 <= 255) + { + newdecvalue -= 1; + this.DisableAllTransitionTechnologies = true; + } + + loadsucceeded = true; + } + } catch (Exception ex) + { + System.Windows.Forms.MessageBox.Show(ex.Message); + } + + return loadsucceeded; + } + + /// + /// Write the DisabledComponents registery value. + /// Needs administrator rights to write the registery value. + /// + /// True if writing to registery is succesfull (needs administrator rights to do this). public bool WriteRegisteryValue() { Microsoft.Win32.RegistryKey keyip6param; @@ -199,13 +323,11 @@ public bool WriteRegisteryValue() bool writesucceeded = true; try { - //keyip6param.GetValue() keyip6param.SetValue("DisabledComponents", this.decvalue, Microsoft.Win32.RegistryValueKind.DWord); } catch (UnauthorizedAccessException) { writesucceeded = false; - //MessageBox.Show("No rights to edit registery key value", "No rights", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { diff --git a/IPv6Control/FrmMain.cs b/IPv6Control/FrmMain.cs index 49e2126..2221a72 100644 --- a/IPv6Control/FrmMain.cs +++ b/IPv6Control/FrmMain.cs @@ -1,5 +1,4 @@ using System; -using System.Security; using System.Windows.Forms; namespace IPv6Control @@ -8,11 +7,25 @@ public partial class FrmMain : Form { private DisabledComponentValue disabledComponentValue; + /// + /// Creating a new instance of FrmMain class. + /// public FrmMain() { InitializeComponent(); this.disabledComponentValue = new DisabledComponentValue(); this.Text = Program.AssemblyTitle + " v" + Program.AssemblyVersion; + if (this.disabledComponentValue.LoadFromRegisteryValue()) + { + this.DisplayNewDisabledComponentValue(); + this.SetCheckboxesByDisabledComponentValue(); + } + else + { + MessageBox.Show("Could not load current settings.\r\nAdministrator rights are required to read to the registery key."); + } + + this.disabledComponentValue.DoCalculateValue = true; } private void DisplayNewDisabledComponentValue() @@ -64,18 +77,34 @@ private void chxDisableAllIpHttps_CheckedChanged(object sender, System.EventArgs private void btnSave_Click(object sender, EventArgs e) { - DialogResult result = MessageBox.Show("Are you sure you want to write the new DisabledComponents DWORD hexdec. value: "+this.lblDisabledComponentHEX.Text+" to Windows registery? A reboot is required for change to be applied.", "Apply settings?", MessageBoxButtons.YesNo); + DialogResult result = MessageBox.Show("Are you sure you want to write the new DisabledComponents DWORD hexdec. value: " + this.lblDisabledComponentHEX.Text + " to Windows registery?\r\nA reboot is required for change to be applied.", + "Apply settings?", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.No) { return; } - if (!this.disabledComponentValue.WriteRegisteryValue()) + if (this.disabledComponentValue.WriteRegisteryValue()) + { + MessageBox.Show("Writing settings succesfull.\r\nA reboot is required to apply new settings.", "Settings saved successfull", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + else { MessageBox.Show("Cannot open registery key need administrator privilege.", "No administrator privilege", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + private void SetCheckboxesByDisabledComponentValue() + { + this.chxDisable6to4.Checked = this.disabledComponentValue.Disable6to4; + this.chxDisableAllIpHttps.Checked = this.disabledComponentValue.DisableAllIpHttps; + this.chxDisableAllTransitionTechnologies.Checked = this.disabledComponentValue.DisableAllTransitionTechnologies; + this.chxDisableIPv6OnAllNotTunnels.Checked = this.disabledComponentValue.DisableIPv6OnAllNotTunnels; + this.chxDisableISATAP.Checked = this.disabledComponentValue.DisableISATAP; + this.chxDisableTeredo.Checked = this.disabledComponentValue.DisableTeredo; + this.chxPreferIPv4.Checked = this.disabledComponentValue.PreferIPv4; + } + private void CheckboxesEnabledAll(bool enabled) { this.chxDisable6to4.Enabled = enabled;