ShareX/HelpersLib/Forms/DNSChangerForm.cs

162 lines
5.4 KiB
C#
Raw Normal View History

2014-03-22 14:39:28 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2008-2014 ShareX Developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
using System.Windows.Forms;
namespace HelpersLib
{
public partial class DNSChangerForm : Form
{
public DNSChangerForm()
{
InitializeComponent();
Icon = ShareXResources.Icon;
2014-03-29 19:58:41 +13:00
AddDNS("Manual");
AddDNS("Google Public DNS", "8.8.8.8", "8.8.4.4"); // https://developers.google.com/speed/public-dns/
AddDNS("OpenDNS", "208.67.222.222", "208.67.220.220"); // http://www.opendns.com/
AddDNS("Level 3 Communications", "4.2.2.1", "4.2.2.2"); // http://www.level3.com/
AddDNS("Norton ConnectSafe", "199.85.126.10", "199.85.127.10"); // https://dns.norton.com/
AddDNS("Comodo Secure DNS", "8.26.56.26", "8.20.247.20"); // http://www.comodo.com/secure-dns/
AddDNS("DNS Advantage", "156.154.70.1", "156.154.71.1"); // http://www.neustar.biz/services/dns-services/free-recursive-dns
AddDNS("Yandex DNS", "77.88.8.2", "77.88.8.88"); // http://dns.yandex.com/
2014-03-23 07:56:31 +13:00
2014-03-23 11:39:05 +13:00
foreach (AdapterInfo adapter in AdapterInfo.GetEnabledAdapters())
2014-03-22 14:39:28 +13:00
{
cbAdapters.Items.Add(adapter);
}
if (cbAdapters.Items.Count > 0)
{
cbAdapters.SelectedIndex = 0;
}
}
2014-03-29 19:58:41 +13:00
private void AddDNS(string name, string primary = null, string secondary = null)
{
cbDNSType.Items.Add(new DNSInfo(name, primary, secondary));
}
2014-03-22 14:39:28 +13:00
private void cbAdapters_SelectedIndexChanged(object sender, EventArgs e)
{
AdapterInfo adapter = cbAdapters.SelectedItem as AdapterInfo;
if (adapter != null)
{
string[] dns = adapter.GetDNS();
2014-03-22 16:16:35 +13:00
if (dns != null && dns.Length == 2)
2014-03-22 14:39:28 +13:00
{
2014-03-23 07:56:31 +13:00
cbAutomatic.Checked = false;
txtPreferredDNS.Text = dns[0];
txtAlternateDNS.Text = dns[1];
2014-03-22 14:39:28 +13:00
}
2014-03-23 07:56:31 +13:00
else
{
cbAutomatic.Checked = true;
}
cbDNSType.SelectedIndex = 0;
2014-03-22 14:39:28 +13:00
}
2014-03-23 07:56:31 +13:00
UpdateControls();
2014-03-22 14:39:28 +13:00
}
2014-03-23 07:56:31 +13:00
private void cbAutomatic_CheckedChanged(object sender, EventArgs e)
2014-03-22 14:39:28 +13:00
{
2014-03-23 07:56:31 +13:00
UpdateControls();
2014-03-22 14:39:28 +13:00
}
2014-03-23 07:56:31 +13:00
private void cbDNSType_SelectedIndexChanged(object sender, EventArgs e)
2014-03-22 14:39:28 +13:00
{
2014-03-23 07:56:31 +13:00
if (cbDNSType.SelectedIndex > 0)
2014-03-22 14:39:28 +13:00
{
2014-03-23 07:56:31 +13:00
DNSInfo dnsInfo = cbDNSType.SelectedItem as DNSInfo;
2014-03-22 14:39:28 +13:00
2014-03-23 07:56:31 +13:00
if (dnsInfo != null)
2014-03-22 14:39:28 +13:00
{
2014-03-23 07:56:31 +13:00
txtPreferredDNS.Text = dnsInfo.PrimaryDNS;
txtAlternateDNS.Text = dnsInfo.SecondaryDNS;
2014-03-22 14:39:28 +13:00
}
}
2014-03-23 07:56:31 +13:00
UpdateControls();
2014-03-22 14:39:28 +13:00
}
private void txtPreferredDNS_TextChanged(object sender, EventArgs e)
{
2014-03-23 07:56:31 +13:00
UpdateControls();
2014-03-22 14:39:28 +13:00
}
private void txtAlternateDNS_TextChanged(object sender, EventArgs e)
{
2014-03-23 07:56:31 +13:00
UpdateControls();
2014-03-22 14:39:28 +13:00
}
2014-03-23 07:56:31 +13:00
private void UpdateControls()
2014-03-22 14:39:28 +13:00
{
2014-03-23 12:15:13 +13:00
cbDNSType.Enabled = !cbAutomatic.Checked;
txtPreferredDNS.Enabled = !cbAutomatic.Checked && cbDNSType.SelectedIndex == 0;
txtAlternateDNS.Enabled = !cbAutomatic.Checked && cbDNSType.SelectedIndex == 0;
2014-03-23 07:56:31 +13:00
}
2014-03-22 14:39:28 +13:00
2014-03-23 07:56:31 +13:00
private void btnSave_Click(object sender, EventArgs e)
{
AdapterInfo adapter = cbAdapters.SelectedItem as AdapterInfo;
2014-03-22 14:39:28 +13:00
2014-03-23 07:56:31 +13:00
if (adapter != null)
{
bool result = false;
if (cbAutomatic.Checked)
2014-03-22 14:39:28 +13:00
{
2014-03-23 11:39:05 +13:00
result = adapter.SetDNSAutomatic();
2014-03-23 07:56:31 +13:00
}
else
{
string primaryDNS = txtPreferredDNS.Text.Trim();
string secondaryDNS = txtAlternateDNS.Text.Trim();
2014-03-23 12:15:13 +13:00
if (Helpers.IsValidIPAddress(primaryDNS) && Helpers.IsValidIPAddress(secondaryDNS))
2014-03-23 07:56:31 +13:00
{
result = adapter.SetDNS(primaryDNS, secondaryDNS);
}
}
if (result)
{
NativeMethods.DnsFlushResolverCache();
MessageBox.Show("DNS successfully set.", "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
2014-03-22 14:39:28 +13:00
}
}
}
2014-03-23 07:56:31 +13:00
private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
2014-03-22 14:39:28 +13:00
}
}