diff --git a/PortProxyGUI/Data/Rule.cs b/PortProxyGUI/Data/Rule.cs index a777399..a9816ff 100644 --- a/PortProxyGUI/Data/Rule.cs +++ b/PortProxyGUI/Data/Rule.cs @@ -13,6 +13,7 @@ namespace PortProxyGUI.Data public int ConnectPort { get; set; } public string Comment { get; set; } public string Group { get; set; } + public string PingStatus { get; set; } public bool Valid => ListenPort > 0 && ConnectPort > 0; diff --git a/PortProxyGUI/PortProxyGUI.Designer.cs b/PortProxyGUI/PortProxyGUI.Designer.cs index 7c0962c..55c580e 100644 --- a/PortProxyGUI/PortProxyGUI.Designer.cs +++ b/PortProxyGUI/PortProxyGUI.Designer.cs @@ -37,6 +37,7 @@ this.columnHeader4 = new System.Windows.Forms.ColumnHeader(); this.columnHeader5 = new System.Windows.Forms.ColumnHeader(); this.columnHeader6 = new System.Windows.Forms.ColumnHeader(); + this.columnPingStatus = new System.Windows.Forms.ColumnHeader(); this.columnHeader7 = new System.Windows.Forms.ColumnHeader(); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.toolStripMenuItem_Enable = new System.Windows.Forms.ToolStripMenuItem(); @@ -51,6 +52,7 @@ this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.toolStripMenuItem_About = new System.Windows.Forms.ToolStripMenuItem(); this.imageListProxies = new System.Windows.Forms.ImageList(this.components); + this.TimerPingTargets = new System.Windows.Forms.Timer(this.components); this.contextMenuStrip1.SuspendLayout(); this.SuspendLayout(); // @@ -64,6 +66,7 @@ this.columnHeader4, this.columnHeader5, this.columnHeader6, + this.columnPingStatus, this.columnHeader7}); this.listViewProxies.ContextMenuStrip = this.contextMenuStrip1; resources.ApplyResources(this.listViewProxies, "listViewProxies"); @@ -104,6 +107,10 @@ this.columnHeader6.Tag = ""; resources.ApplyResources(this.columnHeader6, "columnHeader6"); // + // columnPingStatus + // + resources.ApplyResources(this.columnPingStatus, "columnPingStatus"); + // // columnHeader7 // resources.ApplyResources(this.columnHeader7, "columnHeader7"); @@ -189,6 +196,12 @@ this.imageListProxies.Images.SetKeyName(0, "disable.png"); this.imageListProxies.Images.SetKeyName(1, "enable.png"); // + // TimerPingTargets + // + this.TimerPingTargets.Enabled = true; + this.TimerPingTargets.Interval = 5000; + this.TimerPingTargets.Tick += new System.EventHandler(this.TimerPingTargets_Tick); + // // PortProxyGUI // resources.ApplyResources(this, "$this"); @@ -224,6 +237,8 @@ private System.Windows.Forms.ColumnHeader columnHeader7; internal System.Windows.Forms.ListView listViewProxies; private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem_FlushDnsCache; + private System.Windows.Forms.ColumnHeader columnPingStatus; + private System.Windows.Forms.Timer TimerPingTargets; } } diff --git a/PortProxyGUI/PortProxyGUI.cs b/PortProxyGUI/PortProxyGUI.cs index af4aa82..01a3962 100644 --- a/PortProxyGUI/PortProxyGUI.cs +++ b/PortProxyGUI/PortProxyGUI.cs @@ -2,6 +2,7 @@ using System; using System.Data; using System.Linq; +using System.Net.NetworkInformation; using System.Windows.Forms; using static System.Windows.Forms.ListViewItem; @@ -46,7 +47,8 @@ namespace PortProxyGUI ListenPort = listenPort, ConnectTo = subItems[4].Text.Trim(), ConnectPort = connectPort, - Comment = subItems[6].Text.Trim(), + PingStatus = subItems[6].Text.Trim(), + Comment = subItems[7].Text.Trim(), Group = item.Group?.Header.Trim(), }; return rule; @@ -154,7 +156,8 @@ namespace PortProxyGUI new ListViewSubItem(item, rule.ListenPort.ToString()) { Tag = "Number" }, new ListViewSubItem(item, rule.ConnectTo), new ListViewSubItem(item, rule.ConnectPort.ToString ()) { Tag = "Number" }, - new ListViewSubItem(item, rule.Comment ?? ""), + new ListViewSubItem(item, rule.PingStatus ?? string.Empty), + new ListViewSubItem(item, rule.Comment ?? string.Empty) }); if (rule.Group.IsNullOrWhiteSpace()) item.Group = null; @@ -314,5 +317,28 @@ namespace PortProxyGUI if (e.KeyCode == Keys.Delete) DeleteSelectedProxies(); } } + + private void TimerPingTargets_Tick(object sender, EventArgs e) + { + TimerPingTargets.Stop(); + try + { + var items = listViewProxies.Items.OfType(); + foreach (var item in items) + { + try + { + var rule = ParseRule(item); + //Ping Host + PingCheckerUtil.GetPingResult(rule.ConnectTo, 2, out IPStatus ipStatus, out _, out _); + rule.PingStatus = ipStatus.ToString(); + UpdateListViewItem(item, rule, item.ImageIndex); + } + catch { } + } + } + catch { } + TimerPingTargets.Start(); + } } } diff --git a/PortProxyGUI/PortProxyGUI.resx b/PortProxyGUI/PortProxyGUI.resx index 55babf0..4e3163a 100644 --- a/PortProxyGUI/PortProxyGUI.resx +++ b/PortProxyGUI/PortProxyGUI.resx @@ -94,6 +94,12 @@ 100 + + Ping Status + + + 104 + Comment @@ -105,28 +111,28 @@ - 180, 22 + 164, 22 Enable (&E) - 180, 22 + 164, 22 Disable (&I) - 177, 6 + 161, 6 - 180, 22 + 164, 22 Refresh (&F) - 180, 22 + 164, 22 Flush DNS Cache @@ -135,37 +141,37 @@ Will perform ipconfig/flushDNS - 177, 6 + 161, 6 - 180, 22 + 164, 22 New (&N) ... - 180, 22 + 164, 22 Modify (&M) ... - 180, 22 + 164, 22 Delete (&D) - 177, 6 + 161, 6 - 180, 22 + 164, 22 About - 181, 220 + 165, 198 contextMenuStrip1 @@ -184,10 +190,10 @@ 0, 0 - 4, 3, 4, 3 + 5, 3, 5, 3 - 704, 456 + 797, 456 182, 17 @@ -197,7 +203,7 @@ AAEAAAD/////AQAAAAAAAAAMAgAAAEZTeXN0ZW0uV2luZG93cy5Gb3JtcywgQ3VsdHVyZT1uZXV0cmFs LCBQdWJsaWNLZXlUb2tlbj1iNzdhNWM1NjE5MzRlMDg5BQEAAAAmU3lzdGVtLldpbmRvd3MuRm9ybXMu SW1hZ2VMaXN0U3RyZWFtZXIBAAAABERhdGEHAgIAAAAJAwAAAA8DAAAA3AgAAAJNU0Z0AUkBTAIBAQIB - AAEwAQEBMAEBARABAAEQAQAE/wEJAQAI/wFCAU0BNgEEBgABNgEEAgABKAMAAUADAAEQAwABAQEAAQgG + AAFYAQEBWAEBARABAAEQAQAE/wEJAQAI/wFCAU0BNgEEBgABNgEEAgABKAMAAUADAAEQAwABAQEAAQgG AAEEGAABgAIAAYADAAKAAQABgAMAAYABAAGAAQACgAIAA8ABAAHAAdwBwAEAAfABygGmAQABMwUAATMB AAEzAQABMwEAAjMCAAMWAQADHAEAAyIBAAMpAQADVQEAA00BAANCAQADOQEAAYABfAH/AQACUAH/AQAB kwEAAdYBAAH/AewBzAEAAcYB1gHvAQAB1gLnAQABkAGpAa0CAAH/ATMDAAFmAwABmQMAAcwCAAEzAwAC @@ -252,14 +258,20 @@ 1 + + 324, 17 + True - 6, 13 + 7, 15 - 704, 456 + 797, 456 + + + Segoe UI, 9pt @@ -2464,6 +2476,12 @@ System.Windows.Forms.ColumnHeader, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + columnPingStatus + + + System.Windows.Forms.ColumnHeader, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + columnHeader7 @@ -2542,6 +2560,12 @@ System.Windows.Forms.ImageList, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + TimerPingTargets + + + System.Windows.Forms.Timer, System.Windows.Forms, Culture=neutral, PublicKeyToken=b77a5c561934e089 + PortProxyGUI diff --git a/PortProxyGUI/~DS/PingCheckerUtil.cs b/PortProxyGUI/~DS/PingCheckerUtil.cs new file mode 100644 index 0000000..c986cbf --- /dev/null +++ b/PortProxyGUI/~DS/PingCheckerUtil.cs @@ -0,0 +1,31 @@ +using System.Net; +using System.Net.NetworkInformation; +using System.Text; + +namespace PortProxyGUI +{ + public static class PingCheckerUtil + { + // Adapted from https://docs.microsoft.com/en-us/dotnet/api/system.net.networkinformation.ping.send + public static bool GetPingResult(string ipAddress, int timeout, out IPStatus responseStatus, out IPAddress responseIpAddress, out long responseTime) + { + //Defaults + responseIpAddress = null; + responseTime = 0; + responseStatus = IPStatus.Unknown; + try + { + //Sending 32bytes + byte[] buffer = Encoding.ASCII.GetBytes("12345678901234567890123456789012"); + Ping pingSender = new Ping(); + PingOptions options = new PingOptions(64, true); + PingReply reply = pingSender.Send(ipAddress, timeout, buffer, options); + responseIpAddress = reply.Address; + responseTime = reply.RoundtripTime; + responseStatus = reply.Status; + return (reply.Status == IPStatus.Success) ? true : false; + } + catch { return false; } + } + } +}