diff --git a/HelpersLib/AdapterInfo.cs b/DNSChanger/AdapterInfo.cs similarity index 99% rename from HelpersLib/AdapterInfo.cs rename to DNSChanger/AdapterInfo.cs index a0f659b87..7d5b8b683 100644 --- a/HelpersLib/AdapterInfo.cs +++ b/DNSChanger/AdapterInfo.cs @@ -27,7 +27,7 @@ using System.Collections.Generic; using System.Management; -namespace HelpersLib +namespace DNSChanger { public class AdapterInfo : IDisposable { diff --git a/DNSChanger/DNSChanger.csproj b/DNSChanger/DNSChanger.csproj index 3a9643eca..6f5822950 100644 --- a/DNSChanger/DNSChanger.csproj +++ b/DNSChanger/DNSChanger.csproj @@ -34,6 +34,7 @@ + @@ -43,6 +44,7 @@ + Form diff --git a/DNSChanger/DNSChangerForm.Designer.cs b/DNSChanger/DNSChangerForm.Designer.cs index 4b9d392df..5f5e8fc46 100644 --- a/DNSChanger/DNSChangerForm.Designer.cs +++ b/DNSChanger/DNSChangerForm.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/DNSChanger/DNSChangerForm.cs b/DNSChanger/DNSChangerForm.cs index c6008890e..53c88918d 100644 --- a/DNSChanger/DNSChangerForm.cs +++ b/DNSChanger/DNSChangerForm.cs @@ -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); + } } } \ No newline at end of file diff --git a/DNSChanger/Program.cs b/DNSChanger/Program.cs index 60ce2d05b..9c8a200f0 100644 --- a/DNSChanger/Program.cs +++ b/DNSChanger/Program.cs @@ -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 . +*/ + +#endregion License Information (GPL v3) + +using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; diff --git a/DNSChanger/Properties/AssemblyInfo.cs b/DNSChanger/Properties/AssemblyInfo.cs index 065f815d0..cda3b47a5 100644 --- a/DNSChanger/Properties/AssemblyInfo.cs +++ b/DNSChanger/Properties/AssemblyInfo.cs @@ -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")] diff --git a/HelpersLib/HelpersLib.csproj b/HelpersLib/HelpersLib.csproj index cde6af8c7..593170cb3 100644 --- a/HelpersLib/HelpersLib.csproj +++ b/HelpersLib/HelpersLib.csproj @@ -63,7 +63,6 @@ - False @@ -96,7 +95,8 @@ MonitorTestForm.cs - + + diff --git a/HelpersLib/PingHelper.cs b/HelpersLib/PingHelper.cs new file mode 100644 index 000000000..1d3abd387 --- /dev/null +++ b/HelpersLib/PingHelper.cs @@ -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 . +*/ + +#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; + } + } +} \ No newline at end of file diff --git a/HelpersLib/PingResult.cs b/HelpersLib/PingResult.cs new file mode 100644 index 000000000..86512576c --- /dev/null +++ b/HelpersLib/PingResult.cs @@ -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 . +*/ + +#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 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(); + } + + 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(); + } + } +} \ No newline at end of file diff --git a/ShareX.sln b/ShareX.sln index 426493d49..5ff669014 100644 --- a/ShareX.sln +++ b/ShareX.sln @@ -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 diff --git a/ShareX/Program.cs b/ShareX/Program.cs index 3e08a807f..722c1619e 100644 --- a/ShareX/Program.cs +++ b/ShareX/Program.cs @@ -191,7 +191,7 @@ public static string LogsFilePath } } - public static string ScreenshotsFolder + private static string ScreenshotsFolder { get {