Added ping buttons to DNS Changer

This commit is contained in:
Jaex 2014-03-31 15:18:51 +03:00
parent 1aa1a762be
commit 5df88f8233
11 changed files with 296 additions and 10 deletions

View file

@ -27,7 +27,7 @@
using System.Collections.Generic;
using System.Management;
namespace HelpersLib
namespace DNSChanger
{
public class AdapterInfo : IDisposable
{

View file

@ -34,6 +34,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
@ -43,6 +44,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AdapterInfo.cs" />
<Compile Include="DNSChangerForm.cs">
<SubType>Form</SubType>
</Compile>

View file

@ -39,6 +39,8 @@ private void InitializeComponent()
this.cbDNSType = new System.Windows.Forms.ComboBox();
this.lblDNS = new System.Windows.Forms.Label();
this.cbAutomatic = new System.Windows.Forms.CheckBox();
this.btnPingPrimary = new System.Windows.Forms.Button();
this.btnPingSecondary = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cbAdapters
@ -64,7 +66,7 @@ private void InitializeComponent()
//
this.txtPreferredDNS.Location = new System.Drawing.Point(136, 84);
this.txtPreferredDNS.Name = "txtPreferredDNS";
this.txtPreferredDNS.Size = new System.Drawing.Size(256, 20);
this.txtPreferredDNS.Size = new System.Drawing.Size(214, 20);
this.txtPreferredDNS.TabIndex = 3;
this.txtPreferredDNS.TextChanged += new System.EventHandler(this.txtPreferredDNS_TextChanged);
//
@ -90,7 +92,7 @@ private void InitializeComponent()
//
this.txtAlternateDNS.Location = new System.Drawing.Point(136, 108);
this.txtAlternateDNS.Name = "txtAlternateDNS";
this.txtAlternateDNS.Size = new System.Drawing.Size(256, 20);
this.txtAlternateDNS.Size = new System.Drawing.Size(214, 20);
this.txtAlternateDNS.TabIndex = 4;
this.txtAlternateDNS.TextChanged += new System.EventHandler(this.txtAlternateDNS_TextChanged);
//
@ -144,11 +146,33 @@ private void InitializeComponent()
this.cbAutomatic.UseVisualStyleBackColor = true;
this.cbAutomatic.CheckedChanged += new System.EventHandler(this.cbAutomatic_CheckedChanged);
//
// btnPingPrimary
//
this.btnPingPrimary.Location = new System.Drawing.Point(352, 83);
this.btnPingPrimary.Name = "btnPingPrimary";
this.btnPingPrimary.Size = new System.Drawing.Size(40, 23);
this.btnPingPrimary.TabIndex = 10;
this.btnPingPrimary.Text = "Ping";
this.btnPingPrimary.UseVisualStyleBackColor = true;
this.btnPingPrimary.Click += new System.EventHandler(this.btnPingPrimary_Click);
//
// btnPingSecondary
//
this.btnPingSecondary.Location = new System.Drawing.Point(352, 107);
this.btnPingSecondary.Name = "btnPingSecondary";
this.btnPingSecondary.Size = new System.Drawing.Size(40, 23);
this.btnPingSecondary.TabIndex = 11;
this.btnPingSecondary.Text = "Ping";
this.btnPingSecondary.UseVisualStyleBackColor = true;
this.btnPingSecondary.Click += new System.EventHandler(this.btnPingSecondary_Click);
//
// DNSChangerForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(404, 170);
this.Controls.Add(this.btnPingSecondary);
this.Controls.Add(this.btnPingPrimary);
this.Controls.Add(this.cbAutomatic);
this.Controls.Add(this.lblDNS);
this.Controls.Add(this.cbDNSType);
@ -164,7 +188,7 @@ private void InitializeComponent()
this.MaximizeBox = false;
this.Name = "DNSChangerForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ShareX - DNS changer";
this.Text = "ShareX DNS Changer";
this.ResumeLayout(false);
this.PerformLayout();
@ -183,5 +207,7 @@ private void InitializeComponent()
private System.Windows.Forms.ComboBox cbDNSType;
private System.Windows.Forms.Label lblDNS;
private System.Windows.Forms.CheckBox cbAutomatic;
private System.Windows.Forms.Button btnPingPrimary;
private System.Windows.Forms.Button btnPingSecondary;
}
}

View file

@ -25,6 +25,9 @@
using HelpersLib;
using System;
using System.ComponentModel;
using System.Net.NetworkInformation;
using System.Threading;
using System.Windows.Forms;
namespace DNSChanger
@ -124,6 +127,29 @@ private void UpdateControls()
txtAlternateDNS.Enabled = !cbAutomatic.Checked && cbDNSType.SelectedIndex == 0;
}
private void SendPing(string ip)
{
if (!string.IsNullOrEmpty(ip))
{
btnPingPrimary.Enabled = btnPingSecondary.Enabled = false;
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += (sender, e) =>
{
PingResult pingResult = PingHelper.PingHost(ip);
MessageBox.Show(pingResult.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
};
bw.RunWorkerCompleted += (sender, e) =>
{
btnPingPrimary.Enabled = btnPingSecondary.Enabled = true;
};
bw.RunWorkerAsync();
}
}
private void btnSave_Click(object sender, EventArgs e)
{
AdapterInfo adapter = cbAdapters.SelectedItem as AdapterInfo;
@ -178,5 +204,15 @@ private void btnCancel_Click(object sender, EventArgs e)
{
Close();
}
private void btnPingPrimary_Click(object sender, EventArgs e)
{
SendPing(txtPreferredDNS.Text);
}
private void btnPingSecondary_Click(object sender, EventArgs e)
{
SendPing(txtAlternateDNS.Text);
}
}
}

View file

@ -1,4 +1,29 @@
using System;
#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.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

View file

@ -6,7 +6,7 @@
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ShareX DNS Changer")]
[assembly: AssemblyDescription("ShareX DNS Changer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("ShareX Developers")]
[assembly: AssemblyProduct("ShareX DNS Changer")]

View file

@ -63,7 +63,6 @@
<Reference Include="System.Data" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing.Design" />
<Reference Include="System.Management" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.Threading, Version=1.0.2856.102, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
@ -96,7 +95,8 @@
<DependentUpon>MonitorTestForm.cs</DependentUpon>
</Compile>
<Compile Include="Helpers\SSLBypassHelper.cs" />
<Compile Include="AdapterInfo.cs" />
<Compile Include="PingHelper.cs" />
<Compile Include="PingResult.cs" />
<Compile Include="UITypeEditors\EnumDescriptionConverter.cs" />
<Compile Include="UITypeEditors\DirectoryNameEditor.cs" />
<Compile Include="UITypeEditors\MyColorConverter.cs" />

96
HelpersLib/PingHelper.cs Normal file
View file

@ -0,0 +1,96 @@
#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 HelpersLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace HelpersLib
{
public static class PingHelper
{
public static PingResult PingHost(string host, int timeout = 1000, int pingCount = 4, int waitTime = 100)
{
PingResult pingResult = new PingResult();
IPAddress address = GetIpFromHost(host);
byte[] buffer = new byte[32];
PingOptions pingOptions = new PingOptions(128, true);
using (Ping ping = new Ping())
{
for (int i = 0; i < pingCount; i++)
{
try
{
PingReply pingReply = ping.Send(address, timeout, buffer, pingOptions);
if (pingReply != null)
{
pingResult.PingReplyList.Add(pingReply);
}
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
if (waitTime > 0 && i + 1 < pingCount)
{
Thread.Sleep(waitTime);
}
}
}
return pingResult;
}
private static IPAddress GetIpFromHost(string host)
{
IPAddress address = null;
if (!IPAddress.TryParse(host, out address))
{
try
{
address = Dns.GetHostEntry(host).AddressList[0];
}
catch (Exception e)
{
DebugHelper.WriteException(e);
}
}
return address;
}
}
}

98
HelpersLib/PingResult.cs Normal file
View file

@ -0,0 +1,98 @@
#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.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
namespace HelpersLib
{
public class PingResult
{
public List<PingReply> PingReplyList { get; private set; }
public int Min
{
get
{
return (int)PingReplyList.Where(x => x.Status == IPStatus.Success).Min(x => x.RoundtripTime);
}
}
public int Max
{
get
{
return (int)PingReplyList.Where(x => x.Status == IPStatus.Success).Max(x => x.RoundtripTime);
}
}
public int Average
{
get
{
return (int)PingReplyList.Where(x => x.Status == IPStatus.Success).Average(x => x.RoundtripTime);
}
}
public PingResult()
{
PingReplyList = new List<PingReply>();
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (PingReply pingReply in PingReplyList)
{
if (pingReply != null)
{
switch (pingReply.Status)
{
case IPStatus.Success:
sb.AppendLine(string.Format("Reply from {0}: bytes={1} time={2}ms TTL={3}", pingReply.Address, pingReply.Buffer.Length, pingReply.RoundtripTime, pingReply.Options.Ttl));
break;
case IPStatus.TimedOut:
sb.AppendLine("Request timed out.");
break;
default:
sb.AppendLine(string.Format("Ping failed: {0}", pingReply.Status.ToString()));
break;
}
}
}
if (PingReplyList.Any(x => x.Status == IPStatus.Success))
{
sb.AppendLine(string.Format("Minimum = {0}ms, Maximum = {1}ms, Average = {2}ms", Min, Max, Average));
}
return sb.ToString().Trim();
}
}
}

View file

@ -1,9 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.21005.1
VisualStudioVersion = 12.0.30110.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShareX", "ShareX\ShareX.csproj", "{C5AE4585-E9EC-4FA3-B75A-E1210635ACB6}"
ProjectSection(ProjectDependencies) = postProject
{F1061D2B-4C92-42B9-9A98-34118D6B45A8} = {F1061D2B-4C92-42B9-9A98-34118D6B45A8}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HistoryLib", "HistoryLib\HistoryLib.csproj", "{E7DE6237-AEA2-498B-8F56-9B392472C490}"
EndProject

View file

@ -191,7 +191,7 @@ public static string LogsFilePath
}
}
public static string ScreenshotsFolder
private static string ScreenshotsFolder
{
get
{