private static void SetNetworkDns(DnsProvider dnsProvider)
        {
            if (!Program.IsAdmin)
            {
                Program.RunAsAdmin("--network-dns-item " + dnsProvider.Name);
                return;
            }

            try
            {
                foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (nic.OperationalStatus != OperationalStatus.Up)
                    {
                        continue;
                    }

                    IPInterfaceProperties properties = nic.GetIPProperties();

                    if ((properties.DnsAddresses.Count > 0) && !properties.DnsAddresses[0].IsIPv6SiteLocal)
                    {
                        SetNameServer(nic, dnsProvider.Addresses);
                    }
                }

                MessageBox.Show("The network DNS servers were set to " + dnsProvider.Name + " successfully.", dnsProvider.Name + " Configured - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured while setting " + dnsProvider.Name + " as network DNS server. " + ex.Message, "Error - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void NetworkDnsMenuSubItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item        = sender as ToolStripMenuItem;
            DnsProvider       dnsProvider = item.Tag as DnsProvider;

            SetNetworkDns(dnsProvider);
        }
Exemplo n.º 3
0
        private void NetworkDnsMenuSubItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item        = sender as ToolStripMenuItem;
            DnsProvider       dnsProvider = item.Tag as DnsProvider;

            try
            {
                foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                    if (nic.OperationalStatus != OperationalStatus.Up)
                    {
                        continue;
                    }

                    IPInterfaceProperties properties = nic.GetIPProperties();

                    if ((properties.DnsAddresses.Count > 0) && !properties.DnsAddresses[0].IsIPv6SiteLocal)
                    {
                        SetNameServer(nic, dnsProvider.Addresses);
                    }
                }

                MessageBox.Show("The network DNS servers were set to " + dnsProvider.Name + " successfully.", dnsProvider.Name + " Configured - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured while setting " + dnsProvider.Name + " as network DNS server. " + ex.Message, "Error - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Exemplo n.º 4
0
        private void btnRestoreDefaults_Click(object sender, EventArgs e)
        {
            _dnsProviders.Clear();
            _dnsProviders.AddRange(DnsProvider.GetDefaultProviders());

            RefreshDnsProvidersList();
            ClearForm();
        }
Exemplo n.º 5
0
        private void btnAddUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtDnsProviderName.Text))
            {
                MessageBox.Show("Please enter a valid DNS Provider name.", "Missing DNS Provider!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            List <IPAddress> addresses = new List <IPAddress>();

            foreach (string item in txtIpv4Addresses.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (IPAddress.TryParse(item.Trim(), out IPAddress address) && (address.AddressFamily == AddressFamily.InterNetwork))
                {
                    addresses.Add(address);
                }
                else
                {
                    MessageBox.Show("Please enter a valid IPv4 address.", "Invalid IPv4 Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            foreach (string item in txtIpv6Addresses.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (IPAddress.TryParse(item.Trim(), out IPAddress address) && (address.AddressFamily == AddressFamily.InterNetworkV6))
                {
                    addresses.Add(address);
                }
                else
                {
                    MessageBox.Show("Please enter a valid IPv6 address.", "Invalid IPv6 Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }

            if (addresses.Count == 0)
            {
                MessageBox.Show("Please enter at least one valid DNS provider IP address.", "Missing DNS Provider IP Address!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            if ((btnAddUpdate.Text != "Add") && (listView1.SelectedItems.Count > 0))
            {
                ListViewItem selectedItem = listView1.SelectedItems[0];
                DnsProvider  dnsProvider  = selectedItem.Tag as DnsProvider;

                dnsProvider.Name      = txtDnsProviderName.Text.Trim();
                dnsProvider.Addresses = addresses;
            }
            else
            {
                _dnsProviders.Add(new DnsProvider(txtDnsProviderName.Text.Trim(), addresses));
            }

            RefreshDnsProvidersList();
            ClearForm();
        }
Exemplo n.º 6
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                ListViewItem selectedItem = listView1.SelectedItems[0];
                DnsProvider  dnsProvider  = selectedItem.Tag as DnsProvider;

                _dnsProviders.Remove(dnsProvider);
                listView1.Items.Remove(selectedItem);
            }

            RefreshDnsProvidersList();
            ClearForm();
        }
        private void LoadConfig()
        {
            try
            {
                using (FileStream fS = new FileStream(_configFile, FileMode.Open, FileAccess.Read))
                {
                    BinaryReader bR = new BinaryReader(fS);

                    if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "DT")
                    {
                        throw new InvalidDataException("Invalid DNS Server System Tray App config file format.");
                    }

                    switch (bR.ReadByte())
                    {
                    case 1:
                        int count = bR.ReadInt32();
                        _dnsProviders.Clear();

                        for (int i = 0; i < count; i++)
                        {
                            _dnsProviders.Add(new DnsProvider(bR));
                        }

                        _dnsProviders.Sort();
                        break;

                    default:
                        throw new NotSupportedException("DNS Server System Tray App config file format is not supported.");
                    }
                }
            }
            catch (FileNotFoundException)
            {
                _dnsProviders.Clear();
                _dnsProviders.AddRange(DnsProvider.GetDefaultProviders());
                _dnsProviders.Sort();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured while loading config file. " + ex.Message, "Error - " + Resources.ServiceName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }